# Base classes used by `nitweb`.
module web_base
-import frontend
-import nitcorn
-import json
+import model::model_views
+import model::model_json
+import popcorn
-# Nitcorn server runned by `nitweb`.
-#
-# Usage:
-#
-# ~~~nitish
-# var srv = new NitServer("localhost", 3000)
-# srv.routes.add new Route("/", new MyAction)
-# src.listen
-# ~~~
-class NitServer
-
- # Host to bind.
- var host: String
-
- # Port to use.
- var port: Int
+# Specific nitcorn Action that uses a Model
+class ModelAction
+ super Handler
- # Routes knwon by the server.
- var routes = new Array[Route]
+ # Model to use.
+ var model: Model
- # Start listen on `host:port`.
- fun listen do
- var iface = "{host}:{port}"
- print "Launching server on http://{iface}/"
-
- var vh = new VirtualHost(iface)
- for route in routes do vh.routes.add route
-
- var fac = new HttpFactory.and_libevent
- fac.config.virtual_hosts.add vh
- fac.run
+ # Find the MEntity ` with `full_name`.
+ fun find_mentity(model: ModelView, full_name: nullable String): nullable MEntity do
+ if full_name == null then return null
+ return model.mentity_by_full_name(full_name.from_percent_encoding)
end
-end
-# Specific nitcorn Action for nitweb.
-class NitAction
- super Action
+ # Init the model view from the `req` uri parameters.
+ fun init_model_view(req: HttpRequest): ModelView do
+ var view = new ModelView(model)
+ var show_private = req.bool_arg("private") or else false
+ if not show_private then view.min_visibility = protected_visibility
- # Link to the NitServer that runs this action.
- var srv: NitServer
+ view.include_fictive = req.bool_arg("fictive") or else false
+ view.include_empty_doc = req.bool_arg("empty-doc") or else true
+ view.include_test_suite = req.bool_arg("test-suite") or else false
+ view.include_attribute = req.bool_arg("attributes") or else true
- # Build a custom http response for errors.
- fun render_error(code: Int, message: String): HttpResponse do
- var response = new HttpResponse(code)
- var tpl = new Template
- tpl.add "<h1>Error {code}</h1>"
- tpl.add "<pre><code>{message.html_escape}</code></pre>"
- response.body = tpl.write_to_string
- return response
- end
-
- # Render a view as a HttpResponse 200.
- fun render_view(view: NitView): HttpResponse do
- var response = new HttpResponse(200)
- response.body = view.render(srv).write_to_string
- return response
- end
-
- # Return a HttpResponse containing `json`.
- fun render_json(json: Jsonable): HttpResponse do
- var response = new HttpResponse(200)
- response.body = json.to_json
- return response
+ return view
end
end
# A NitView is rendered by an action.
interface NitView
# Renders this view and returns something that can be written to a HTTP response.
- fun render(srv: NitServer): Writable is abstract
+ fun render: Writable is abstract
+end
+
+redef class HttpResponse
+ # Render a NitView as response.
+ fun send_view(view: NitView, status: nullable Int) do send(view.render, status)
end
redef class HttpRequest