-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (46 loc) · 1.1 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
package main
import (
"github.com/RainbowDashy/we-care-you/store"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type Config struct {
dbPath string
frontendPath string
}
var config Config
func init() {
config = Config{
dbPath: "./data.db",
frontendPath: "./frontend/dist",
}
}
func main() {
s, err := store.NewStore(config.dbPath)
l, _ := zap.NewDevelopment()
defer l.Sync()
if err != nil {
panic(err.Error())
}
r := gin.Default()
r.Use(static.Serve("/", static.LocalFile(config.frontendPath, true)))
// cors
r.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.GetHeader("Origin"))
c.Header("Access-Control-Allow-Methods", "POST, GET, PATCH")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization")
c.Header("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
})
api := NewAPI(r.Group("/api"), s, l)
api.Register()
r.NoRoute(func(c *gin.Context) {
c.File(config.frontendPath + "/index.html")
})
r.Run()
}