71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"mime"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"git.cosysda.de/HuskyTeufel/Hochzeit/handlers"
|
|
"git.cosysda.de/HuskyTeufel/Hochzeit/planner"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jinzhu/gorm"
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
)
|
|
|
|
type App struct {
|
|
r *gin.Engine
|
|
proxyips []string
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (a *App) start() {
|
|
a.db.AutoMigrate(&planner.Besucher{})
|
|
a.r.Use(CORSMiddleware())
|
|
a.r.Use(HEADERMiddleWare())
|
|
a.r.SetTrustedProxies(a.proxyips)
|
|
a.r.NoRoute((func(c *gin.Context) {
|
|
dir, file := path.Split(c.Request.RequestURI)
|
|
ext := filepath.Ext(file)
|
|
if file == "" || ext == "" {
|
|
c.File("./ui/dist/ui/index.html")
|
|
} else {
|
|
c.File("./ui/dist/ui/" + path.Join(dir, file))
|
|
}
|
|
}))
|
|
|
|
env := handlers.EnvHandler{
|
|
DB: a.db,
|
|
}
|
|
|
|
a.r.GET("/besucher", env.GetBesucherListHandler)
|
|
a.r.POST("/besucher", env.AddBesucherHandler)
|
|
//a.r.DELETE("/besucher/:id", env.DeleteBesucherHandler)
|
|
//a.r.PUT("/besucher", env.ComeBesucherHandler)
|
|
|
|
err := a.r.Run(":3001")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func HEADERMiddleWare() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
mime.AddExtensionType(".js", "application/javascript")
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func CORSMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
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", "DELETE, GET, OPTIONS, POST, PUT")
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.AbortWithStatus(204)
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|