-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
101 lines (83 loc) · 2.16 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"math/rand"
"strings"
"time"
"io/ioutil"
"github.com/gin-gonic/gin"
"github.com/swayops/closer"
"github.com/swayops/sway/config"
"github.com/swayops/sway/server"
)
func main() {
rand.Seed(time.Now().UnixNano())
log.SetFlags(log.Lshortfile)
cfg, err := config.New("config/config.json")
if err != nil {
log.Fatal(err)
}
if !cfg.Sandbox {
gin.SetMode(gin.ReleaseMode)
}
log.Println("Stripe:", cfg.Stripe.Key)
log.Println("Lob:", cfg.Lob.Key, cfg.Lob.Addr, cfg.Lob.BankAcct)
r := gin.New()
r.Use(gin.Recovery())
r.Use(ginLogger(cfg.Sandbox, "/static", "/favicon.ico", "/api/v1/getIncompleteInfluencers"))
// Ping test
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
srv, err := server.New(cfg, r)
if err != nil {
log.Fatal(err)
}
defer closer.Defer(srv.Close)()
// Listen and Serve
if err = srv.Run(); err != nil {
// using panic rather than fatal because fatal would terminal the program
// and it would never call our closer
log.Panicf("Failed to listen: %v", err)
}
}
func ginLogger(sandbox bool, prefixesToSkip ...string) gin.HandlerFunc {
// shamelessly copied from gin.Logger
return func(c *gin.Context) {
path := c.Request.URL.Path
for _, pre := range prefixesToSkip {
if strings.HasPrefix(path, pre) {
return
}
}
start := time.Now()
if sandbox {
switch m := c.Request.Method; m {
case "POST", "PUT", "DELETE":
var buf bytes.Buffer
io.Copy(&buf, c.Request.Body)
c.Request.Body.Close()
c.Request.Body = ioutil.NopCloser(&buf)
j, _ := json.Marshal(c.Request.Header)
if ln := buf.Len(); ln > 0 {
switch buf.Bytes()[0] {
case '[', '{', 'n': // [], {} and nullable
log.Printf("%s: %s\n\tHeaders: %s\n\tRequest (%d): %s", m, path, j, ln, buf.String())
default:
log.Printf("%s: %s\n\t\n\tHeaders: %s\n\tRequest (%d): <binary>", m, path, j, ln)
}
}
}
}
c.Next()
end := time.Now()
latency := end.Sub(start)
clientIP := c.ClientIP()
method := c.Request.Method
statusCode := c.Writer.Status()
log.Printf("[%s] [%d] %s %s [%s]", clientIP, statusCode, method, path, latency)
}
}