nitweb: init session
[nit.git] / src / nitweb.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Runs a webserver based on nitcorn that render things from model.
16 module nitweb
17
18 import popcorn::pop_config
19 import frontend
20 import web
21
22 redef class ToolContext
23
24 # Path to app config file.
25 var opt_config = new OptionString("Path to app config file", "--config")
26
27 # Host name to bind on (will overwrite the config one).
28 var opt_host = new OptionString("Host to bind the server on", "--host")
29
30 # Port number to bind on (will overwrite the config one).
31 var opt_port = new OptionInt("Port number to use", -1, "--port")
32
33 # Web rendering phase.
34 var webphase: Phase = new NitwebPhase(self, null)
35
36 init do
37 super
38 option_context.add_option(opt_config, opt_host, opt_port)
39 end
40 end
41
42 # Phase that builds the model and wait for http request to serve pages.
43 private class NitwebPhase
44 super Phase
45
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(
51 config_file,
52 toolcontext.modelbuilder.model,
53 mainmodule,
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
59 return config
60 end
61
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)
73 end
74 end
75 end
76 catalog.git_info(mpackage)
77 catalog.package_page(mpackage)
78 end
79 return catalog
80 end
81
82 redef fun process_mainmodule(mainmodule, mmodules)
83 do
84 var model = mainmodule.model
85 var modelbuilder = toolcontext.modelbuilder
86 var config = build_config(toolcontext, mainmodule)
87 var catalog = build_catalog(model, modelbuilder)
88
89 var app = new App
90
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)
96
97 app.listen(config.app_host, config.app_port)
98 end
99 end
100
101 # Group all api handlers in one router.
102 class NitwebAPIRouter
103 super APIRouter
104
105 # Catalog to pass to handlers.
106 var catalog: Catalog
107
108 init do
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))
123 end
124 end
125
126 # build toolcontext
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
132
133 # process options
134 toolcontext.process_options(args)
135 var arguments = toolcontext.option_context.rest
136
137 # build model
138 var model = new Model
139 var mbuilder = new ModelBuilder(model, toolcontext)
140 var mmodules = mbuilder.parse_full(arguments)
141
142 # process
143 if mmodules.is_empty then return
144 mbuilder.run_phases
145 toolcontext.run_global_phases(mmodules)