doc: Commands tests use `test_frontend`
[nit.git] / lib / popcorn / pop_config.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Configuration file and options for Popcorn apps
18 #
19 # `pop_config` provide a configuration framework for Popcorn apps based on ini
20 # files.
21 #
22 # By default `AppConfig` provides `app.host` and `app.port` keys, it's all we
23 # need to start an app:
24 #
25 # ~~~
26 # import popcorn
27 # import popcorn::pop_config
28 #
29 # # Build config from options
30 # var config = new AppConfig
31 # config.parse_options(args)
32 #
33 # # Use options
34 # var app = new App
35 # app.listen(config.app_host, config.app_port)
36 # ~~~
37 #
38 # For more advanced uses, `AppConfig` and `AppOptions` can be specialized to
39 # offer additional config options:
40 #
41 # ~~~
42 # import popcorn
43 # import popcorn::pop_config
44 #
45 # class MyConfig
46 # super AppConfig
47 #
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"
50 #
51 # # opt --secret
52 # var opt_secret = new OptionString("My secret string", "--secret")
53 #
54 # redef init do
55 # super
56 # add_option opt_secret
57 # end
58 # end
59 #
60 # class SecretHandler
61 # super Handler
62 #
63 # # Config to use to access `secret`
64 # var config: MyConfig
65 #
66 # redef fun get(req, res) do
67 # res.send config.secret
68 # end
69 # end
70 #
71 # var config = new MyConfig
72 # config.parse_options(args)
73 #
74 # var app = new App
75 # app.use("/secret", new SecretHandler(config))
76 # app.listen(config.app_host, config.app_port)
77 # ~~~
78 module pop_config
79
80 import config
81
82 # Configuration file for Popcorn apps
83 #
84 # ~~~
85 # import popcorn
86 # import popcorn::pop_config
87 #
88 # # Build config from default values
89 # var config = new AppConfig
90 # config.parse_options(args)
91 #
92 # # Change config values
93 # config.ini["app.port"] = 3001.to_s
94 #
95 # # Use options
96 # var app = new App
97 # app.listen(config.app_host, config.app_port)
98 # ~~~
99 class AppConfig
100 super IniConfig
101
102 redef var default_config_file: String = "app.ini"
103
104 # Host name to bind on (will overwrite the config one).
105 var opt_host = new OptionString("Host to bind the server on", "--host")
106
107 # Web app host name
108 #
109 # * key: `app.host`
110 # * default: `localhost`
111 fun app_host: String do return opt_host.value or else ini["app.host"] or else "localhost"
112
113 # Port number to bind on (will overwrite the config one).
114 var opt_port = new OptionInt("Port number to use", -1, "--port")
115
116 # Web app port
117 #
118 # * key: `app.port`
119 # * default: `3000`
120 fun app_port: Int do
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
125 return 3000
126 end
127
128 # Displayed host name
129 #
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.
133 #
134 # Default is `"{app_host}:{app_port}"`
135 var opt_hostname = new OptionString("Displayed host name", "--hostname")
136
137 # Displayed host name config
138 #
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
146 var res = app_host
147 if app_port != 80 then res = "{res}:{app_port}"
148 return res
149 end
150
151 init do
152 super
153 add_option(opt_host, opt_port, opt_hostname)
154 end
155 end