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 diff --git a/example/simple_server.dart b/example/simple_server.dart new file mode 100644 index 0000000..e6e8f65 --- /dev/null +++ b/example/simple_server.dart @@ -0,0 +1,14 @@ +import '../lib/synth.dart'; + +void main() { + synthInit(webRoot: 'example/webapp/'); + route('GET', '/myroute', (req, res) => myRouteHandler(req, res)); + + start(port: 7000); + print('Listening on port 7000'); + } + + void myRouteHandler(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..434402b 100644 --- a/lib/src/ehttp.dart +++ b/lib/src/ehttp.dart @@ -79,13 +79,27 @@ 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; + 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 +160,15 @@ 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); + } + + 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