Mensa-API/server/server.go
2024-10-01 19:01:26 +02:00

40 lines
911 B
Go

package server
import (
"github.com/gin-gonic/gin"
)
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
func Run() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.Use(CORSMiddleware())
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Mensen API",
})
})
r.GET("/state/:state", state)
r.GET("/city/:city", city)
r.GET("/mensa/:city/:mensa", mensa)
r.Run(":80")
}