/
main.go
59 lines (51 loc) · 1.17 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
package main
import (
"fmt"
"net/http"
"os"
"time"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
var zapLogger *zap.Logger = newLogger()
func Getenv(env string, defaultEnv string) string {
e := os.Getenv(env)
if e == "" {
e = defaultEnv
}
return e
}
func serve() {
router := gin.New()
// Add a ginzap middleware
router.Use(ginzap.GinzapWithConfig(zapLogger, &ginzap.Config{
TimeFormat: time.RFC3339,
UTC: false,
SkipPaths: []string{"/ping", "/node"},
}))
// Logs all panic to error log
// - stack means whether output the stack info.
router.Use(ginzap.RecoveryWithZap(zapLogger, true))
// Wait 1 second for headless DNS sync up
time.Sleep(1*time.Second)
node, err := node()
if err != nil {
panic(fmt.Sprintf("Fail to init node, err: %s", err.Error()))
}
dbclient := DynamoClient()
if dbclient == nil {
panic(fmt.Sprintf("Fail to init db client"))
}
root := router.Group("/")
root.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, "pong")
})
root.GET("/node", getID(node))
root.POST("/newurl", setURL(dbclient, node))
root.GET("/:regex", proxy(dbclient))
router.Run()
}
func main() {
serve()
}