nitweb: split APIRouter in modules
[nit.git] / src / nitweb.nit
index a953fcb..071a2af 100644 (file)
 module nitweb
 
 import popcorn::pop_config
+import popcorn::pop_auth
 import frontend
 import web
 
+redef class NitwebConfig
+
+       # Github client id used for Github OAuth login.
+       #
+       # * key: `github.client_id`
+       # * default: ``
+       var github_client_id: String is lazy do return value_or_default("github.client.id", "")
+
+       # Github client secret used for Github OAuth login.
+       #
+       # * key: `github.client_secret`
+       # * default: ``
+       var github_client_secret: String is lazy do
+               return value_or_default("github.client.secret", "")
+       end
+end
+
 redef class ToolContext
 
        # Path to app config file.
@@ -59,38 +77,18 @@ private class NitwebPhase
                return config
        end
 
-       # 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 config = build_config(toolcontext, mainmodule)
-               var catalog = build_catalog(model, modelbuilder)
 
                var app = new App
 
                app.use_before("/*", new SessionInit)
                app.use_before("/*", new RequestClock)
-               app.use("/api", new NitwebAPIRouter(config, catalog))
+               app.use("/api", new APIRouter(config))
+               app.use("/login", new GithubLogin(config.github_client_id))
+               app.use("/oauth", new GithubOAuthCallBack(config.github_client_id, config.github_client_secret))
+               app.use("/logout", new GithubLogout)
                app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
                app.use_after("/*", new ConsoleLog)
 
@@ -98,31 +96,6 @@ private class NitwebPhase
        end
 end
 
-# Group all api handlers in one router.
-class NitwebAPIRouter
-       super APIRouter
-
-       # Catalog to pass to handlers.
-       var catalog: Catalog
-
-       init do
-               use("/catalog", new APICatalogRouter(config, catalog))
-               use("/list", new APIList(config))
-               use("/search", new APISearch(config))
-               use("/random", new APIRandom(config))
-               use("/entity/:id", new APIEntity(config))
-               use("/code/:id", new APIEntityCode(config))
-               use("/uml/:id", new APIEntityUML(config))
-               use("/linearization/:id", new APIEntityLinearization(config))
-               use("/defs/:id", new APIEntityDefs(config))
-               use("/feedback/", new APIFeedbackRouter(config))
-               use("/inheritance/:id", new APIEntityInheritance(config))
-               use("/graph/", new APIGraphRouter(config))
-               use("/docdown/", new APIDocdown(config))
-               use("/metrics/", new APIMetricsRouter(config))
-       end
-end
-
 # build toolcontext
 var toolcontext = new ToolContext
 var tpl = new Template