nitweb: move APIRouter to nitweb.nit
[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
21 redef class ToolContext
22
23 # Host name to bind on.
24 var opt_host = new OptionString("Host to bind the server on", "--host")
25
26 # Port number to bind on.
27 var opt_port = new OptionInt("Port number to use", 3000, "--port")
28
29 # Web rendering phase.
30 var webphase: Phase = new NitwebPhase(self, null)
31
32 init do
33 super
34 option_context.add_option(opt_host, opt_port)
35 end
36 end
37
38 # Phase that builds the model and wait for http request to serve pages.
39 private class NitwebPhase
40 super Phase
41 redef fun process_mainmodule(mainmodule, mmodules)
42 do
43 var model = mainmodule.model
44 var modelbuilder = toolcontext.modelbuilder
45
46 # Run the server
47 var host = toolcontext.opt_host.value or else "localhost"
48 var port = toolcontext.opt_port.value
49
50 var app = new App
51
52 app.use("/api", new APIRouter(model, modelbuilder, mainmodule))
53 app.use("/doc/:namespace", new DocAction(model, mainmodule, modelbuilder))
54 app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
55
56 app.listen(host, port.to_i)
57 end
58 end
59
60 # Group all api handlers in one router.
61 class APIRouter
62 super Router
63
64 # Model to pass to handlers.
65 var model: Model
66
67 # ModelBuilder to pass to handlers.
68 var modelbuilder: ModelBuilder
69
70 # Mainmodule to pass to handlers.
71 var mainmodule: MModule
72
73 # Catalog to pass to handlers.
74 var catalog: Catalog
75
76 init do
77 use("/list", new APIList(model, mainmodule))
78 use("/search", new APISearch(model, mainmodule))
79 use("/random", new APIRandom(model, mainmodule))
80 use("/entity/:id", new APIEntity(model, mainmodule))
81 use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
82 use("/uml/:id", new APIEntityUML(model, mainmodule))
83 end
84 end
85
86 # build toolcontext
87 var toolcontext = new ToolContext
88 var tpl = new Template
89 tpl.add "Usage: nitweb [OPTION]... <file.nit>...\n"
90 tpl.add "Run a webserver based on nitcorn that serve pages about model."
91 toolcontext.tooldescription = tpl.write_to_string
92
93 # process options
94 toolcontext.process_options(args)
95 var arguments = toolcontext.option_context.rest
96
97 # build model
98 var model = new Model
99 var mbuilder = new ModelBuilder(model, toolcontext)
100 var mmodules = mbuilder.parse_full(arguments)
101
102 # process
103 if mmodules.is_empty then return
104 mbuilder.run_phases
105 toolcontext.run_global_phases(mmodules)