tests: add some runtime error in nitin.input
[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 frontend
19 import web
20 import doc::doc_down
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 = new NitwebConfig(
49 toolcontext.modelbuilder.model,
50 mainmodule,
51 toolcontext.modelbuilder)
52 var config_file = toolcontext.opt_config.value
53 if config_file == null then config.default_config_file = "nitweb.ini"
54 config.parse_options(args)
55 var opt_host = toolcontext.opt_host.value
56 if opt_host != null then config.ini["app.host"] = opt_host
57 var opt_port = toolcontext.opt_port.value
58 if opt_port >= 0 then config.ini["app.port"] = opt_port.to_s
59 return config
60 end
61
62 redef fun process_mainmodule(mainmodule, mmodules)
63 do
64 var config = build_config(toolcontext, mainmodule)
65 config.model.nitdoc_md_processor = config.md_processor
66 config.build_catalog
67
68 var app = new App
69
70 app.use_before("/*", new SessionInit)
71 app.use_before("/*", new RequestClock)
72 app.use("/api", new APIRouter(config))
73 app.use("/login", new GithubLogin(config.github_client_id))
74 app.use("/oauth", new GithubOAuthCallBack(config.github_client_id, config.github_client_secret))
75 app.use("/logout", new GithubLogout)
76 app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
77 app.use_after("/*", new ConsoleLog)
78
79 app.listen(config.app_host, config.app_port)
80 end
81 end
82
83 # build toolcontext
84 var toolcontext = new ToolContext
85 var tpl = new Template
86 tpl.add "Usage: nitweb [OPTION]... <file.nit>...\n"
87 tpl.add "Run a webserver based on nitcorn that serves pages about model."
88 toolcontext.tooldescription = tpl.write_to_string
89
90 # process options
91 toolcontext.process_options(args)
92 var arguments = toolcontext.option_context.rest
93
94 # build model
95 var model = new Model
96 var mbuilder = new ModelBuilder(model, toolcontext)
97 var mmodules = mbuilder.parse_full(arguments)
98
99 # process
100 if mmodules.is_empty then return
101 mbuilder.run_phases
102 toolcontext.run_global_phases(mmodules)