-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoreload.sh
executable file
·61 lines (51 loc) · 1.59 KB
/
autoreload.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
#!/bin/bash
# Autoreloads go application on file changes
# This is based on a script written by Ford Hurley (@fordhurley)
# Requires inotifywait, therefore should be run from within docker
# 1. Add the following to your Dockerfile
# RUN apt-get update && apt-get install -y inotify-tools
# 2. You should run this script either as your `CMD` or `ENTRYPOINT` in the Dockerfile
# For example: ENTRYPOINT ["./autoreload.sh"]
# Then you can run `docker run my_docker_image myApp`
# * where `my_docker_image` is the docker image of your app
# * myApp is the name of the static binary that should be rebuilt on file changes (you probably want this to be gitignore'd)
TARGET=${1:-"app"}
FILES_TO_WATCH=${2:-"./*.go"}
wait_for_changes() {
echo "Waiting for changes"
# FILES_TO_WATCH can be something like: ./*.go ./**/*.go ./**/**/*.go
# depending on how long the folder structure is.
# unfortunately, inotifywait can't recursively watch specific file types
inotifywait -e modify -e create -e delete -e move $FILES_TO_WATCH
}
build() {
echo "Rebuilding..."
rm -vf $TARGET
go build -v -o $TARGET
}
kill_server() {
if [[ -n $SERVER_PID ]]; then
echo
echo "Stopping server (PID: $SERVER_PID)"
kill $SERVER_PID
fi
}
serve() {
echo "Starting server"
./$TARGET &
SERVER_PID=$!
}
# Exit on ctrl-c (without this, ctrl-c would go to inotifywait, causing it to
# reload instead of exit):
trap "exit 0" SIGINT SIGTERM
trap kill_server "EXIT"
build
serve
while wait_for_changes; do
kill_server
if ! build; then
echo "Error building server"
continue
fi
serve
done