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 # Runs a webserver based on nitcorn that render things from model.
21 redef class ToolContext
23 # Host name to bind on.
24 var opt_host
= new OptionString("Host to bind the server on", "--host")
26 # Port number to bind on.
27 var opt_port
= new OptionInt("Port number to use", 3000, "--port")
29 # Web rendering phase.
30 var webphase
: Phase = new NitwebPhase(self, null)
34 option_context
.add_option
(opt_host
, opt_port
)
38 # Phase that builds the model and wait for http request to serve pages.
39 private class NitwebPhase
41 redef fun process_mainmodule
(mainmodule
, mmodules
)
43 var model
= mainmodule
.model
44 var modelbuilder
= toolcontext
.modelbuilder
47 var host
= toolcontext
.opt_host
.value
or else "localhost"
48 var port
= toolcontext
.opt_port
.value
50 var srv
= new NitServer(host
, port
.to_i
)
51 srv
.routes
.add
new Route("/random", new RandomAction(srv
, model
))
52 srv
.routes
.add
new Route("/doc/:namespace", new DocAction(srv
, model
, modelbuilder
))
53 srv
.routes
.add
new Route("/code/:namespace", new CodeAction(srv
, model
, modelbuilder
))
54 srv
.routes
.add
new Route("/search/:namespace", new SearchAction(srv
, model
))
55 srv
.routes
.add
new Route("/", new TreeAction(srv
, model
))
62 var toolcontext
= new ToolContext
63 var tpl
= new Template
64 tpl
.add
"Usage: nitweb [OPTION]... <file.nit>...\n"
65 tpl
.add
"Run a webserver based on nitcorn that serve pages about model."
66 toolcontext
.tooldescription
= tpl
.write_to_string
69 toolcontext
.process_options
(args
)
70 var arguments
= toolcontext
.option_context
.rest
74 var mbuilder
= new ModelBuilder(model
, toolcontext
)
75 var mmodules
= mbuilder
.parse_full
(arguments
)
78 if mmodules
.is_empty
then return
80 toolcontext
.run_global_phases
(mmodules
)