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 catalog
= new Catalog(modelbuilder
)
48 for mpackage
in model
.mpackages
do
49 catalog
.deps
.add_node
(mpackage
)
50 for mgroup
in mpackage
.mgroups
do
51 for mmodule
in mgroup
.mmodules
do
52 for imported
in mmodule
.in_importation
.direct_greaters
do
53 var ip
= imported
.mpackage
54 if ip
== null or ip
== mpackage
then continue
55 catalog
.deps
.add_edge
(mpackage
, ip
)
59 catalog
.git_info
(mpackage
)
60 catalog
.package_page
(mpackage
)
64 var host
= toolcontext
.opt_host
.value
or else "localhost"
65 var port
= toolcontext
.opt_port
.value
69 app
.use_before
("/*", new RequestClock)
70 app
.use
("/api", new APIRouter(model
, modelbuilder
, mainmodule
, catalog
))
71 app
.use
("/*", new StaticHandler(toolcontext
.share_dir
/ "nitweb", "index.html"))
72 app
.use_after
("/*", new ConsoleLog)
74 app
.listen
(host
, port
.to_i
)
78 # Group all api handlers in one router.
82 # Model to pass to handlers.
85 # ModelBuilder to pass to handlers.
86 var modelbuilder
: ModelBuilder
88 # Mainmodule to pass to handlers.
89 var mainmodule
: MModule
91 # Catalog to pass to handlers.
95 use
("/catalog", new APICatalogRouter(model
, mainmodule
, catalog
))
96 use
("/list", new APIList(model
, mainmodule
))
97 use
("/search", new APISearch(model
, mainmodule
))
98 use
("/random", new APIRandom(model
, mainmodule
))
99 use
("/entity/:id", new APIEntity(model
, mainmodule
))
100 use
("/code/:id", new APIEntityCode(model
, mainmodule
, modelbuilder
))
101 use
("/uml/:id", new APIEntityUML(model
, mainmodule
))
102 use
("/linearization/:id", new APIEntityLinearization(model
, mainmodule
))
103 use
("/defs/:id", new APIEntityDefs(model
, mainmodule
))
104 use
("/inheritance/:id", new APIEntityInheritance(model
, mainmodule
))
105 use
("/graph/", new APIGraphRouter(model
, mainmodule
))
110 var toolcontext
= new ToolContext
111 var tpl
= new Template
112 tpl
.add
"Usage: nitweb [OPTION]... <file.nit>...\n"
113 tpl
.add
"Run a webserver based on nitcorn that serve pages about model."
114 toolcontext
.tooldescription
= tpl
.write_to_string
117 toolcontext
.process_options
(args
)
118 var arguments
= toolcontext
.option_context
.rest
121 var model
= new Model
122 var mbuilder
= new ModelBuilder(model
, toolcontext
)
123 var mmodules
= mbuilder
.parse_full
(arguments
)
126 if mmodules
.is_empty
then return
128 toolcontext
.run_global_phases
(mmodules
)