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.
18 import popcorn
::pop_config
22 redef class ToolContext
24 # Path to app config file.
25 var opt_config
= new OptionString("Path to app config file", "--config")
27 # Host name to bind on (will overwrite the config one).
28 var opt_host
= new OptionString("Host to bind the server on", "--host")
30 # Port number to bind on (will overwrite the config one).
31 var opt_port
= new OptionInt("Port number to use", -1, "--port")
33 # Web rendering phase.
34 var webphase
: Phase = new NitwebPhase(self, null)
38 option_context
.add_option
(opt_config
, opt_host
, opt_port
)
42 # Phase that builds the model and wait for http request to serve pages.
43 private class NitwebPhase
46 # Build the nitweb config from `toolcontext` options.
47 fun build_config
(toolcontext
: ToolContext, mainmodule
: MModule): NitwebConfig do
48 var config_file
= toolcontext
.opt_config
.value
49 if config_file
== null then config_file
= "nitweb.ini"
50 var config
= new NitwebConfig(
52 toolcontext
.modelbuilder
.model
,
54 toolcontext
.modelbuilder
)
55 var opt_host
= toolcontext
.opt_host
.value
56 if opt_host
!= null then config
["app.host"] = opt_host
57 var opt_port
= toolcontext
.opt_port
.value
58 if opt_port
>= 0 then config
["app.port"] = opt_port
.to_s
62 # Build the nit catalog used in homepage.
63 fun build_catalog
(model
: Model, modelbuilder
: ModelBuilder): Catalog do
64 var catalog
= new Catalog(modelbuilder
)
65 for mpackage
in model
.mpackages
do
66 catalog
.deps
.add_node
(mpackage
)
67 for mgroup
in mpackage
.mgroups
do
68 for mmodule
in mgroup
.mmodules
do
69 for imported
in mmodule
.in_importation
.direct_greaters
do
70 var ip
= imported
.mpackage
71 if ip
== null or ip
== mpackage
then continue
72 catalog
.deps
.add_edge
(mpackage
, ip
)
76 catalog
.git_info
(mpackage
)
77 catalog
.package_page
(mpackage
)
82 redef fun process_mainmodule
(mainmodule
, mmodules
)
84 var model
= mainmodule
.model
85 var modelbuilder
= toolcontext
.modelbuilder
86 var config
= build_config
(toolcontext
, mainmodule
)
87 var catalog
= build_catalog
(model
, modelbuilder
)
91 app
.use_before
("/*", new SessionInit)
92 app
.use_before
("/*", new RequestClock)
93 app
.use
("/api", new NitwebAPIRouter(config
, catalog
))
94 app
.use
("/*", new StaticHandler(toolcontext
.share_dir
/ "nitweb", "index.html"))
95 app
.use_after
("/*", new ConsoleLog)
97 app
.listen
(config
.app_host
, config
.app_port
)
101 # Group all api handlers in one router.
102 class NitwebAPIRouter
105 # Catalog to pass to handlers.
109 use
("/catalog", new APICatalogRouter(config
, catalog
))
110 use
("/list", new APIList(config
))
111 use
("/search", new APISearch(config
))
112 use
("/random", new APIRandom(config
))
113 use
("/entity/:id", new APIEntity(config
))
114 use
("/code/:id", new APIEntityCode(config
))
115 use
("/uml/:id", new APIEntityUML(config
))
116 use
("/linearization/:id", new APIEntityLinearization(config
))
117 use
("/defs/:id", new APIEntityDefs(config
))
118 use
("/feedback/", new APIFeedbackRouter(config
))
119 use
("/inheritance/:id", new APIEntityInheritance(config
))
120 use
("/graph/", new APIGraphRouter(config
))
121 use
("/docdown/", new APIDocdown(config
))
122 use
("/metrics/", new APIMetricsRouter(config
))
127 var toolcontext
= new ToolContext
128 var tpl
= new Template
129 tpl
.add
"Usage: nitweb [OPTION]... <file.nit>...\n"
130 tpl
.add
"Run a webserver based on nitcorn that serves pages about model."
131 toolcontext
.tooldescription
= tpl
.write_to_string
134 toolcontext
.process_options
(args
)
135 var arguments
= toolcontext
.option_context
.rest
138 var model
= new Model
139 var mbuilder
= new ModelBuilder(model
, toolcontext
)
140 var mmodules
= mbuilder
.parse_full
(arguments
)
143 if mmodules
.is_empty
then return
145 toolcontext
.run_global_phases
(mmodules
)