1883b52f3850638db24c6c690bdc1a31c2f7dccd
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
30 # var opts = new AppOptions.from_args(args)
32 # # Build config from options
33 # var config = new AppConfig.from_options(opts)
37 # app.listen(config.app_host, config.app_port)
40 # For more advanced uses, `AppConfig` and `AppOptions` can be specialized to
41 # offer additional config options:
45 # import popcorn::pop_config
50 # # My secret code I don't want to share in my source repository
51 # var secret: String = value_or_default("secret", "my-secret")
53 # redef init from_options(options) do
55 # if options isa MyOptions then
56 # var secret = options.opt_secret.value
57 # if secret != null then self["secret"] = secret
65 # var opt_secret = new OptionString("My secret string", "--secret")
69 # add_option opt_secret
76 # # Config to use to access `secret`
77 # var config: MyConfig
79 # redef fun get(req, res) do
80 # res.send config.secret
84 # var opts = new MyOptions.from_args(args)
85 # var config = new MyConfig.from_options(opts)
88 # app.use("/secret", new SecretHandler(config))
89 # app.listen(config.app_host, config.app_port)
96 # Configuration file for Popcorn apps
100 # import popcorn::pop_config
102 # # Build config from default values
103 # var config = new AppConfig("app.ini")
105 # # Change config values
106 # config["app.port"] = 3001.to_s
110 # app.listen(config.app_host, config.app_port)
115 # Kind of options used by this config
116 type OPTIONS: AppOptions
118 # Default configuration file path
119 var default_config_file
: String = "app.ini"
124 # * default: `localhost`
125 var app_host
: String is lazy
do return value_or_default
("app.host", "localhost")
131 var app_port
: Int is lazy
do return value_or_default
("app.port", "3000").to_i
133 # Init `self` from a `AppOptions` option values
134 init from_options
(opts
: OPTIONS) do
135 init(opts
.opt_config
.value
or else default_config_file
)
136 var opt_host
= opts
.opt_host
.value
137 if opt_host
!= null then self["app.host"] = opt_host
138 var opt_port
= opts
.opt_port
.value
139 if opt_port
> 0 then self["app.port"] = opt_port
.to_s
142 # Return the registered value for `key` or `default`
143 protected fun value_or_default
(key
: String, default
: String): String do
144 return self[key
] or else default
148 # Options configuration for Popcorn apps
150 # Use the `AppOptions` class in your app to parse command line args:
153 # import popcorn::pop_config
155 # # Parse app options
156 # var opts = new AppOptions.from_args(args)
158 # # Build config from options
159 # var config = new AppConfig.from_options(opts)
163 # app.listen(config.app_host, config.app_port)
169 var opt_help
= new OptionBool("Show this help message", "-h", "--help")
171 # Path to app config file.
172 var opt_config
= new OptionString("Path to app config file", "--config")
174 # Host name to bind on (will overwrite the config one).
175 var opt_host
= new OptionString("Host to bind the server on", "--host")
177 # Port number to bind on (will overwrite the config one).
178 var opt_port
= new OptionInt("Port number to use", -1, "--port")
180 # You should redefined this method to add your options
183 add_option
(opt_help
, opt_config
, opt_host
, opt_port
)
186 # Initialize `self` and parse `args`
187 init from_args
(args
: Collection[String]) do