aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Mo Tarbin <mhed.t91@gmail.com>2024-07-24 23:55:34 -0400
committerLibravatar Mo Tarbin <mhed.t91@gmail.com>2024-07-24 23:55:34 -0400
commitcafc9b6e22d160968ecb37fc8bc2ef82652fa36e (patch)
tree75dba1981636289012771fd729203e055f7dba80
parent6ce3bd837d3a4036bbcc73249033c6a87a104345 (diff)
parent71f8dfb26c9bee7721682dbe230e8a0137af2ce6 (diff)
downloaddonetick-cafc9b6e22d160968ecb37fc8bc2ef82652fa36e.tar.gz
donetick-cafc9b6e22d160968ecb37fc8bc2ef82652fa36e.tar.bz2
donetick-cafc9b6e22d160968ecb37fc8bc2ef82652fa36e.zip
Merge branch 'dev'
Diffstat (limited to '')
-rw-r--r--frontend/dist/index.html9
-rw-r--r--frontend/handler.go29
2 files changed, 32 insertions, 6 deletions
diff --git a/frontend/dist/index.html b/frontend/dist/index.html
new file mode 100644
index 0000000..5cdba0d
--- /dev/null
+++ b/frontend/dist/index.html
@@ -0,0 +1,9 @@
+<html>
+ <head>
+ <title>This file will be replaced by the frontend build process</title>
+ </head>
+ <body>
+ <h5>This file will be replaced by the frontend build process</h5>
+
+ </body>
+</html> \ No newline at end of file
diff --git a/frontend/handler.go b/frontend/handler.go
index 87ae17c..cedd505 100644
--- a/frontend/handler.go
+++ b/frontend/handler.go
@@ -23,15 +23,15 @@ func NewHandler(config *config.Config) *Handler {
}
func Routes(router *gin.Engine, h *Handler) {
+ // this whole logic is walkaround for serving frontend files
+ // TODO: figure out better way to improve it. main issue i run into is failing over to index.html when file does not exist
+
if h.ServeFrontend {
+ // if file exists in dist folder, serve it
router.Use(staticMiddleware("dist"))
- router.Static("/assets", "dist/assets")
+ // if file does not exist in dist folder fallback to index.html
+ router.NoRoute(staticMiddlewareNoRoute("dist"))
- // Gzip compression middleware
- router.Group("/assets").Use(func(c *gin.Context) {
- c.Header("Cache-Control", "max-age=31536000, immutable")
- c.Next()
- })
}
}
@@ -40,7 +40,24 @@ func staticMiddleware(root string) gin.HandlerFunc {
fileServer := http.FileServer(getFileSystem(root))
return func(c *gin.Context) {
+ _, err := fs.Stat(embeddedFiles, "dist"+c.Request.URL.Path)
+ if err != nil {
+ c.Next()
+ return
+ }
fileServer.ServeHTTP(c.Writer, c.Request)
+
+ }
+}
+func staticMiddlewareNoRoute(root string) gin.HandlerFunc {
+ fileServer := http.FileServer(getFileSystem(root))
+
+ // always serve index.html for any route does not match:
+ return func(c *gin.Context) {
+ // Rewrite all requests to serve index.html
+ c.Request.URL.Path = "/"
+ fileServer.ServeHTTP(c.Writer, c.Request)
+
}
}