-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (54 loc) · 1.66 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
package main
import (
"fmt"
"os"
"github.com/gin-gonic/gin"
"github.com/kushao1267/Facade/facade/controllers"
"github.com/kushao1267/Facade/facade/db"
_ "github.com/kushao1267/Facade/facade/db"
)
var githash = ""
var buildstamp = ""
var goversion = ""
// CORSMiddleware 跨域中间件
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, x-access-token")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
fmt.Println("OPTIONS")
c.AbortWithStatus(200)
} else {
c.Next()
}
}
}
func main() {
// 二进制文件信息
args := os.Args
if len(args) == 2 && (args[1] == "--version" || args[1] == "-v") {
fmt.Printf("Git Commit Hash: %s\n", githash)
fmt.Printf("UTC Build Time : %s\n", buildstamp)
fmt.Printf("Golang Version : %s\n", goversion)
return
}
r := gin.New()
gin.SetMode(os.Getenv("GIN_MODE"))
r.Use(gin.Logger())
r.Use(gin.Recovery())
r.Use(CORSMiddleware())
db.Init()
v1 := r.Group("/api/v1")
{
ping := new(controllers.PingController)
v1.GET("/ping", ping.Ping)
link := new(controllers.LinkController)
v1.POST("/preview", link.Preview)
v1.POST("/del", link.Del)
}
_ = r.Run(":8080")
}