place/api/endpoints.go

45 lines
1023 B
Go
Raw Normal View History

2024-06-12 18:44:12 +00:00
package api
import (
"net/http"
2024-06-13 17:25:45 +00:00
"path/filepath"
2024-06-12 18:44:12 +00:00
"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)
2024-06-13 17:25:45 +00:00
router.GET("/updates", read.Continuous)
2024-06-13 11:25:18 +00:00
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 != "" {
2024-06-13 17:25:45 +00:00
router.StaticFile("/", filepath.Join(staticPath, "index.html"))
2024-06-13 12:43:09 +00:00
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)
}
}