1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Runs a webserver based on nitcorn that render things from model.
18 import popcorn
::pop_config
19 import popcorn
::pop_auth
24 redef class NitwebConfig
26 # Github client id used for Github OAuth login.
28 # * key: `github.client_id`
30 var github_client_id
: String is lazy
do return value_or_default
("github.client.id", "")
32 # Github client secret used for Github OAuth login.
34 # * key: `github.client_secret`
36 var github_client_secret
: String is lazy
do
37 return value_or_default
("github.client.secret", "")
41 redef class ToolContext
43 # Path to app config file.
44 var opt_config
= new OptionString("Path to app config file", "--config")
46 # Host name to bind on (will overwrite the config one).
47 var opt_host
= new OptionString("Host to bind the server on", "--host")
49 # Port number to bind on (will overwrite the config one).
50 var opt_port
= new OptionInt("Port number to use", -1, "--port")
52 # Web rendering phase.
53 var webphase
: Phase = new NitwebPhase(self, null)
57 option_context
.add_option
(opt_config
, opt_host
, opt_port
)
61 # Phase that builds the model and wait for http request to serve pages.
62 private class NitwebPhase
65 # Build the nitweb config from `toolcontext` options.
66 fun build_config
(toolcontext
: ToolContext, mainmodule
: MModule): NitwebConfig do
67 var config_file
= toolcontext
.opt_config
.value
68 if config_file
== null then config_file
= "nitweb.ini"
69 var config
= new NitwebConfig(
71 toolcontext
.modelbuilder
.model
,
73 toolcontext
.modelbuilder
)
74 var opt_host
= toolcontext
.opt_host
.value
75 if opt_host
!= null then config
["app.host"] = opt_host
76 var opt_port
= toolcontext
.opt_port
.value
77 if opt_port
>= 0 then config
["app.port"] = opt_port
.to_s
81 redef fun process_mainmodule
(mainmodule
, mmodules
)
83 var config
= build_config
(toolcontext
, mainmodule
)
84 config
.model
.nitdoc_md_processor
= config
.md_processor
88 app
.use_before
("/*", new SessionInit)
89 app
.use_before
("/*", new RequestClock)
90 app
.use
("/api", new APIRouter(config
))
91 app
.use
("/login", new GithubLogin(config
.github_client_id
))
92 app
.use
("/oauth", new GithubOAuthCallBack(config
.github_client_id
, config
.github_client_secret
))
93 app
.use
("/logout", new GithubLogout)
94 app
.use
("/*", new StaticHandler(toolcontext
.share_dir
/ "nitweb", "index.html"))
95 app
.use_after
("/*", new ConsoleLog)
97 app
.listen
(config
.app_host
, config
.app_port
)
102 var toolcontext
= new ToolContext
103 var tpl
= new Template
104 tpl
.add
"Usage: nitweb [OPTION]... <file.nit>...\n"
105 tpl
.add
"Run a webserver based on nitcorn that serves pages about model."
106 toolcontext
.tooldescription
= tpl
.write_to_string
109 toolcontext
.process_options
(args
)
110 var arguments
= toolcontext
.option_context
.rest
113 var model
= new Model
114 var mbuilder
= new ModelBuilder(model
, toolcontext
)
115 var mmodules
= mbuilder
.parse_full
(arguments
)
118 if mmodules
.is_empty
then return
120 toolcontext
.run_global_phases
(mmodules
)