nitweb: migrate to config
[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_auth
19 import frontend
20 import web
21 import doc::doc_down
22
23 redef class NitwebConfig
24
25 # Github client id used for Github OAuth login.
26 #
27 # * key: `github.client_id`
28 # * default: ``
29 fun github_client_id: String do return ini["github.client.id"] or else ""
30
31 # Github client secret used for Github OAuth login.
32 #
33 # * key: `github.client_secret`
34 # * default: ``
35 fun github_client_secret: String do return ini["github.client.secret"] or else ""
36 end
37
38 redef class ToolContext
39
40 # Path to app config file.
41 var opt_config = new OptionString("Path to app config file", "--config")
42
43 # Host name to bind on (will overwrite the config one).
44 var opt_host = new OptionString("Host to bind the server on", "--host")
45
46 # Port number to bind on (will overwrite the config one).
47 var opt_port = new OptionInt("Port number to use", -1, "--port")
48
49 # Web rendering phase.
50 var webphase: Phase = new NitwebPhase(self, null)
51
52 init do
53 super
54 option_context.add_option(opt_config, opt_host, opt_port)
55 end
56 end
57
58 # Phase that builds the model and wait for http request to serve pages.
59 private class NitwebPhase
60 super Phase
61
62 # Build the nitweb config from `toolcontext` options.
63 fun build_config(toolcontext: ToolContext, mainmodule: MModule): NitwebConfig do
64 var config = new NitwebConfig(
65 toolcontext.modelbuilder.model,
66 mainmodule,
67 toolcontext.modelbuilder)
68 var config_file = toolcontext.opt_config.value
69 if config_file == null then config.default_config_file = "nitweb.ini"
70 config.parse_options(args)
71 var opt_host = toolcontext.opt_host.value
72 if opt_host != null then config.ini["app.host"] = opt_host
73 var opt_port = toolcontext.opt_port.value
74 if opt_port >= 0 then config.ini["app.port"] = opt_port.to_s
75 return config
76 end
77
78 redef fun process_mainmodule(mainmodule, mmodules)
79 do
80 var config = build_config(toolcontext, mainmodule)
81 config.model.nitdoc_md_processor = config.md_processor
82
83 var app = new App
84
85 app.use_before("/*", new SessionInit)
86 app.use_before("/*", new RequestClock)
87 app.use("/api", new APIRouter(config))
88 app.use("/login", new GithubLogin(config.github_client_id))
89 app.use("/oauth", new GithubOAuthCallBack(config.github_client_id, config.github_client_secret))
90 app.use("/logout", new GithubLogout)
91 app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
92 app.use_after("/*", new ConsoleLog)
93
94 app.listen(config.app_host, config.app_port)
95 end
96 end
97
98 # build toolcontext
99 var toolcontext = new ToolContext
100 var tpl = new Template
101 tpl.add "Usage: nitweb [OPTION]... <file.nit>...\n"
102 tpl.add "Run a webserver based on nitcorn that serves pages about model."
103 toolcontext.tooldescription = tpl.write_to_string
104
105 # process options
106 toolcontext.process_options(args)
107 var arguments = toolcontext.option_context.rest
108
109 # build model
110 var model = new Model
111 var mbuilder = new ModelBuilder(model, toolcontext)
112 var mmodules = mbuilder.parse_full(arguments)
113
114 # process
115 if mmodules.is_empty then return
116 mbuilder.run_phases
117 toolcontext.run_global_phases(mmodules)