-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_devel_backend.sh
135 lines (114 loc) · 2.88 KB
/
_devel_backend.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
#!/bin/sh
# * $1 == 'mongo*' launch mongo shell with current config DB name
# * any param "$1" will redirect node.js' output i.e:
# 1>>log/app_back_stdout.txt 2>>log/app_back_stderr.txt
# * if env $NODEJS_CONFIG is exported it is used and no hardcoded file is read
cd "${0%/*}" 2>/dev/null
set -e
# though git for windows is preferred a bundle of cygwin executables can be here
PATH=.:bin:$PATH
trap 'echo "
Unexpected Script Error! Use /bin/sh -x $0 to trace it.
"
set +e
trap "" 0
exit 0
' 0
normal_exit(){
echo "
Normal Exit${1:- (backend is running)}
"
set +e
trap '' 0
exit 0
}
trap 'normal_exit' HUP TERM INT
if [ "$NODEJS_CONFIG" ]
then
echo '
^ config is already exported in "$NODEJS_CONFIG"'
else
echo '
^ reading config in "$NODEJS_CONFIG" from file "./config/cfg_default.js"'
NODEJS_CONFIG=`sed '' <./config/cfg_default.js`
echo '^ exporting it for childs'
export NODEJS_CONFIG
fi
case "$1" in
mongo*)
sh app_modules/supromongod/etc/mongo-shell.sh
trap '' 0
exit 0
;;
esac
# JS config sample to parse:
# backend:{
# file: './app_main/app_back.js',
# job_port: 3007,// app/api
# here>> ctl_port: 3008,// controlling
# ctl_on_done: null,// set app module handlers for ctl close/app exit
# init_timeout: 123
# }
BACKEND_PORT=${NODEJS_CONFIG##*ctl_port:}
BACKEND_PORT=${BACKEND_PORT## }
BACKEND_PORT=${BACKEND_PORT%%,*}
A=${NODEJS_CONFIG##*backend:{}
A=${A##*file:[ \'\"][\'\"]}
A=${A%%[\'\"],*} # app_main/app_back.js
BACKEND="node $A"
A=${A##*/}
A=${A%%.js*} # app_back
echo '
^ running Node.JS backend
^ command: `'"$BACKEND"'`
^ ctlport: "'"$BACKEND_PORT"'"
'
# lftp way of http access (obsolete): `_lftp_http 1 'cmd_exit'`
_lftp_http() { # $1=timeout $2=cmd
{ # http head request with contentlength=0 reply
echo "[lftp->nodeJS:$JSAPPCTLPORT] sending '$2'"
lftp -c '
set cmd:long-running 2
set net:idle 2
set net:timeout 2
set net:max-retries 2
set net:reconnect-interval-base '"$1"'
set net:reconnect-interval-multiplier 1
cd http://127.0.0.1:'"$BACKEND_PORT"'/ && cat '"$2"' && exit 0 || exit 1
'
} 0</dev/null
return $?
}
_http() { # $1=cmd $2=timeout
node './_http.js' "http://127.0.0.1:$BACKEND_PORT/$1" "$2"
}
[ "$1" ] && {
echo 'Logging in "./log/"'
[ -d './log/' ] || {
echo 'Creating "./log/"'
mkdir log
}
exec 7>>log/${A}_stdout.txt 8>>log/${A}_stderr.txt
} || {
exec 7>&1 8>&2
}
$BACKEND 1>&7 2>&8 &
while echo '
Press "Enter" key to reload, "CTRL+D" stop backend || "CTRL+C" to break...
NOTE: config is not reloaded (stop + start required)!
'
do
read A || {
echo '
Stop backend (y/n)? '
read A && {
[ 'y' = "$A" ] && {
_http 'cmd_exit'
A='.'
normal_exit "$A"
}
} || normal_exit
}
_http 'cmd_exit' || :
$BACKEND 1>&7 2>&8 &
done