add base url

This commit is contained in:
Alireza Ahmadi
2024-02-26 19:01:01 +01:00
parent 30c9ed6aa7
commit c54d9c15bc
16 changed files with 80 additions and 30 deletions
+18 -10
View File
@@ -53,6 +53,11 @@ func (s *Server) initRouter() (*gin.Engine, error) {
engine := gin.Default()
base_url, err := s.settingService.GetWebPath()
if err != nil {
return nil, err
}
webDomain, err := s.settingService.GetWebDomain()
if err != nil {
return nil, err
@@ -68,10 +73,11 @@ func (s *Server) initRouter() (*gin.Engine, error) {
}
engine.Use(gzip.Gzip(gzip.DefaultCompression))
assetsBasePath := "/assets/"
assetsBasePath := base_url + "assets/"
store := cookie.NewStore(secret)
engine.Use(sessions.Sessions("session", store))
engine.Use(func(c *gin.Context) {
uri := c.Request.RequestURI
if strings.HasPrefix(uri, assetsBasePath) {
@@ -87,26 +93,28 @@ func (s *Server) initRouter() (*gin.Engine, error) {
engine.StaticFS(assetsBasePath, http.FS(assetsFS))
group_api := engine.Group("/api")
group_api := engine.Group(base_url + "api")
api.NewAPIHandler(group_api)
// Load the HTML template
engine.LoadHTMLFiles("backend/web/html/index.html")
// Serve index.html as the entry point
// Handle all other routes by serving index.html
engine.NoRoute(func(c *gin.Context) {
if c.Request.URL.Path != "/login" && !api.IsLogin(c) {
c.Redirect(http.StatusTemporaryRedirect, "/login")
if !strings.HasPrefix(c.Request.URL.Path, base_url) {
c.String(404, "")
return
}
if c.Request.URL.Path == "/login" && api.IsLogin(c) {
c.Redirect(http.StatusTemporaryRedirect, "/")
if c.Request.URL.Path != base_url+"login" && !api.IsLogin(c) {
c.Redirect(http.StatusTemporaryRedirect, base_url+"login")
return
}
data, err := content.ReadFile("html/index.html")
if err != nil {
c.String(http.StatusInternalServerError, "Internal Server Error")
if c.Request.URL.Path == base_url+"login" && api.IsLogin(c) {
c.Redirect(http.StatusTemporaryRedirect, base_url)
return
}
c.Data(http.StatusOK, "text/html", data)
c.HTML(http.StatusOK, "index.html", gin.H{"BASE_URL": base_url})
})
return engine, nil