-
Notifications
You must be signed in to change notification settings - Fork 81
/
grpcox.go
78 lines (65 loc) · 1.64 KB
/
grpcox.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
71
72
73
74
75
76
77
78
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gorilla/mux"
"github.com/gusaul/grpcox/core"
"github.com/gusaul/grpcox/handler"
)
func main() {
// logging conf
f, err := os.OpenFile("log/grpcox.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.SetFlags(log.LstdFlags | log.Lshortfile)
// start app
addr := "0.0.0.0:6969"
if value, ok := os.LookupEnv("BIND_ADDR"); ok {
addr = value
}
muxRouter := mux.NewRouter()
handler.Init(muxRouter)
var wait time.Duration = time.Second * 15
srv := &http.Server{
Addr: addr,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: muxRouter,
}
fmt.Println("Service started on", addr)
go func() {
log.Fatal(srv.ListenAndServe())
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
err = removeProtos()
if err != nil {
log.Printf("error while removing protos: %s", err.Error())
}
srv.Shutdown(ctx)
log.Println("shutting down")
os.Exit(0)
}
// removeProtos will remove all uploaded proto file
// this function will be called as the server shutdown gracefully
func removeProtos() error {
log.Println("removing proto dir from /tmp")
err := os.RemoveAll(core.BasePath)
return err
}