Mensa-API/server/server.go

40 lines
911 B
Go
Raw Normal View History

2024-03-27 16:41:57 +00:00
package server
import (
"github.com/gin-gonic/gin"
)
2024-10-01 17:01:26 +00:00
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()
}
}
2024-03-27 16:41:57 +00:00
func Run() {
2024-09-15 18:36:42 +00:00
gin.SetMode(gin.ReleaseMode)
2024-03-27 16:41:57 +00:00
r := gin.Default()
2024-09-15 18:36:42 +00:00
2024-10-01 17:01:26 +00:00
r.Use(CORSMiddleware())
2024-09-15 18:36:42 +00:00
r.GET("/", func(c *gin.Context) {
2024-09-15 16:57:03 +00:00
c.JSON(200, gin.H{
2024-09-15 18:36:42 +00:00
"message": "Mensen API",
2024-03-27 16:41:57 +00:00
})
2024-09-15 16:57:03 +00:00
})
r.GET("/state/:state", state)
2024-09-15 17:10:19 +00:00
r.GET("/city/:city", city)
r.GET("/mensa/:city/:mensa", mensa)
2024-09-15 18:36:42 +00:00
r.Run(":80")
2024-03-27 16:41:57 +00:00
}