-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·79 lines (61 loc) · 2.01 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
package main
import (
"fmt"
"net/http"
"runtime"
"github.com/ShubhamBansal1997/go-api-starter-kit/controllers"
"github.com/ShubhamBansal1997/go-api-starter-kit/db"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
)
//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() {
r := gin.Default()
store, _ := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret"))
r.Use(sessions.Sessions("gin-boilerplate-session", store))
r.Use(CORSMiddleware())
db.Init()
v1 := r.Group("/v1")
{
/*** START USER ***/
user := new(controllers.UserController)
v1.POST("/user/signin", user.Signin)
v1.POST("/user/signup", user.Signup)
v1.GET("/user/signout", user.Signout)
/*** START Article ***/
article := new(controllers.ArticleController)
v1.POST("/article", article.Create)
v1.GET("/articles", article.All)
v1.GET("/article/:id", article.One)
v1.PUT("/article/:id", article.Update)
v1.DELETE("/article/:id", article.Delete)
}
r.LoadHTMLGlob("./public/html/*")
r.Static("/public", "./public")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"ginBoilerplateVersion": "v0.03",
"goVersion": runtime.Version(),
})
})
r.NoRoute(func(c *gin.Context) {
c.HTML(404, "404.html", gin.H{})
})
r.Run(":9000")
}