From 31607686beab5cbacccf24573a9825b5dfd95278 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sun, 28 Feb 2016 08:11:17 -0500 Subject: [PATCH] Bless up --- .gitignore | 65 ++++++++++++++++++++++++++++++++++++++ lib/angel_framework.dart | 4 +++ lib/src/http/http.dart | 11 +++++++ lib/src/http/routable.dart | 10 ++++++ lib/src/http/route.dart | 14 ++++++++ lib/src/http/server.dart | 39 +++++++++++++++++++++++ pubspec.yaml | 7 ++++ 7 files changed, 150 insertions(+) create mode 100644 .gitignore create mode 100644 lib/angel_framework.dart create mode 100644 lib/src/http/http.dart create mode 100644 lib/src/http/routable.dart create mode 100644 lib/src/http/route.dart create mode 100644 lib/src/http/server.dart create mode 100644 pubspec.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..917abe7c --- /dev/null +++ b/.gitignore @@ -0,0 +1,65 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio + +*.iml + +## Directory-based project format: +.idea/ +# if you remove the above rule, at least ignore the following: + +# User-specific stuff: +# .idea/workspace.xml +# .idea/tasks.xml +# .idea/dictionaries + +# Sensitive or high-churn files: +# .idea/dataSources.ids +# .idea/dataSources.xml +# .idea/sqlDataSources.xml +# .idea/dynamic.xml +# .idea/uiDesigner.xml + +# Gradle: +# .idea/gradle.xml +# .idea/libraries + +# Mongo Explorer plugin: +# .idea/mongoSettings.xml + +## File-based project format: +*.ipr +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +### Dart template +# Don’t commit the following directories created by pub. +.buildlog +.pub/ +build/ +packages +.packages + +# Or the files created by dart2js. +*.dart.js +*.js_ +*.js.deps +*.js.map + +# Include when developing application packages. +pubspec.lock + diff --git a/lib/angel_framework.dart b/lib/angel_framework.dart new file mode 100644 index 00000000..207d8ea8 --- /dev/null +++ b/lib/angel_framework.dart @@ -0,0 +1,4 @@ +/// A controller-based MVC + WebSocket framework in Dart. +library angel_framework; + +export 'src/http/http.dart'; \ No newline at end of file diff --git a/lib/src/http/http.dart b/lib/src/http/http.dart new file mode 100644 index 00000000..3703c3bf --- /dev/null +++ b/lib/src/http/http.dart @@ -0,0 +1,11 @@ +/// HTTP logic +library angel_framework.http; + +import 'dart:async'; +import 'dart:io'; +import 'package:json_god/json_god.dart'; +import 'package:route/server.dart'; + +part 'route.dart'; +part 'routable.dart'; +part 'server.dart'; \ No newline at end of file diff --git a/lib/src/http/routable.dart b/lib/src/http/routable.dart new file mode 100644 index 00000000..5f91102c --- /dev/null +++ b/lib/src/http/routable.dart @@ -0,0 +1,10 @@ +part of angel_framework.http; + +/// A routable server that can handle dynamic requests. +class Routable { + /// Additional filters to be run on designated requests. + Map middleware = {}; + + /// Dynamic request paths that this server will respond to. + Map routes = {}; +} \ No newline at end of file diff --git a/lib/src/http/route.dart b/lib/src/http/route.dart new file mode 100644 index 00000000..669013c0 --- /dev/null +++ b/lib/src/http/route.dart @@ -0,0 +1,14 @@ +part of angel_framework.http; + +class Route { + Pattern matcher; + String method; + + Route(String method, Pattern path, [List handlers]) { + this.method = method; + if (path is RegExp) this.matcher = path; + else this.matcher = new RegExp('^' + + path.toString().replaceAll(new RegExp('\/'), r'\/').replaceAll( + new RegExp(':[a-zA-Z_]+'), '([^\/]+)') + r'$'); + } +} diff --git a/lib/src/http/server.dart b/lib/src/http/server.dart new file mode 100644 index 00000000..c98ca182 --- /dev/null +++ b/lib/src/http/server.dart @@ -0,0 +1,39 @@ +part of angel_framework.http; + +/// A function that binds +typedef Future ServerGenerator(InternetAddress address, int port); + +/// A powerful real-time/REST/MVC server class. +class Angel extends Routable { + ServerGenerator _serverGenerator; + + _startServer(InternetAddress address, int port) async { + var server = await _serverGenerator( + address ?? InternetAddress.LOOPBACK_IP_V4, port); + var router = new Router(server); + + this.routes.forEach((Route route, value) { + router.serve(route.matcher, method: route.method).listen(( + HttpRequest request) { + + }); + }); + } + + /// Starts the server. + void listen({InternetAddress address, int port: 3000}) { + runZoned(() async { + await _startServer(address, port); + }, onError: onError); + } + + /// Handles a server error. + void onError(e, [StackTrace stackTrace]) { + + } + + Angel() {} + + /// Creates an HTTPS server. + Angel.secure() {} +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..86b44c74 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,7 @@ +name: angel_framework +version: 0.0.1-dev +description: Core libraries for the Angel framework. +author: Tobe O +dependencies: + json_god: any + route: any \ No newline at end of file