Fix routing in frontend not working (#723)

This commit is contained in:
gadotroee 2022-01-31 10:49:44 +02:00 committed by GitHub
parent 0abd7c06ff
commit 4be7164f20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 27 deletions

View File

@ -62,7 +62,6 @@ const (
socketConnectionRetries = 30 socketConnectionRetries = 30
socketConnectionRetryDelay = time.Second * 2 socketConnectionRetryDelay = time.Second * 2
socketHandshakeTimeout = time.Second * 2 socketHandshakeTimeout = time.Second * 2
uiIndexPath = "./site/index.html"
) )
func main() { func main() {
@ -248,16 +247,23 @@ func hostApi(socketHarOutputChannel chan<- *tapApi.OutputChannelItem) {
app.Use(DisableRootStaticCache()) app.Use(DisableRootStaticCache())
if err := setUIFlags(); err != nil { var staticFolder string
logger.Log.Errorf("Error setting ui mode, err: %v", err) if config.Config.StandaloneMode {
staticFolder = "./site-standalone"
} else {
staticFolder = "./site"
} }
if config.Config.StandaloneMode { indexStaticFile := staticFolder + "/index.html"
app.Use(static.ServeRoot("/", "./site-standalone")) if err := setUIFlags(indexStaticFile); err != nil {
} else { logger.Log.Errorf("Error setting ui flags, err: %v", err)
app.Use(static.ServeRoot("/", "./site"))
} }
app.Use(static.ServeRoot("/", staticFolder))
app.NoRoute(func(c *gin.Context) {
c.File(indexStaticFile)
})
app.Use(middlewares.CORSMiddleware()) // This has to be called after the static middleware, does not work if its called before app.Use(middlewares.CORSMiddleware()) // This has to be called after the static middleware, does not work if its called before
api.WebSocketRoutes(app, &eventHandlers, startTime) api.WebSocketRoutes(app, &eventHandlers, startTime)
@ -278,7 +284,6 @@ func hostApi(socketHarOutputChannel chan<- *tapApi.OutputChannelItem) {
routes.EntriesRoutes(app) routes.EntriesRoutes(app)
routes.MetadataRoutes(app) routes.MetadataRoutes(app)
routes.StatusRoutes(app) routes.StatusRoutes(app)
routes.NotFoundRoute(app)
utils.StartServer(app) utils.StartServer(app)
} }
@ -293,7 +298,7 @@ func DisableRootStaticCache() gin.HandlerFunc {
} }
} }
func setUIFlags() error { func setUIFlags(uiIndexPath string) error {
read, err := ioutil.ReadFile(uiIndexPath) read, err := ioutil.ReadFile(uiIndexPath)
if err != nil { if err != nil {
return err return err

View File

@ -1,18 +0,0 @@
package routes
import (
"github.com/gin-gonic/gin"
"net/http"
)
// NotFoundRoute defines the 404 Error route.
func NotFoundRoute(app *gin.Engine) {
app.Use(
func(c *gin.Context) {
c.JSON(http.StatusNotFound, map[string]interface{}{
"error": true,
"msg": "sorry, endpoint is not found",
})
},
)
}