reveil/ui/routes.go

85 lines
2.6 KiB
Go
Raw Normal View History

2022-10-01 17:37:12 +00:00
package ui
import (
"io"
"net/http"
"net/url"
"path"
"github.com/gin-gonic/gin"
"git.nemunai.re/nemunaire/reveil/config"
)
func serveOrReverse(forced_url string, cfg *config.Config) gin.HandlerFunc {
if cfg.DevProxy != "" {
// Forward to the Vue dev proxy
return func(c *gin.Context) {
if u, err := url.Parse(cfg.DevProxy); err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
} else {
if forced_url != "" {
u.Path = path.Join(u.Path, forced_url)
} else {
u.Path = path.Join(u.Path, c.Request.URL.Path)
}
if r, err := http.NewRequest(c.Request.Method, u.String(), c.Request.Body); err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
} else if resp, err := http.DefaultClient.Do(r); err != nil {
http.Error(c.Writer, err.Error(), http.StatusBadGateway)
} else {
defer resp.Body.Close()
for key := range resp.Header {
c.Writer.Header().Add(key, resp.Header.Get(key))
}
c.Writer.WriteHeader(resp.StatusCode)
io.Copy(c.Writer, resp.Body)
}
}
}
} else if forced_url != "" {
// Serve forced_url
return func(c *gin.Context) {
c.FileFromFS(forced_url, Assets)
}
} else {
// Serve requested file
return func(c *gin.Context) {
c.FileFromFS(c.Request.URL.Path, Assets)
}
}
}
func DeclareRoutes(router *gin.Engine, cfg *config.Config) {
if cfg.DevProxy != "" {
router.GET("/.svelte-kit/*_", serveOrReverse("", cfg))
router.GET("/node_modules/*_", serveOrReverse("", cfg))
router.GET("/@vite/*_", serveOrReverse("", cfg))
2024-06-13 17:58:38 +00:00
router.GET("/@id/*_", serveOrReverse("", cfg))
2022-10-01 17:37:12 +00:00
router.GET("/@fs/*_", serveOrReverse("", cfg))
router.GET("/src/*_", serveOrReverse("", cfg))
2024-06-13 17:58:38 +00:00
router.GET("/home/*_", serveOrReverse("", cfg))
2022-10-01 17:37:12 +00:00
}
router.GET("/", serveOrReverse("", cfg))
router.GET("/alarms", serveOrReverse("/", cfg))
2022-10-02 21:24:33 +00:00
router.GET("/alarms/*_", serveOrReverse("/", cfg))
2022-10-01 17:37:12 +00:00
router.GET("/settings", serveOrReverse("/", cfg))
2022-10-02 21:24:33 +00:00
router.GET("/settings/*_", serveOrReverse("/", cfg))
2022-10-01 17:37:12 +00:00
router.GET("/routines", serveOrReverse("/", cfg))
2022-10-02 21:24:33 +00:00
router.GET("/routines/*_", serveOrReverse("/", cfg))
2022-10-01 17:37:12 +00:00
router.GET("/musiks", serveOrReverse("/", cfg))
2022-10-02 21:24:33 +00:00
router.GET("/musiks/*_", serveOrReverse("/", cfg))
2022-10-01 17:37:12 +00:00
router.GET("/history", serveOrReverse("/", cfg))
2022-10-02 21:24:33 +00:00
router.GET("/history/*_", serveOrReverse("/", cfg))
2022-10-01 17:37:12 +00:00
router.GET("/_app/*_", serveOrReverse("", cfg))
router.GET("/img/*_", serveOrReverse("", cfg))
router.GET("/favicon.ico", serveOrReverse("", cfg))
router.GET("/manifest.json", serveOrReverse("", cfg))
router.GET("/service-worker.js", serveOrReverse("", cfg))
}