1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Base classes used by `nitweb`.
18 import model
::model_views
22 # Nitcorn server runned by `nitweb`.
27 # var srv = new NitServer("localhost", 3000)
28 # srv.routes.add new Route("/", new MyAction)
39 # Routes knwon by the server.
40 var routes
= new Array[Route]
42 # Start listen on `host:port`.
44 var iface
= "{host}:{port}"
45 print
"Launching server on http://{iface}/"
47 var vh
= new VirtualHost(iface
)
48 for route
in routes
do vh
.routes
.add route
50 var fac
= new HttpFactory.and_libevent
51 fac
.config
.virtual_hosts
.add vh
56 # Specific nitcorn Action for nitweb.
60 # Link to the NitServer that runs this action.
63 # Build a custom http response for errors.
64 fun render_error
(code
: Int, message
: String): HttpResponse do
65 var response
= new HttpResponse(code
)
66 var tpl
= new Template
67 tpl
.add
"<h1>Error {code}</h1>"
68 tpl
.add
"<pre><code>{message.html_escape}</code></pre>"
69 response
.body
= tpl
.write_to_string
73 # Render a view as a HttpResponse 200.
74 fun render_view
(view
: NitView): HttpResponse do
75 var response
= new HttpResponse(200)
76 response
.body
= view
.render
(srv
).write_to_string
80 # Return a HttpResponse containing `json`.
81 fun render_json
(json
: Jsonable): HttpResponse do
82 var response
= new HttpResponse(200)
83 response
.body
= json
.to_json
88 # Specific nitcorn Action that uses a Model
95 # Init the model view from the `req` uri parameters.
96 fun init_model_view
(req
: HttpRequest): ModelView do
97 var view
= new ModelView(model
)
99 var show_private
= req
.bool_arg
("private") or else false
100 if not show_private
then view
.min_visibility
= protected_visibility
102 view
.include_fictive
= req
.bool_arg
("fictive") or else false
103 view
.include_empty_doc
= req
.bool_arg
("empty-doc") or else true
104 view
.include_test_suite
= req
.bool_arg
("test-suite") or else false
105 view
.include_attribute
= req
.bool_arg
("attributes") or else true
111 # A NitView is rendered by an action.
113 # Renders this view and returns something that can be written to a HTTP response.
114 fun render
(srv
: NitServer): Writable is abstract
117 redef class HttpRequest
118 # Does the client asked for a json formatted response?
120 # Checks the URL get parameter `?json=true`.
121 fun is_json_asked
: Bool do return bool_arg
("json") or else false