-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver
executable file
·181 lines (151 loc) · 3.76 KB
/
server
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
#!/bin/bash
# default hostname
HOST=localhost
# default port number
PORT=8080
# router PHP script (optional)
ROUTER=
# script name
NAME=${0##*/}
usage () {
cat <<EOF
$NAME (PHP built-in web server manager) Version 0.3.0
PHP builtin server manager on port $PORT
usage: ./$NAME <command> [<hostname>:<port> [<router>]]
Available commands:
start Starts PHP built-in web server server on specified hostname:port, default is localhost:$PORT
router is a PHP script to which all requests will be redirected (if used)
stop Stops the PHP built-in web server
restart Stops and Starts on previously specified hostname:port
status Status of "$NAME" process
log Show the PHP built-in web server logs. Use the -f option for a live update
report bugs to [email protected]
$NAME homepage: <https://github.com/cubny/php-built-in-server-manager>
EOF
return 0
}
setup_colors() {
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
NORMAL=""
fi
}
# if no command specified exit and show usage
if [[ $# < 1 ]]; then
echo $NAME: no command specified
usage
exit 1
fi
# if hostname:port specified override defaults
if [[ $# > 1 ]]; then
IFS=':' read -r -a hostport <<< "$2"
if [[ ! -z "${hostport[0]}" ]]; then
HOST=${hostport[0]}
fi
if [[ ! -z "${hostport[1]}" ]]; then
PORT=${hostport[1]}
fi
fi
# if router specified
if [[ $# > 2 ]]; then
read -r router <<< "$3"
if [[ ! -z "${router}" ]]; then
ROUTER=${router}
fi
fi
# pidfile contents would be hostname:port:pid
PIDFILE="$NAME".pid
LOGFILE="$NAME".log
validate_server () {
which php &> /dev/null
if [[ $? -eq 1 ]]; then
printf "${YELLOW}Error: PHP not found. ${NORMAL}Please install PHP version 5.4 or greater!\n"
return 1
fi
php -h | grep -q -- '-S'
if [[ $? -eq 1 ]]; then
printf "${YELLOW}Error: PHP version must be 5.4 or greater!${NORMAL}\n"
return 1
fi
return 0
}
start_server () {
validate_server
if [[ $? -eq 1 ]]; then
return 1
fi
if [[ -e "$PIDFILE" ]]; then
printf "${YELLOW}Server seems to be running!${NORMAL}\n"
echo
echo if not, there is probably a zombie "$PIDFILE" in this directory.
echo if you are sure no server is running just remove "$PIDFILE" manually and start again
return 1
else
printf "${GREEN}"$NAME" started on $HOST:$PORT${NORMAL}\n"
php -S "$HOST":"$PORT" $ROUTER >> "$LOGFILE" 2>&1 &
echo "$HOST":"$PORT":$! > $PIDFILE
return 0
fi
}
read_pidfile() {
if [[ -e "$PIDFILE" ]]; then
PIDFILECONTENT=`cat "$PIDFILE"`
IFS=: read HOST PORT PID <<< "$PIDFILECONTENT:"
return 0
else
return 1
fi
}
stop_server () {
if read_pidfile; then
kill -9 "$PID"
rm -f "$PIDFILE"
printf "${GREEN}"$NAME" stopped!${NORMAL}\n"
return 0
else
printf "${YELLOW}"$NAME" is not running!${NORMAL}\n"
return 1
fi
}
status_server() {
if read_pidfile && kill -0 "$PID" ; then
printf "${BLUE}"$NAME" is running on ${HOST}:${PORT}${NORMAL}\n"
else
printf "${YELLOW}"$NAME" is not running!${NORMAL}\n"
fi
}
log_server() {
if read_pidfile && kill -0 "$PID" ; then
if [[ "$1" = "-f" ]]; then
TAIL_OPTS="-f"
fi
tail $TAIL_OPTS "$LOGFILE"
else
printf "${YELLOW}"$NAME" is not running!${NORMAL}\n"
fi
}
setup_colors
case $1 in
start) start_server;;
stop) stop_server;;
restart) stop_server; start_server ;;
status) status_server;;
log) log_server $2;;
-h) usage ;;
--help) usage ;;
*) usage;;
esac