-
Notifications
You must be signed in to change notification settings - Fork 2
/
startup.sh
42 lines (34 loc) · 909 Bytes
/
startup.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
#!/bin/bash
# Install golang and create project directory
sudo bash -c 'apt-get update && \
apt-get install -yq golang && \
mkdir -p /projects/webapp'
# Create go app file with content and create webapp systemd service file
sudo bash -c 'cat > /projects/webapp/main.go <<EOF
package main
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Google Cloud !!!")
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":80", nil))
}
EOF
cat > /etc/systemd/system/webapp.service <<EOF
[Unit]
Description=Start the webapp
[Service]
Restart=on-failure
WorkingDirectory=/projects/webapp
ExecStart=/usr/bin/go run main.go
[Install]
WantedBy=multi-user.target
EOF'
sudo bash -c 'chmod +x /projects/webapp/main.go && \
systemctl start webapp.service && \
systemctl enable webapp.service'