From: Alexandre Terrasa Date: Mon, 16 May 2016 15:47:32 +0000 (-0400) Subject: lib/popcorn: introduce App implementation X-Git-Url: http://nitlanguage.org?hp=34c52786d675f4f750cf091c0b8e7402cc630851 lib/popcorn: introduce App implementation Signed-off-by: Alexandre Terrasa --- diff --git a/lib/popcorn/popcorn.nit b/lib/popcorn/popcorn.nit new file mode 100644 index 0000000..887c122 --- /dev/null +++ b/lib/popcorn/popcorn.nit @@ -0,0 +1,89 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2016 Alexandre Terrasa +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Application server abstraction on top of nitcorn. +module popcorn + +import nitcorn +import pop_middlewares +intrude import pop_handlers + +# App acts like a wrapper around a nitcorn `Action`. +redef class App + super Action + + # Do not show anything on console + var quiet = false is writable + + # Start listening on `host:port`. + fun listen(host: String, port: Int) do + var iface = "{host}:{port}" + var vh = new VirtualHost(iface) + + vh.routes.add new Route("/", self) + + var fac = new HttpFactory.and_libevent + fac.config.virtual_hosts.add vh + + if not quiet then + print "Launching server on http://{iface}/" + end + fac.run + end + + # Handle request from nitcorn + redef fun answer(req, uri) do + uri = uri.simplify_path + var res = new HttpResponse(404) + for route, handler in handlers do + handler.handle(route, uri, req, res) + end + if not res.sent then + res.send(error_tpl(res.status_code, res.status_message), 404) + end + res.session = req.session + return res + end + + # + fun error_tpl(status: Int, message: nullable String): Template do + return new ErrorTpl(status, message) + end +end + +# +class ErrorTpl + super Template + + # + var status: Int + + # + var message: nullable String + + redef fun rendering do add """ + + + + + {{{message or else status}}} + + +

{{{status}}} {{{message or else ""}}}

+ + """ + +end