nitweb: factorize catalog building
[nit.git] / src / nitweb.nit
index 1df4153..3f3b7f5 100644 (file)
@@ -38,10 +38,37 @@ end
 # Phase that builds the model and wait for http request to serve pages.
 private class NitwebPhase
        super Phase
+
+       # Build the nit catalog used in homepage.
+       fun build_catalog(model: Model, modelbuilder: ModelBuilder): Catalog do
+               var catalog = new Catalog(modelbuilder)
+               for mpackage in model.mpackages do
+                       catalog.deps.add_node(mpackage)
+                       for mgroup in mpackage.mgroups do
+                               for mmodule in mgroup.mmodules do
+                                       for imported in mmodule.in_importation.direct_greaters do
+                                               var ip = imported.mpackage
+                                               if ip == null or ip == mpackage then continue
+                                               catalog.deps.add_edge(mpackage, ip)
+                                       end
+                               end
+                       end
+                       catalog.git_info(mpackage)
+                       catalog.package_page(mpackage)
+               end
+               return catalog
+       end
+
        redef fun process_mainmodule(mainmodule, mmodules)
        do
                var model = mainmodule.model
                var modelbuilder = toolcontext.modelbuilder
+               var catalog = build_catalog(model, modelbuilder)
+
+               # Prepare mongo connection
+               var mongo = new MongoClient("mongodb://localhost:27017/")
+               var db = mongo.database("nitweb")
+               var collection = db.collection("stars")
 
                # Run the server
                var host = toolcontext.opt_host.value or else "localhost"
@@ -49,19 +76,51 @@ private class NitwebPhase
 
                var app = new App
 
-               app.use("/api", new APIRouter(model, modelbuilder, mainmodule))
-               app.use("/doc/:namespace", new DocAction(model, mainmodule, modelbuilder))
-               app.use("/", new TreeAction(model, mainmodule))
+               app.use_before("/*", new RequestClock)
+               app.use("/api", new NitwebAPIRouter(model, mainmodule, modelbuilder, catalog, stars))
+               app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
+               app.use_after("/*", new ConsoleLog)
 
                app.listen(host, port.to_i)
        end
 end
 
+# Group all api handlers in one router.
+class NitwebAPIRouter
+       super APIRouter
+
+       # ModelBuilder to pass to handlers.
+       var modelbuilder: ModelBuilder
+
+       # Catalog to pass to handlers.
+       var catalog: Catalog
+
+       # Mongo collection used to store ratings.
+       var collection: MongoCollection
+
+       init do
+               use("/catalog", new APICatalogRouter(model, mainmodule, catalog))
+               use("/list", new APIList(model, mainmodule))
+               use("/search", new APISearch(model, mainmodule))
+               use("/random", new APIRandom(model, mainmodule))
+               use("/entity/:id", new APIEntity(model, mainmodule))
+               use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
+               use("/uml/:id", new APIEntityUML(model, mainmodule))
+               use("/linearization/:id", new APIEntityLinearization(model, mainmodule))
+               use("/defs/:id", new APIEntityDefs(model, mainmodule))
+               use("/feedback/", new APIFeedbackRouter(model, mainmodule, collection))
+               use("/inheritance/:id", new APIEntityInheritance(model, mainmodule))
+               use("/graph/", new APIGraphRouter(model, mainmodule))
+               use("/docdown/", new APIDocdown(model, mainmodule, modelbuilder))
+               use("/metrics/", new APIMetricsRouter(model, mainmodule))
+       end
+end
+
 # build toolcontext
 var toolcontext = new ToolContext
 var tpl = new Template
 tpl.add "Usage: nitweb [OPTION]... <file.nit>...\n"
-tpl.add "Run a webserver based on nitcorn that serve pages about model."
+tpl.add "Run a webserver based on nitcorn that serves pages about model."
 toolcontext.tooldescription = tpl.write_to_string
 
 # process options