-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
docker-entrypoint.sh
executable file
·208 lines (166 loc) · 5.17 KB
/
docker-entrypoint.sh
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
command_args=""
# ======= Helper Functions =======
get_boolean_str() {
if [ "$1" = "true" ]; then
echo "true"
else
echo "false"
fi
}
is_in_cmd_with_value() {
CMD_ARG=$1
for arg in "${@:1}"
do
if [[ "$arg" == "$CMD_ARG"* ]] && [ -n "${arg#*=}" ]; then
echo "true"
return
fi
done
echo "false"
}
set_value() {
VAR_NAME=$1
CMD_ARG=$2
DEFAULT_VALUE=$3
# if command line argument is preset, use it
if [ "$(is_in_cmd_with_value "$CMD_ARG" ${@:3})" = "true" ]; then
command_args+=" $CMD_ARG$(get_cmd_value "$CMD_ARG" ${@:3})"
return
fi
# if environment variable is preset, use it
if [ -n "${!VAR_NAME}" ]; then
command_args+=" $CMD_ARG${!VAR_NAME}"
return
fi
# if default value is not '@@NULL@@', use it
if [ "$DEFAULT_VALUE" != "@@NULL@@" ]; then
command_args+=" $CMD_ARG$DEFAULT_VALUE"
fi
}
is_in_cmd() {
CMD_ARG=$1
for arg in "${@:2}"
do
if [[ "$arg" == "$CMD_ARG" ]]; then
echo "true"
return
fi
done
echo "false"
}
set_switch() {
VAR_NAME=$1
CMD_ARG=$2
DEFAULT_VALUE=$3
# if command line argument is preset, use it
if [ "$(is_in_cmd "$CMD_ARG" ${@:3})" = "true" ]; then
command_args+=" $CMD_ARG"
return
fi
# if environment variable is preset, use it
if [ -n "${!VAR_NAME}" ]; then
if [ "${!VAR_NAME}" = "true" ]; then
command_args+=" $CMD_ARG"
fi
return
fi
# use default value
if [ "$DEFAULT_VALUE" = "true" ]; then
command_args+=" $CMD_ARG"
fi
}
get_cmd_value() {
CMD_ARG=$1
for arg in "${@:2}"
do
if [[ "$arg" == "$CMD_ARG"* ]]; then
echo "${arg#*=}"
return
fi
done
echo ""
}
get_mongodb_configuration() {
CMD=$@
CONNECTION_STRING="mongodb://"
if [ "$(is_in_cmd_with_value "--db_connection_string=" $CMD)" = "true" ]; then
CONNECTION_STRING=$(get_cmd_value "--db_connection_string=" $CMD)
else
if [ "$(is_in_cmd_with_value "--db_user=" $CMD)" = "true" ]; then
CONNECTION_STRING+="$(get_cmd_value "--db_user=" $CMD):************@"
fi
CONNECTION_STRING+="$(get_cmd_value "--db_host=" $CMD):$(get_cmd_value "--db_port=" $CMD)/$(get_cmd_value "--collection_name=" $CMD)"
if [ "$(is_in_cmd_with_value "--db_options=" $CMD)" = "true" ]; then
CONNECTION_STRING+="?$(get_cmd_value "--db_options=" $CMD)"
fi
fi
echo $CONNECTION_STRING
}
# ================================
# ======= Environment Variables =======
declare -A valueMap=(
[PORT]="value,--port=,2333"
[DEMO]="switch,--demo,false"
[ALLOWED_ORIGINS]="value,--allowed_origins=,localhost"
[CONFIG_PATH]="value,--config_path=,@@NULL@@"
# DB
[DB_COLLECTION_NAME]="value,--collection_name=,mx-space"
[DB_HOST]="value,--db_host=,127.0.0.1"
[DB_PORT]="value,--db_port=,27017"
[DB_USER]="value,--db_user=,@@NULL@@"
[DB_PASSWORD]="value,--db_password=,@@NULL@@"
[DB_OPTIONS]="value,--db_options=,@@NULL@@"
[DB_CONNECTION_STRING]="value,--db_connection_string=,@@NULL@@"
# Redis
[REDIS_HOST]="value,--redis_host=,127.0.0.1"
[REDIS_PORT]="value,--redis_port=,6379"
[REDIS_PASSWORD]="value,--redis_password=,@@NULL@@"
[DISABLE_CACHE]="switch,--disable_cache,false"
# JWT
[JWT_SECRET]="value,--jwt_secret=,@@NULL@@"
[JWT_EXPIRE]="value,--jwt_expire=,@@NULL@@"
# Cluster
[CLUSTER]="switch,--cluster,false"
[CLUSTER_WORKERS]="value,--cluster_workers=,@@NULL@@"
# Debug
[DEBUG]="switch,--http_request_verbose,false"
[DEBUG_MEMORY_DUMP]="switch,--debug_memory_dump,false"
# Cache
[CACHE_TTL]="value,--http_cache_ttl=,@@NULL@@"
[CACHE_CDN_HEADER]="switch,--http_cache_enable_cdn_header=,false"
[CACHE_FORCE_HEADER]="switch,--http_cache_enable_force_cache_header,false"
# Security
[ENCRYPT_ENABLE]="switch,--encrypt_enable,false"
[ENCRYPT_KEY]="value,--encrypt_key=,@@NULL@@"
[ENCRYPT_ALGORITHM]="value,--encrypt_algorithm=,@@NULL@@"
# Throttle
[THROTTLE_TTL]="value,--throttle_ttl=,@@NULL@@"
[THROTTLE_LIMIT]="value,--throttle_limit=,@@NULL@@"
# Color
[COLOR]="switch,--color,true"
)
for key in "${!valueMap[@]}"; do
IFS=',' read -ra tuple <<< "${valueMap[$key]}"
TYPE=${tuple[0]}
CMD_ARG=${tuple[1]}
DEFAULT=${tuple[2]}
if [ "$TYPE" = "value" ]; then
set_value $key $CMD_ARG $DEFAULT $@
elif [ "$TYPE" = "switch" ]; then
set_switch $key $CMD_ARG $DEFAULT $@
fi
done
# ====================================
echo "Starting Mix Space"
echo "============== Configurations =============="
echo "Listen Port: $(get_cmd_value "--port=" $command_args)"
echo "MongoDB: $(get_mongodb_configuration $command_args)"
echo "Redis: $(get_cmd_value "--redis_host=" $command_args):$(get_cmd_value "--redis_port=" $command_args)"
echo "Allowed Origins: $(get_cmd_value "--allowed_origins=" $command_args)"
echo "Config Path: $(if [ -z "$(get_cmd_value "--config_path=" $command_args)" ]; then echo "NULL"; else echo "$(get_cmd_value "--config_path=" $command_args)"; fi)"
echo "Encryption: $(get_boolean_str $(is_in_cmd "--encrypt_enable" $command_args))"
echo "Cluster: $(get_boolean_str $(is_in_cmd "--cluster" $command_args))"
echo "============================================"
command="node index.js $command_args"
exec $command