import frontend
import web
+import doc::doc_down
redef class ToolContext
- # Host name to bind on.
+ # Path to app config file.
+ var opt_config = new OptionString("Path to app config file", "--config")
+
+ # Host name to bind on (will overwrite the config one).
var opt_host = new OptionString("Host to bind the server on", "--host")
- # Port number to bind on.
- var opt_port = new OptionInt("Port number to use", 3000, "--port")
+ # Port number to bind on (will overwrite the config one).
+ var opt_port = new OptionInt("Port number to use", -1, "--port")
# Web rendering phase.
var webphase: Phase = new NitwebPhase(self, null)
init do
super
- option_context.add_option(opt_host, opt_port)
+ option_context.add_option(opt_config, opt_host, opt_port)
end
end
# Phase that builds the model and wait for http request to serve pages.
private class NitwebPhase
super Phase
+
+ # Build the nitweb config from `toolcontext` options.
+ fun build_config(toolcontext: ToolContext, mainmodule: MModule): NitwebConfig do
+ var config = new NitwebConfig(
+ toolcontext.modelbuilder.model,
+ mainmodule,
+ toolcontext.modelbuilder)
+ var config_file = toolcontext.opt_config.value
+ if config_file == null then config.default_config_file = "nitweb.ini"
+ config.parse_options(args)
+ var opt_host = toolcontext.opt_host.value
+ if opt_host != null then config.ini["app.host"] = opt_host
+ var opt_port = toolcontext.opt_port.value
+ if opt_port >= 0 then config.ini["app.port"] = opt_port.to_s
+ return config
+ end
+
redef fun process_mainmodule(mainmodule, mmodules)
do
- var model = mainmodule.model
- var modelbuilder = toolcontext.modelbuilder
-
- # Run the server
- var host = toolcontext.opt_host.value or else "localhost"
- var port = toolcontext.opt_port.value
-
- var srv = new NitServer(host, port.to_i)
- srv.routes.add new Route("/random", new RandomAction(srv, model))
- srv.routes.add new Route("/doc/:namespace", new DocAction(srv, model, modelbuilder))
- srv.routes.add new Route("/code/:namespace", new CodeAction(srv, model, modelbuilder))
- srv.routes.add new Route("/search/:namespace", new SearchAction(srv, model))
- srv.routes.add new Route("/uml/:namespace", new UMLDiagramAction(srv, model, mainmodule))
- srv.routes.add new Route("/", new TreeAction(srv, model))
-
- srv.listen
+ var config = build_config(toolcontext, mainmodule)
+ config.model.nitdoc_md_processor = config.md_processor
+
+ var app = new App
+
+ app.use_before("/*", new SessionInit)
+ app.use_before("/*", new RequestClock)
+ 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)
+
+ app.listen(config.app_host, config.app_port)
end
end
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