-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
71 lines (59 loc) · 1.74 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package api
import (
"io/fs"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-errors/errors"
"github.com/zaggash/led-matrix-ui/utils"
"github.com/zaggash/led-matrix-ui/webui"
)
type httpResponse struct {
Message string
Status int
Description string
}
func (d *Display) Run() {
pixelsFolder := utils.GetPixelsFolder()
// Set the router as the default one shipped with Gin
router := gin.Default()
//gin.SetMode(gin.ReleaseMode) // TODO : Set to Production Mode !
// Set Middleware
router.Use(gin.CustomRecovery(errorHandler))
// Set MaxFile size to 8Mb
router.MaxMultipartMemory = 8 << 20
// Set Webui
var webStatic = "/public/"
//// Enable subdir from embedFS assets to /public
subAssets, _ := fs.Sub(webui.EmbedAssets, "assets")
router.StaticFS(webStatic, http.FS(subAssets))
//// Enable Pixel Images folder to /pixels
router.Static(pixelsFolder, pixelsFolder)
router.HTMLRender = webui.LoadTemplates(webui.EmbedTemplates)
// Define favicon as favicon.ico and use it in html templates
router.GET("favicon.ico", func(c *gin.Context) {
file, _ := webui.EmbedAssets.ReadFile("assets/favicon.png")
c.Data(
http.StatusOK,
"image/x-png",
file,
)
})
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "home", nil)
})
// Setup route group for the API
api := router.Group("/api")
{
api.GET("/ping", healthcheck)
api.POST("/upload", uploadFile)
api.POST("/draw/:type", d.drawImage)
api.GET("/images/:type", listImages)
}
// Start and run the server
router.Run(":3000")
}
func errorHandler(c *gin.Context, err any) {
goErr := errors.Wrap(err, 2)
httpResponse := httpResponse{Message: "Internal server error", Status: 500, Description: goErr.Error()}
c.AbortWithStatusJSON(500, httpResponse)
}