nitweb: add API user login/logout end points
[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 popcorn::pop_auth
20 import frontend
21 import web
22
23 redef class NitwebConfig
24
25 # Github client id used for Github OAuth login.
26 #
27 # * key: `github.client_id`
28 # * default: ``
29 var github_client_id: String is lazy do return value_or_default("github.client.id", "")
30
31 # Github client secret used for Github OAuth login.
32 #
33 # * key: `github.client_secret`
34 # * default: ``
35 var github_client_secret: String is lazy do
36 return value_or_default("github.client.secret", "")
37 end
38 end
39
40 redef class ToolContext
41
42 # Path to app config file.
43 var opt_config = new OptionString("Path to app config file", "--config")
44
45 # Host name to bind on (will overwrite the config one).
46 var opt_host = new OptionString("Host to bind the server on", "--host")
47
48 # Port number to bind on (will overwrite the config one).
49 var opt_port = new OptionInt("Port number to use", -1, "--port")
50
51 # Web rendering phase.
52 var webphase: Phase = new NitwebPhase(self, null)
53
54 init do
55 super
56 option_context.add_option(opt_config, opt_host, opt_port)
57 end
58 end
59
60 # Phase that builds the model and wait for http request to serve pages.
61 private class NitwebPhase
62 super Phase
63
64 # Build the nitweb config from `toolcontext` options.
65 fun build_config(toolcontext: ToolContext, mainmodule: MModule): NitwebConfig do
66 var config_file = toolcontext.opt_config.value
67 if config_file == null then config_file = "nitweb.ini"
68 var config = new NitwebConfig(
69 config_file,
70 toolcontext.modelbuilder.model,
71 mainmodule,
72 toolcontext.modelbuilder)
73 var opt_host = toolcontext.opt_host.value
74 if opt_host != null then config["app.host"] = opt_host
75 var opt_port = toolcontext.opt_port.value
76 if opt_port >= 0 then config["app.port"] = opt_port.to_s
77 return config
78 end
79
80 # Build the nit catalog used in homepage.
81 fun build_catalog(model: Model, modelbuilder: ModelBuilder): Catalog do
82 var catalog = new Catalog(modelbuilder)
83 for mpackage in model.mpackages do
84 catalog.deps.add_node(mpackage)
85 for mgroup in mpackage.mgroups do
86 for mmodule in mgroup.mmodules do
87 for imported in mmodule.in_importation.direct_greaters do
88 var ip = imported.mpackage
89 if ip == null or ip == mpackage then continue
90 catalog.deps.add_edge(mpackage, ip)
91 end
92 end
93 end
94 catalog.git_info(mpackage)
95 catalog.package_page(mpackage)
96 end
97 return catalog
98 end
99
100 redef fun process_mainmodule(mainmodule, mmodules)
101 do
102 var model = mainmodule.model
103 var modelbuilder = toolcontext.modelbuilder
104 var config = build_config(toolcontext, mainmodule)
105 var catalog = build_catalog(model, modelbuilder)
106
107 var app = new App
108
109 app.use_before("/*", new SessionInit)
110 app.use_before("/*", new RequestClock)
111 app.use("/api", new NitwebAPIRouter(config, catalog))
112 app.use("/login", new GithubLogin(config.github_client_id))
113 app.use("/oauth", new GithubOAuthCallBack(config.github_client_id, config.github_client_secret))
114 app.use("/logout", new GithubLogout)
115 app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
116 app.use_after("/*", new ConsoleLog)
117
118 app.listen(config.app_host, config.app_port)
119 end
120 end
121
122 # Group all api handlers in one router.
123 class NitwebAPIRouter
124 super APIRouter
125
126 # Catalog to pass to handlers.
127 var catalog: Catalog
128
129 init do
130 use("/catalog", new APICatalogRouter(config, catalog))
131 use("/list", new APIList(config))
132 use("/search", new APISearch(config))
133 use("/random", new APIRandom(config))
134 use("/entity/:id", new APIEntity(config))
135 use("/code/:id", new APIEntityCode(config))
136 use("/uml/:id", new APIEntityUML(config))
137 use("/linearization/:id", new APIEntityLinearization(config))
138 use("/defs/:id", new APIEntityDefs(config))
139 use("/feedback/", new APIFeedbackRouter(config))
140 use("/inheritance/:id", new APIEntityInheritance(config))
141 use("/graph/", new APIGraphRouter(config))
142 use("/docdown/", new APIDocdown(config))
143 use("/metrics/", new APIMetricsRouter(config))
144 use("/user", new GithubUser)
145 end
146 end
147
148 # build toolcontext
149 var toolcontext = new ToolContext
150 var tpl = new Template
151 tpl.add "Usage: nitweb [OPTION]... <file.nit>...\n"
152 tpl.add "Run a webserver based on nitcorn that serves pages about model."
153 toolcontext.tooldescription = tpl.write_to_string
154
155 # process options
156 toolcontext.process_options(args)
157 var arguments = toolcontext.option_context.rest
158
159 # build model
160 var model = new Model
161 var mbuilder = new ModelBuilder(model, toolcontext)
162 var mmodules = mbuilder.parse_full(arguments)
163
164 # process
165 if mmodules.is_empty then return
166 mbuilder.run_phases
167 toolcontext.run_global_phases(mmodules)