From abdf5c63787a5c673b6d84758a816bab0cc01f84 Mon Sep 17 00:00:00 2001 From: Aapo Romu Date: Fri, 18 Jan 2013 10:58:00 +0200 Subject: [PATCH 1/4] git ignore packages --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b9351a0..518be65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .project -.DS_Store \ No newline at end of file +.DS_Store +packages From a73ee27fb9216dcd07a90856c76462dd5e88b232 Mon Sep 17 00:00:00 2001 From: Aapo Romu Date: Fri, 18 Jan 2013 13:27:06 +0200 Subject: [PATCH 2/4] Added possibility to serve static files --- example/simple_server.dart | 18 ++++++++++++++++++ example/webapp/index.html | 21 +++++++++++++++++++++ lib/src/ehttp.dart | 30 ++++++++++++++++++++++++++++-- lib/synth.dart | 15 ++++++++++++++- 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 example/simple_server.dart create mode 100644 example/webapp/index.html diff --git a/example/simple_server.dart b/example/simple_server.dart new file mode 100644 index 0000000..7e16e15 --- /dev/null +++ b/example/simple_server.dart @@ -0,0 +1,18 @@ +import '../lib/synth.dart'; + +void main() { + synthInit(webRoot: 'example/webapp/'); + route('GET', '/query', (req, res) => idolQueryServe(req, res)); + + start(port: 7000); + print('Listening on port 7000'); + } + + void fileServe(req, res) { + res.write('Hello, World!'); + } + + void idolQueryServe(req, res) { + res.write('REST path ${req.path}'); + } + diff --git a/example/webapp/index.html b/example/webapp/index.html new file mode 100644 index 0000000..8a699f6 --- /dev/null +++ b/example/webapp/index.html @@ -0,0 +1,21 @@ + + + + + + index + + + +

index

+ +

Hello world from Dart!

+ +
+

+
+ + + + + diff --git a/lib/src/ehttp.dart b/lib/src/ehttp.dart index 462eb04..b054758 100644 --- a/lib/src/ehttp.dart +++ b/lib/src/ehttp.dart @@ -79,13 +79,28 @@ class Response implements HttpResponse { /** Enhanced Server object. Provides middleware server. */ class Server implements HttpServer { HttpServer _server; + String _webRoot; final List _middlewares = new List(); - Server(this._server) { + Server(this._server, this._webRoot) { _server.defaultRequestHandler = _defaultReqHandler; } void _defaultReqHandler(final HttpRequest req, final HttpResponse res) { + final String path = req.path == '/' ? '/index.html' : req.path; + print('reg: ${req.path} | Path: ${path}'); + final File file = new File('${_webRoot}${path}'); + file.exists().then((bool found) { + if (found) { + file.openInputStream().pipe(res.outputStream); + } else { + print('"Requested file ${_webRoot}${path} not found'); + _send404(res); + } + }); + } + + void _send404(HttpResponse res) { res.statusCode = HttpStatus.NOT_FOUND; res.headers.set(HttpHeaders.CONTENT_TYPE, "text/plain; charset=UTF-8"); res.outputStream.write('${res.statusCode} Page not found.'.charCodes); @@ -146,7 +161,18 @@ class Server implements HttpServer { _middlewares.add(middleware); } - void listen(String host, int port, {int backlog: 128}) => _server.listen(host, port); + void listen(String host, + int port, + {int backlog: 128, + String certificate_name, + bool requestClientCertificate: false}) { + listenPort(host, port, backlog: backlog); + + print(backlog); + print(certificate_name); + } + + void listenPort(String host, int port, {int backlog: 128}) => _server.listen(host, port, backlog: backlog); void listenOn(ServerSocket serverSocket) => _server.listenOn(serverSocket); addRequestHandler(bool matcher(HttpRequest request), void handler(HttpRequest request, HttpResponse response)) diff --git a/lib/synth.dart b/lib/synth.dart index 341ea3c..e6370d8 100644 --- a/lib/synth.dart +++ b/lib/synth.dart @@ -9,11 +9,18 @@ part 'src/ehttp.dart'; part 'src/render.dart'; // Private variables -final Server _server = new Server(new HttpServer()); +Server _server; final Router _router = new Router(); +void synthInit({String webRoot: 'webapp/'}) { + _server = new Server(new HttpServer(), webRoot); +} + void route(final String method, final String path, middleware, [Handler handler]) { + if (_server == null) { + throw new StateError('Synth not initialized. Please call synthInit first'); + } if (handler == null) { handler = middleware; middleware = null; @@ -23,9 +30,15 @@ void route(final String method, final String path, middleware, } void start({int port: 7000, String host: '127.0.0.1'}) { + if (_server == null) { + throw new StateError('Synth not initialized. Please call synthInit first'); + } _server.listen(host, port); } void use(final Middleware middleware) { + if (_server == null) { + throw new StateError('Synth not initialized. Please call synthInit first'); + } _server.addMiddleware(middleware); } \ No newline at end of file From 43351fd3f7576e1a010e1c978741c5632b7af581 Mon Sep 17 00:00:00 2001 From: Aapo Romu Date: Fri, 18 Jan 2013 13:51:35 +0200 Subject: [PATCH 3/4] Cleaned up example --- example/simple_server.dart | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/example/simple_server.dart b/example/simple_server.dart index 7e16e15..e6e8f65 100644 --- a/example/simple_server.dart +++ b/example/simple_server.dart @@ -2,17 +2,13 @@ import '../lib/synth.dart'; void main() { synthInit(webRoot: 'example/webapp/'); - route('GET', '/query', (req, res) => idolQueryServe(req, res)); + route('GET', '/myroute', (req, res) => myRouteHandler(req, res)); start(port: 7000); print('Listening on port 7000'); } - void fileServe(req, res) { - res.write('Hello, World!'); - } - - void idolQueryServe(req, res) { + void myRouteHandler(req, res) { res.write('REST path ${req.path}'); } From 647e1c43b17e9f7f396e4febb139ac19e13f0f35 Mon Sep 17 00:00:00 2001 From: Aapo Romu Date: Fri, 18 Jan 2013 13:57:21 +0200 Subject: [PATCH 4/4] Cleaned uneccessary debug --- lib/src/ehttp.dart | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/src/ehttp.dart b/lib/src/ehttp.dart index b054758..434402b 100644 --- a/lib/src/ehttp.dart +++ b/lib/src/ehttp.dart @@ -88,7 +88,6 @@ class Server implements HttpServer { void _defaultReqHandler(final HttpRequest req, final HttpResponse res) { final String path = req.path == '/' ? '/index.html' : req.path; - print('reg: ${req.path} | Path: ${path}'); final File file = new File('${_webRoot}${path}'); file.exists().then((bool found) { if (found) { @@ -166,10 +165,7 @@ class Server implements HttpServer { {int backlog: 128, String certificate_name, bool requestClientCertificate: false}) { - listenPort(host, port, backlog: backlog); - - print(backlog); - print(certificate_name); + listenPort(host, port, backlog: backlog); } void listenPort(String host, int port, {int backlog: 128}) => _server.listen(host, port, backlog: backlog);