1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Configuration file and options for Popcorn apps
19 # `pop_config` provide a configuration framework for Popcorn apps based on ini
22 # By default `AppConfig` provides `app.host` and `app.port` keys, it's all we
23 # need to start an app:
27 # import popcorn::pop_config
29 # # Build config from options
30 # var config = new AppConfig
31 # config.parse_options(args)
35 # app.listen(config.app_host, config.app_port)
38 # For more advanced uses, `AppConfig` and `AppOptions` can be specialized to
39 # offer additional config options:
43 # import popcorn::pop_config
48 # # My secret code I don't want to share in my source repository
49 # fun secret: String do return opt_secret.value or else ini["secret"] or else "my-secret"
52 # var opt_secret = new OptionString("My secret string", "--secret")
56 # add_option opt_secret
63 # # Config to use to access `secret`
64 # var config: MyConfig
66 # redef fun get(req, res) do
67 # res.send config.secret
71 # var config = new MyConfig
72 # config.parse_options(args)
75 # app.use("/secret", new SecretHandler(config))
76 # app.listen(config.app_host, config.app_port)
82 # Configuration file for Popcorn apps
86 # import popcorn::pop_config
88 # # Build config from default values
89 # var config = new AppConfig
90 # config.parse_options(args)
92 # # Change config values
93 # config.ini["app.port"] = 3001.to_s
97 # app.listen(config.app_host, config.app_port)
102 redef var default_config_file
: String = "app.ini"
104 # Host name to bind on (will overwrite the config one).
105 var opt_host
= new OptionString("Host to bind the server on", "--host")
110 # * default: `localhost`
111 fun app_host
: String do return opt_host
.value
or else ini
["app.host"] or else "localhost"
113 # Port number to bind on (will overwrite the config one).
114 var opt_port
= new OptionInt("Port number to use", -1, "--port")
121 var opt
= opt_port
.value
122 if opt
> -1 then return opt
123 var val
= ini
["app.port"]
124 if val
!= null then return val
.to_i
128 # Displayed host name
130 # Specify this option if you need a qualified hostname to use within your handlers
131 # and `app_host` requires something else like an IP to bind.
132 # Really useful if the popcorn app runs behind a proxy.
134 # Default is `"{app_host}:{app_port}"`
135 var opt_hostname
= new OptionString("Displayed host name", "--hostname")
137 # Displayed host name config
139 # * key: `app.hostname`
140 # * default: `"{app_host}:{app_port}"`
141 fun app_hostname
: String do
142 var opt
= opt_hostname
.value
143 if opt
!= null then return opt
144 var cfg
= ini
["app.hostname"]
145 if cfg
!= null then return cfg
147 if app_port
!= 80 then res
= "{res}:{app_port}"
153 add_option
(opt_host
, opt_port
, opt_hostname
)