-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.sh
executable file
·203 lines (191 loc) · 5.88 KB
/
run.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
#!/bin/bash
BASE_DIR="$(cd "$(dirname "$0" )" && pwd )"
API_DIR=$BASE_DIR"/api"
NG_DIR=$BASE_DIR"/ng"
DEF_LOG='\033[0m'
API_LOG='\033[0;36m'
NG_LOG='\033[0;31m'
SH_LOG='\033[0;32m'
UNIT_TEST_CONFIG_DIR=$NG_DIR/karma.single.conf.js
TIMEOUT=100
function get-ip {
echo $(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
}
function get-api-interface {
echo $(cat $API_DIR/.env | awk '/HOST/ && !/DB/' | cut -d '=' -f2):$(cat $API_DIR/.env | awk '/PORT/ && !/DB/' | cut -d '=' -f2)
}
function api {
echo Starting web backend...
cd $API_DIR
go run main.go dev
}
function ui {
echo Starting web frontend...
cd $NG_DIR
if [[ $# -eq 0 ]]; then
ng serve
elif [[ $# -eq 1 ]]; then
case $1 in
lambda)
echo "Using AWS Lambda API environment..."
ng serve --configuration=lambda
;;
prod)
echo "Using production environment..."
ng serve --configuration=production
;;
host)
IPADDR=$(get-ip)
echo "Hosting on $IPADDR..."
ng serve --host=$IPADDR
;;
prodhost)
echo "Using production environment..."
IPADDR=$(get-ip)
echo "Hosting on $IPADDR..."
ng serve --configuration=production --host=$IPADDR
;;
*)
echo "FATAL ERR: Internal script function could not recognize parameter"
exit 1
;;
esac
fi
}
function run-tests {
API_INTERFACE=$(get-api-interface)
echo "Waiting for $API_INTERFACE..."
((timer = $TIMEOUT))
while [[ $timer -ne 0 ]]; do
curl $API_INTERFACE 2> /dev/null
rc=$?
if [[ $rc -eq 0 ]]; then
((timer = 1))
fi
((timer = timer - 1))
sleep 1
done
if [[ $rc -eq 0 ]]; then
cd $NG_DIR
echo
echo "Starting end-to-end tests..."
ng e2e
rc_e2e=$?
if [[ $# -eq 1 ]] && [[ $1 == "e2e" ]]; then
echo "Skipping unit tests."
rc_units=-1
else
echo "Starting unit tests..."
ng test --karmaConfig=$UNIT_TEST_CONFIG_DIR
rc_units=$?
fi
if [[ $rc_e2e -eq 0 ]]; then
echo "All end-to-end tests passed!"
else
echo "ERR: Some end-to-end tests did not pass."
fi
if [[ $rc_units -eq 0 ]]; then
echo "All unit tests passed!"
elif [[ $rc_units -eq -1 ]]; then
echo "Unit tests skipped."
else
echo "ERR: Some unit tests did not pass."
fi
exit 0
else
echo "FATAL ERR: Timeout"
exit 1
fi
}
function log {
sed -e "s/^/`printf "${2}"`[$1] `printf "${DEF_LOG}"`/"
}
function usage {
echo "Available Options:"
echo "(no flags): Run web UI and web API locally in development mode"
echo "-a, --api-only: Run web API only in development mode"
echo "-u, --ui-only: Run web UI only in development mode"
echo "-ul, --ui-lambda: Run web UI only in development mode with connection to production API on AWS Lambda"
echo "-p, --production-mode: Run web UI in development mode with a production environment. This mode will NOT run the web API, since the web UI will make calls from the live web API"
echo "-h, --host-lan: Host web UI on local IP address"
echo "-t, --test: Run unit tests and end-to-end tests while running web API in development mode"
echo "-e, --e2e: Run end-to-end tests only while running web API in development mode"
}
function invalid {
echo "Invalid option(s) specified. Run with -o or --options to view all options."
}
if [[ $# -eq 0 ]]; then
clear
echo "Starting script..." | log pw3:sh $SH_LOG
(api | log pw3:api $API_LOG & ui | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
elif [[ $# -eq 1 ]]; then
case $1 in
-o | --options)
usage
exit 0
;;
-p | --production-mode)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
(ui prod | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
-h | --host-lan)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
(api | log pw3:api $API_LOG & ui host | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
-hp | -ph)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
(ui prodhost | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
-a | --api-only)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
(api | log pw3:api $API_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
-u | --ui-only)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
(ui | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
-ul | --ui-lambda)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
(ui lambda | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
-uh | -hu)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
(ui host | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
-t | --test)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
trap "kill 0" EXIT
(api | log pw3:api $API_LOG & run-tests | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
-e | --e2e)
clear
echo "Starting script..." | log pw3:sh $SH_LOG
trap "kill 0" EXIT
(api | log pw3:api $API_LOG & run-tests e2e | log pw3:ng $NG_LOG)
echo "Script complete." | log pw3:sh $SH_LOG
;;
*)
invalid | log pw3:sh $SH_LOG
exit 1
;;
esac
else
usage
fi