-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS55_playground
52 lines (50 loc) · 1.41 KB
/
S55_playground
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
mknginxconf() {
tempdir="$1"
port="$2"
cat << EOF > "$tempdir/.nginx.conf"
pid "$tempdir/.nginx.pid";
events { worker_connections 1024; }
http {
default_type application/octet-stream;
server {
access_log "$tempdir/nginx.log";
listen "$port" default;
location / {
root "$tempdir";
}
}
types { text/html html htm shtml; text/css css; text/xml xml; application/x-javascript js; image/jpeg jpeg jpg;
image/png png; application/x-shockwave-flash swf; video/mp4 mp4; }
}
EOF
}
freeport() {
while true; do
port=$(($RANDOM+1024))
if nc -z localhost $port > /dev/null; then continue; fi
echo $port
break
done
}
playground() {
tempdir=$(mktemp -d /tmp/playground.XXXXXX)
if [ $? -ne 0 ]; then echo "$0: can't create temporary directory"; return 1; fi
cd "$tempdir"
cp ~/.zsh.d/static/playground/* .
port="$(freeport)"
mknginxconf "$tempdir" "$port"
alias nginx='nginx -c "$tempdir"/.nginx.conf'
nginx -c "$tempdir"/.nginx.conf
if ! which open > /dev/null; then
echo "direct your browser at http://localhost:$port and hit [enter]"
read
else
open -g "http://localhost:$port"
fi
vim "$tempdir"/index.html
nginx -c "$tempdir"/.nginx.conf -s stop
cd -
echo "Hit [enter] to destroy your playground ($tempdir)"
read
rm -fr "$tempdir"
}