1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
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 |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
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. |
14 |
|
|
15 |
|
|
16 |
|
-module(pgmp_dbs_sup). |
17 |
|
|
18 |
|
|
19 |
|
-behaviour(supervisor). |
20 |
|
-export([default/1]). |
21 |
|
-export([init/1]). |
22 |
|
-export([start_child/1]). |
23 |
|
-export([start_link/0]). |
24 |
|
-export_type([db/0]). |
25 |
|
-import(pgmp_sup, [supervisor/1]). |
26 |
|
|
27 |
|
|
28 |
|
-type db() :: #{user := binary(), |
29 |
|
password => fun (() -> binary()), |
30 |
|
port := binary(), |
31 |
|
host := binary(), |
32 |
|
scope => atom(), |
33 |
|
group => atom(), |
34 |
|
application_name => binary(), |
35 |
|
database := binary()}. |
36 |
|
|
37 |
|
|
38 |
|
start_link() -> |
39 |
10 |
supervisor:start_link({local, ?MODULE}, ?MODULE, []). |
40 |
|
|
41 |
|
|
42 |
|
start_child(URI) when is_list(URI) -> |
43 |
:-( |
?FUNCTION_NAME(list_to_binary(URI)); |
44 |
|
|
45 |
|
start_child(URI) when is_binary(URI) -> |
46 |
:-( |
supervisor:start_child( |
47 |
|
?MODULE, |
48 |
|
child_spec(maps:merge(default(), pgmp_uri:parse(URI)))). |
49 |
|
|
50 |
|
|
51 |
|
init([]) -> |
52 |
10 |
{ok, {pgmp_config:sup_flags(?MODULE), children()}}. |
53 |
|
|
54 |
|
|
55 |
|
children() -> |
56 |
10 |
[child_spec(DB) || DB <- dbs(), pgmp_config:enabled(connect)]. |
57 |
|
|
58 |
|
|
59 |
|
child_spec(#{application_name := ApplicationName} = DB) -> |
60 |
10 |
supervisor( |
61 |
|
#{id => ApplicationName, |
62 |
|
m => pgmp_db_sup, |
63 |
|
args => [DB]}). |
64 |
|
|
65 |
|
|
66 |
|
-spec dbs() -> [db()]. |
67 |
|
|
68 |
|
dbs() -> |
69 |
10 |
try |
70 |
10 |
lists:map( |
71 |
|
fun |
72 |
|
(URI) -> |
73 |
:-( |
maps:merge(default(), pgmp_uri:parse(URI)) |
74 |
|
end, |
75 |
|
string:split( |
76 |
|
pgmp_config:database(uri), |
77 |
|
pgmp_config:database(uri_separator), |
78 |
|
all)) |
79 |
|
|
80 |
|
catch |
81 |
|
error:badarg -> |
82 |
10 |
[default()] |
83 |
|
end. |
84 |
|
|
85 |
|
|
86 |
|
default() -> |
87 |
10 |
lists:foldl( |
88 |
|
fun |
89 |
|
(Parameter, A) -> |
90 |
60 |
A#{Parameter => default(Parameter)} |
91 |
|
end, |
92 |
|
#{}, |
93 |
|
[user, |
94 |
|
application_name, |
95 |
|
password, |
96 |
|
port, |
97 |
|
host, |
98 |
|
database]). |
99 |
|
|
100 |
|
|
101 |
|
default(password = Parameter) -> |
102 |
10 |
fun |
103 |
|
() -> |
104 |
66 |
pgmp_config:database(Parameter) |
105 |
|
end; |
106 |
|
|
107 |
|
default(host) -> |
108 |
10 |
pgmp_config:database(hostname); |
109 |
|
|
110 |
|
default(database) -> |
111 |
10 |
pgmp_config:database(name); |
112 |
|
|
113 |
|
default(Parameter) -> |
114 |
12292 |
pgmp_config:database(Parameter). |