-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
69 lines (63 loc) · 2.02 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
package main
import (
"toolKit/app/toolkit/service/service_xrate"
"toolKit/app/toolkit/swagger_routers"
"toolKit/routers"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/unrolled/secure"
"net/http"
)
//Cors cross-domain solution(Gin Middleware)
func Cors() gin.HandlerFunc {
return func(context *gin.Context) {
method := context.Request.Method
context.Header("Access-Control-Allow-Origin", "*")
context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token,X-Nideshop-Token, x-nideshop-token, Authorization, Token")
context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
context.Header("Access-Control-Allow-Credentials", "true")
if method == "OPTIONS" {
context.AbortWithStatus(http.StatusOK)
}
context.Next()
}
}
//TlsHandler is func to use TLS
func TlsHandler() gin.HandlerFunc {
return func(c *gin.Context) {
secureMiddleware := secure.New(secure.Options{
SSLRedirect: true,
SSLHost: "localhost:8779",
})
err := secureMiddleware.Process(c.Writer, c.Request)
// If there was an error, do not continue.
if err != nil {
return
}
c.Next()
}
}
// @title toolKit
// @version 1.0
// @description This is a toolKit for server with Gin.
// @contact.name sovea
// @license.name MIT License
// @license.url https://opensource.org/licenses/MIT
func main() {
//RawInit
Gin_Instance := routers.RawInit()
//CORS
Gin_Instance.Use(Cors())
// Access rate limit
Gin_Instance.Use(service_xrate.DefaultLimitMiddleware())
//TLS
Gin_Instance.Use(TlsHandler())
//Include Some Router
routers.IncludeWith(Gin_Instance, swagger_routers.Routers)
// Static resource loading
Gin_Instance.StaticFS("/public", http.Dir("./public"))
Gin_Instance.StaticFile("/favicon.png", "../resources/index.png")
//Start server
Gin_Instance.RunTLS("127.0.0.1:8779", "./resources/ssl/toolkit.crt", "./resources/ssl/toolkit.key")
}