nitweb: factorize catalog building
[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
42 # Build the nit catalog used in homepage.
43 fun build_catalog(model: Model, modelbuilder: ModelBuilder): Catalog do
44 var catalog = new Catalog(modelbuilder)
45 for mpackage in model.mpackages do
46 catalog.deps.add_node(mpackage)
47 for mgroup in mpackage.mgroups do
48 for mmodule in mgroup.mmodules do
49 for imported in mmodule.in_importation.direct_greaters do
50 var ip = imported.mpackage
51 if ip == null or ip == mpackage then continue
52 catalog.deps.add_edge(mpackage, ip)
53 end
54 end
55 end
56 catalog.git_info(mpackage)
57 catalog.package_page(mpackage)
58 end
59 return catalog
60 end
61
62 redef fun process_mainmodule(mainmodule, mmodules)
63 do
64 var model = mainmodule.model
65 var modelbuilder = toolcontext.modelbuilder
66 var catalog = build_catalog(model, modelbuilder)
67
68 # Prepare mongo connection
69 var mongo = new MongoClient("mongodb://localhost:27017/")
70 var db = mongo.database("nitweb")
71 var collection = db.collection("stars")
72
73 # Run the server
74 var host = toolcontext.opt_host.value or else "localhost"
75 var port = toolcontext.opt_port.value
76
77 var app = new App
78
79 app.use_before("/*", new RequestClock)
80 app.use("/api", new NitwebAPIRouter(model, mainmodule, modelbuilder, catalog, stars))
81 app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
82 app.use_after("/*", new ConsoleLog)
83
84 app.listen(host, port.to_i)
85 end
86 end
87
88 # Group all api handlers in one router.
89 class NitwebAPIRouter
90 super APIRouter
91
92 # ModelBuilder to pass to handlers.
93 var modelbuilder: ModelBuilder
94
95 # Catalog to pass to handlers.
96 var catalog: Catalog
97
98 # Mongo collection used to store ratings.
99 var collection: MongoCollection
100
101 init do
102 use("/catalog", new APICatalogRouter(model, mainmodule, catalog))
103 use("/list", new APIList(model, mainmodule))
104 use("/search", new APISearch(model, mainmodule))
105 use("/random", new APIRandom(model, mainmodule))
106 use("/entity/:id", new APIEntity(model, mainmodule))
107 use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
108 use("/uml/:id", new APIEntityUML(model, mainmodule))
109 use("/linearization/:id", new APIEntityLinearization(model, mainmodule))
110 use("/defs/:id", new APIEntityDefs(model, mainmodule))
111 use("/feedback/", new APIFeedbackRouter(model, mainmodule, collection))
112 use("/inheritance/:id", new APIEntityInheritance(model, mainmodule))
113 use("/graph/", new APIGraphRouter(model, mainmodule))
114 use("/docdown/", new APIDocdown(model, mainmodule, modelbuilder))
115 use("/metrics/", new APIMetricsRouter(model, mainmodule))
116 end
117 end
118
119 # build toolcontext
120 var toolcontext = new ToolContext
121 var tpl = new Template
122 tpl.add "Usage: nitweb [OPTION]... <file.nit>...\n"
123 tpl.add "Run a webserver based on nitcorn that serves pages about model."
124 toolcontext.tooldescription = tpl.write_to_string
125
126 # process options
127 toolcontext.process_options(args)
128 var arguments = toolcontext.option_context.rest
129
130 # build model
131 var model = new Model
132 var mbuilder = new ModelBuilder(model, toolcontext)
133 var mmodules = mbuilder.parse_full(arguments)
134
135 # process
136 if mmodules.is_empty then return
137 mbuilder.run_phases
138 toolcontext.run_global_phases(mmodules)