9e9d54ff7cc57b6b8ee444113031dc09a094070b
[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
21 redef class ToolContext
22
23 # Path to app config file.
24 var opt_config = new OptionString("Path to app config file", "--config")
25
26 # Host name to bind on (will overwrite the config one).
27 var opt_host = new OptionString("Host to bind the server on", "--host")
28
29 # Port number to bind on (will overwrite the config one).
30 var opt_port = new OptionInt("Port number to use", -1, "--port")
31
32 # --no-private
33 var opt_no_private = new OptionBool("Do not show private entities", "--no-private")
34
35 # --no-fictive
36 var opt_no_fictive = new OptionBool("Do not show fictive entities", "--no-fictive")
37
38 # --no-test
39 var opt_no_test = new OptionBool("Do not show test related entities", "--no-test")
40
41 # --no-attribute
42 var opt_no_attribute = new OptionBool("Do not show attributes", "--no-attribute")
43
44 # --no-empty-doc
45 var opt_no_empty_doc = new OptionBool("Do not undocumented entities", "--no-empty-doc")
46
47 # Web rendering phase.
48 var webphase: Phase = new NitwebPhase(self, null)
49
50 init do
51 super
52 option_context.add_option(opt_config, opt_host, opt_port, opt_no_private,
53 opt_no_fictive, opt_no_test, opt_no_attribute, opt_no_empty_doc)
54 end
55 end
56
57 # Phase that builds the model and wait for http request to serve pages.
58 private class NitwebPhase
59 super Phase
60
61 # Build the nitweb config from `toolcontext` options.
62 fun build_config(toolcontext: ToolContext, mainmodule: MModule): NitwebConfig do
63
64 var model = toolcontext.modelbuilder.model
65
66 var filter = new ModelFilter(
67 if toolcontext.opt_no_private.value then protected_visibility else private_visibility,
68 accept_fictive = not toolcontext.opt_no_fictive.value,
69 accept_empty_doc = not toolcontext.opt_no_empty_doc.value,
70 accept_test = not toolcontext.opt_no_test.value,
71 accept_attribute = not toolcontext.opt_no_attribute.value
72 )
73
74 var catalog = build_catalog(toolcontext.modelbuilder, filter)
75
76 var config = new NitwebConfig(model, mainmodule, toolcontext.modelbuilder, filter, catalog)
77 var config_file = toolcontext.opt_config.value
78 if config_file == null then config.default_config_file = "nitweb.ini"
79 config.parse_options(args)
80 var opt_host = toolcontext.opt_host.value
81 if opt_host != null then config.ini["app.host"] = opt_host
82 var opt_port = toolcontext.opt_port.value
83 if opt_port >= 0 then config.ini["app.port"] = opt_port.to_s
84 return config
85 end
86
87 redef fun process_mainmodule(mainmodule, mmodules)
88 do
89 var config = build_config(toolcontext, mainmodule)
90 config.model.index # pre load model index
91 config.model.nitdoc_md_processor = config.md_processor
92
93 var app = new App
94
95 app.use_before("/*", new SessionInit)
96 app.use_before("/*", new RequestClock)
97 app.use("/api", new APIRouter(config))
98 app.use("/login", new GithubLogin(config.github_client_id))
99 app.use("/oauth", new GithubOAuthCallBack(config.github_client_id, config.github_client_secret))
100 app.use("/logout", new GithubLogout)
101 app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
102 app.use_after("/*", new ConsoleLog)
103
104 app.listen(config.app_host, config.app_port)
105 end
106
107 # Build the catalog
108 #
109 # This method should be called at nitweb startup.
110 fun build_catalog(modelbuilder: ModelBuilder, filter: nullable ModelFilter): Catalog do
111 var catalog = new Catalog(modelbuilder)
112 var mpackages = modelbuilder.model.collect_mpackages(filter)
113 # Compute the poset
114 for p in mpackages do
115 var g = p.root
116 assert g != null
117 modelbuilder.scan_group(g)
118
119 catalog.deps.add_node(p)
120 for gg in p.mgroups do for m in gg.mmodules do
121 for im in m.in_importation.direct_greaters do
122 var ip = im.mpackage
123 if ip == null or ip == p then continue
124 catalog.deps.add_edge(p, ip)
125 end
126 end
127 end
128 # Build the catalog
129 for mpackage in mpackages do
130 catalog.package_page(mpackage)
131 catalog.git_info(mpackage)
132 catalog.mpackage_stats(mpackage)
133 end
134 return catalog
135 end
136
137 end
138
139 # build toolcontext
140 var toolcontext = new ToolContext
141 var tpl = new Template
142 tpl.add "Usage: nitweb [OPTION]... <file.nit>...\n"
143 tpl.add "Run a webserver based on nitcorn that serves pages about model."
144 toolcontext.tooldescription = tpl.write_to_string
145
146 # process options
147 toolcontext.process_options(args)
148 var arguments = toolcontext.option_context.rest
149
150 # build model
151 var model = new Model
152 var mbuilder = new ModelBuilder(model, toolcontext)
153 var mmodules = mbuilder.parse_full(arguments)
154
155 # process
156 if mmodules.is_empty then return
157 mbuilder.run_phases
158 toolcontext.run_global_phases(mmodules)