forked from gotthardp/lorawan-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlorawan_app.erl
More file actions
102 lines (90 loc) · 3.29 KB
/
lorawan_app.erl
File metadata and controls
102 lines (90 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
%
% Copyright (c) 2016-2019 Petr Gotthard <[email protected]>
% All rights reserved.
% Distributed under the terms of the MIT License. See the LICENSE file.
%
-module(lorawan_app).
-behaviour(application).
-export([start/0]).
-export([start/2, stop/1]).
start() ->
{ok, _Started} = application:ensure_all_started(lorawan_server).
start(_Type, _Args) ->
ok = ensure_erlang_version(21),
lager:debug("Using config: ~p", [application:get_all_env(lorawan_server)]),
lorawan_db:ensure_tables(),
case {application:get_env(lorawan_server, http_admin_listen, []), retrieve_valid_ssl()} of
{[], []} ->
lager:warning("Web-admin does not listen on any port"),
ok;
{HttpOpts, []} ->
start_http(HttpOpts, normal_dispatch());
{[], SslOpts} ->
start_https(SslOpts, normal_dispatch());
{HttpOpts, SslOpts} ->
start_https(SslOpts, normal_dispatch()),
start_http(HttpOpts,
case application:get_env(lorawan_server, http_admin_redirect_ssl, true) of
false ->
normal_dispatch();
true ->
redirect_dispatch()
end)
end,
lorawan_sup:start_link().
retrieve_valid_ssl() ->
case application:get_env(lorawan_server, http_admin_listen_ssl, []) of
[] ->
[];
Config ->
case file_configured(certfile, Config) and file_configured(keyfile, Config) of
false ->
lager:warning("http_admin_listen_ssl not configured"),
[];
true ->
Config
end
end.
file_configured(Name, Config) ->
case proplists:get_value(Name, Config) of
undefined ->
false;
File ->
case file:read_file_info(File) of
{ok, _} ->
true;
{error, _} ->
false
end
end.
stop(_State) ->
cowboy:stop_listener(http),
cowboy:stop_listener(https),
ok.
ensure_erlang_version(Min) ->
case list_to_integer(erlang:system_info(otp_release)) of
Num when Num >= Min -> ok;
_Else -> {error, prerequisite_failed}
end.
normal_dispatch() ->
cowboy_router:compile([
{'_', lorawan_http_registry:get_static(routes)++lorawan_http_registry:get_custom(routes)}
]).
redirect_dispatch() ->
Port = ranch:get_port(https),
lager:info("Redirecting to HTTPS port ~B", [Port]),
cowboy_router:compile([{'_',
[{'_', lorawan_admin_redirect, #{scheme => <<"https">>, port => Port}}]}]).
start_http(Opts, Dispatch) ->
{ok, _} = cowboy:start_clear(http, Opts, #{
env => #{dispatch => Dispatch},
metrics_callback => fun prometheus_cowboy2_instrumenter:observe/1,
stream_handlers => [lorawan_admin_logger, cowboy_compress_h,
cowboy_metrics_h, cowboy_stream_h]}).
start_https(Opts, Dispatch) ->
{ok, _} = cowboy:start_tls(https, Opts, #{
env => #{dispatch => Dispatch},
metrics_callback => fun prometheus_cowboy2_instrumenter:observe/1,
stream_handlers => [lorawan_admin_logger, cowboy_compress_h,
cowboy_metrics_h, cowboy_stream_h]}).
% end of file