forked from oauth-io/oauthd
-
Notifications
You must be signed in to change notification settings - Fork 3
/
oauthd.sh
68 lines (65 loc) · 1.89 KB
/
oauthd.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
#! /bin/bash
myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link" | sed "s|^\([^/].*\)\$|$(dirname $1)/\1|"); }
whereis_realpath() { local SCRIPT_PATH=$1; myreadlink ${SCRIPT_PATH} | sed "s|^\([^/].*\)\$|$(dirname ${SCRIPT_PATH})/\1|"; }
cd $(dirname $(whereis_realpath ${0}))
oauthdurl=`node -e 'var c=require("./lib/config");console.log("http://127.0.0.1:"+c.port+c.base)'`
case "$1" in
start)
if curl -o /dev/null --silent --fail "$oauthdurl"; then
echo -n "Stopping OAuth daemon..."
forever stop lib/oauthd.js >/dev/null 2>/dev/null
echo " OK"
fi
echo -n "Starting OAuth daemon."
forever --minUptime 1000 --spinSleepTime 1000 -a -l forever.log -o out.log -e err.log start lib/oauthd.js >/dev/null 2>/dev/null
for i in {1..10}
do
sleep 1
if curl -o /dev/null --silent --fail "$oauthdurl"; then
echo " OK"
node -e 'var c=require("./lib/config");console.log("Admin interface at "+c.host_url+c.base+"/admin")'
exit 0
else
echo -n "."
fi
done
echo " Failed"
;;
status)
if curl -o /dev/null --silent --fail "$oauthdurl"; then
echo "OAuth daemon is running"
else
echo "OAuth daemon is stopped"
fi
;;
stop)
if curl -o /dev/null --silent --fail "$oauthdurl"; then
echo -n "Stopping OAuth daemon..."
forever stop lib/oauthd.js >/dev/null 2>/dev/null
echo " OK"
else
echo "OAuth daemon is already stopped"
fi
;;
restart)
$0 stop
$0 start
;;
startsync)
forever --minUptime 1000 --spinSleepTime 1000 -a -l forever.log -o out.log -e err.log lib/oauthd.js
;;
config)
if [ ! -f ./config.local.js ]; then
cp ./config.js ./config.local.js
fi
if [ "$EDITOR" != "" ]; then
$EDITOR config.local.js
else
echo "You can use 'EDITOR=xxx oauthd config' to set the editor to use. Defaulting to vi..."
vi config.local.js
fi
;;
*)
echo "Usage: oauthd {status|config|start|stop|restart}"
exit 1
esac