-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
83 lines (75 loc) · 2.14 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
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"flag"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/gin-gonic/gin"
"github.com/op/go-logging"
)
func main() {
addr := flag.String("addr", "0.0.0.0:7000", "Server listening addr")
level := flag.String("level", "INFO", "Server log level")
isHttps := flag.Bool("https", false, "Start the HTTPS")
certFile := flag.String("cert", "cert.pem", "TLS cert file")
keyFile := flag.String("key", "key.pem", "TLS key file")
flag.Parse()
logLevel, err := logging.LogLevel(strings.ToUpper(*level))
if err != nil {
panic(err.Error())
}
leveledBackend.SetLevel(logLevel, log.Module)
log.SetBackend(leveledBackend)
gin.Logger()
gin.ForceConsoleColor()
router := gin.Default()
{
router.GET("/health", HealthHandler)
router.GET("/hello", HelloHandler)
router.GET("/info", InfoHandler)
router.GET("/", WebSocketHandler)
router.POST("/subscribe", SubscribeHandler)
}
srv := &http.Server{Addr: *addr, Handler: router}
go func() {
var err error
if *isHttps {
log.Info("Server Listen On:", "https://"+srv.Addr)
err = srv.ListenAndServeTLS(GetFileName(*certFile), GetFileName(*keyFile))
} else {
log.Info("Server Listen On:", "http://"+srv.Addr)
err = srv.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Info("Shutdown Server ...")
if err := srv.Shutdown(context.Background()); err != nil {
log.Fatalf("Server shutdown error: %s\n", err)
} else {
log.Info("Server exiting.")
}
}
func GetFileName(name string) string {
if _, err := os.Stat(name); err == nil {
return name
}
name = filepath.Join(os.Getenv("GOPATH"),
"/src/github.com/zhcppy/go-walletconnect-bridge", name)
if _, err := os.Stat(name); err == nil {
return name
}
panic("No found file: " + name)
}