place/api/endpoints.go

42 lines
899 B
Go
Raw Normal View History

2024-06-12 18:44:12 +00:00
package api
import (
"net/http"
"time"
2024-06-13 11:25:18 +00:00
"git.tek.govt.hu/dowerx/place/api/read"
"git.tek.govt.hu/dowerx/place/api/write"
2024-06-12 18:44:12 +00:00
"github.com/gin-gonic/gin"
)
2024-06-13 12:43:09 +00:00
func Listen(address string, staticPath string) {
2024-06-12 18:44:12 +00:00
router := gin.Default()
2024-06-13 11:25:18 +00:00
router.GET("/info", read.Info)
router.GET("/tile", read.Tile)
router.POST("/set", write.Pixel)
router.OPTIONS("/set", func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
2024-06-13 12:43:09 +00:00
c.Header("Vary", "Origin")
c.Header("Access-Control-Allow-Headers", "content-type")
2024-06-13 11:25:18 +00:00
c.Header("Access-Control-Allow-Methods", "*")
2024-06-13 12:43:09 +00:00
c.Header("Access-Control-Allow-Credentials", "true")
2024-06-13 11:25:18 +00:00
})
2024-06-13 12:43:09 +00:00
if staticPath != "" {
router.Static("/static", staticPath)
}
2024-06-12 18:44:12 +00:00
server := &http.Server{
Addr: address,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}