Merge: curl: basic Unix domain socket support
[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 doc::api
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 # --no-private
34 var opt_no_private = new OptionBool("Do not show private entities", "--no-private")
35
36 # --no-fictive
37 var opt_no_fictive = new OptionBool("Do not show fictive entities", "--no-fictive")
38
39 # --no-test
40 var opt_no_test = new OptionBool("Do not show test related entities", "--no-test")
41
42 # --no-attribute
43 var opt_no_attribute = new OptionBool("Do not show attributes", "--no-attribute")
44
45 # --no-empty-doc
46 var opt_no_empty_doc = new OptionBool("Do not undocumented entities", "--no-empty-doc")
47
48 # Web rendering phase.
49 var webphase: Phase = new NitwebPhase(self, null)
50
51 init do
52 super
53 option_context.add_option(opt_config, opt_host, opt_port, opt_no_private,
54 opt_no_fictive, opt_no_test, opt_no_attribute, opt_no_empty_doc)
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
65 var model = toolcontext.modelbuilder.model
66
67 var filter = new ModelFilter(
68 if toolcontext.opt_no_private.value then protected_visibility else private_visibility,
69 accept_fictive = not toolcontext.opt_no_fictive.value,
70 accept_empty_doc = not toolcontext.opt_no_empty_doc.value,
71 accept_test = not toolcontext.opt_no_test.value,
72 accept_attribute = not toolcontext.opt_no_attribute.value
73 )
74
75 var view = new ModelView(model, mainmodule, filter)
76
77 var config = new NitwebConfig(model, mainmodule, toolcontext.modelbuilder, view)
78 var config_file = toolcontext.opt_config.value
79 if config_file == null then config.default_config_file = "nitweb.ini"
80 config.parse_options(args)
81 var opt_host = toolcontext.opt_host.value
82 if opt_host != null then config.ini["app.host"] = opt_host
83 var opt_port = toolcontext.opt_port.value
84 if opt_port >= 0 then config.ini["app.port"] = opt_port.to_s
85 return config
86 end
87
88 redef fun process_mainmodule(mainmodule, mmodules)
89 do
90 var config = build_config(toolcontext, mainmodule)
91 config.model.nitdoc_md_processor = config.md_processor
92 config.build_catalog
93
94 var app = new App
95
96 app.use_before("/*", new SessionInit)
97 app.use_before("/*", new RequestClock)
98 app.use("/api", new APIRouter(config))
99 app.use("/login", new GithubLogin(config.github_client_id))
100 app.use("/oauth", new GithubOAuthCallBack(config.github_client_id, config.github_client_secret))
101 app.use("/logout", new GithubLogout)
102 app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
103 app.use_after("/*", new ConsoleLog)
104
105 app.listen(config.app_host, config.app_port)
106 end
107 end
108
109 # build toolcontext
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 serves pages about model."
114 toolcontext.tooldescription = tpl.write_to_string
115
116 # process options
117 toolcontext.process_options(args)
118 var arguments = toolcontext.option_context.rest
119
120 # build model
121 var model = new Model
122 var mbuilder = new ModelBuilder(model, toolcontext)
123 var mmodules = mbuilder.parse_full(arguments)
124
125 # process
126 if mmodules.is_empty then return
127 mbuilder.run_phases
128 toolcontext.run_global_phases(mmodules)