added template for new golang backend
This commit is contained in:
21
server/common/ginext/cors.go
Normal file
21
server/common/ginext/cors.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package ginext
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CorsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusOK)
|
||||
} else {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
}
|
||||
21
server/common/ginext/gin.go
Normal file
21
server/common/ginext/gin.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package ginext
|
||||
|
||||
import (
|
||||
scn "blackforestbytes.com/simplecloudnotifier"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func NewEngine(cfg scn.Config) *gin.Engine {
|
||||
engine := gin.New()
|
||||
|
||||
engine.RedirectFixedPath = false
|
||||
engine.RedirectTrailingSlash = false
|
||||
|
||||
engine.Use(CorsMiddleware())
|
||||
|
||||
if cfg.GinDebug {
|
||||
engine.Use(gin.Logger())
|
||||
}
|
||||
|
||||
return engine
|
||||
}
|
||||
24
server/common/ginext/handler.go
Normal file
24
server/common/ginext/handler.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package ginext
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func RedirectFound(newuri string) gin.HandlerFunc {
|
||||
return func(g *gin.Context) {
|
||||
g.Redirect(http.StatusFound, newuri)
|
||||
}
|
||||
}
|
||||
|
||||
func RedirectTemporary(newuri string) gin.HandlerFunc {
|
||||
return func(g *gin.Context) {
|
||||
g.Redirect(http.StatusTemporaryRedirect, newuri)
|
||||
}
|
||||
}
|
||||
|
||||
func RedirectPermanent(newuri string) gin.HandlerFunc {
|
||||
return func(g *gin.Context) {
|
||||
g.Redirect(http.StatusPermanentRedirect, newuri)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user