-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (60 loc) · 1.27 KB
/
main.go
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
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
func isPrime(value int) bool {
for i := 2; i <= value/2; i++ {
if value%i == 0 {
return false
}
}
return value > 1
}
func main() {
m := martini.Classic()
m.Use(render.Renderer(
render.Options{
Directory: "templates",
},
))
m.Get("/", func(r render.Render, req *http.Request) {
if req.URL.Query().Get("wait") != "" {
sleep, _ := strconv.Atoi(req.URL.Query().Get("wait"))
log.Printf("Sleep for %d seconds\n", sleep)
time.Sleep(time.Duration(sleep) * time.Second)
}
if req.URL.Query().Get("prime") != "" {
val, _ := strconv.Atoi(req.URL.Query().Get("prime"))
log.Printf("Is %d prime: %t", val, isPrime(val))
}
r.HTML(200, "index", nil)
})
if os.Getenv("PANIC") == "true" {
panic("this is crashing")
}
port := "3000"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
listener, err := net.Listen("tcp", ":"+port)
if err != nil {
panic(err)
}
go http.Serve(listener, m)
log.Println("Listening on 0.0.0.0:" + port)
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGTERM)
<-sigs
fmt.Println("SIGTERM, time to shutdown")
listener.Close()
}