-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (67 loc) · 2.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"flag"
"log"
"net/http"
"os"
"time"
"github.com/deadlock-delegate/payfee-api/config"
"github.com/deadlock-delegate/payfee-api/handlers"
"github.com/deadlock-delegate/payfee-api/server"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/gin-gonic/gin"
)
var allowOriging string
func main() {
flag.StringVar(&config.Webhook.Token, "token", "", "Webhook token")
flag.StringVar(&config.Webhook.VerificationKey, "verification", "", "Webhook verification key")
flag.StringVar(&allowOriging, "allowOriging", "http://localhost:8080", "Access control allow origin")
flag.Parse()
db, err := gorm.Open("postgres", "host=localhost port=5432 user=postgres dbname=postgres password=postgres sslmode=disable")
if err != nil {
log.Println("Failed to connect to postgres:", err)
os.Exit(1)
} else {
log.Println("Connected to postgres")
}
defer db.Close()
r := gin.Default()
r.Use(func(c *gin.Context) {
c.Set("db", db)
c.Next()
})
r.Use(func(c *gin.Context) {
// todo: cleanup these values
c.Writer.Header().Set("Access-Control-Allow-Origin", allowOriging)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Content-Length, X-CSRF-Token, Token, session, Origin, Host, Connection, Accept-Encoding, Accept-Language, X-Requested-With")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Request.Header.Del("Origin")
c.Next()
})
r.Use(gin.Recovery())
socket, err := server.InitSocket()
if err != nil {
log.Fatal(err)
}
r.GET("/ping", handlers.Ping)
r.GET("/list", handlers.ListLinks)
r.GET("/link/:linkID", handlers.GetLink)
r.POST("/create", handlers.CreateLink)
r.POST("/events", handlers.WebhookEvents)
r.GET("/socket.io/*any", gin.WrapH(socket))
r.POST("/socket.io/*any", gin.WrapH(socket))
s := &http.Server{
Addr: ":3000",
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}