add base url
This commit is contained in:
+7
-2
@@ -27,7 +27,8 @@ func NewAPIHandler(g *gin.RouterGroup) {
|
||||
|
||||
func (a *APIHandler) initRouter(g *gin.RouterGroup) {
|
||||
g.Use(func(c *gin.Context) {
|
||||
if c.Request.URL.Path != "/api/login" && c.Request.URL.Path != "/api/logout" {
|
||||
path := c.Request.URL.Path
|
||||
if !strings.HasSuffix(path, "login") && !strings.HasSuffix(path, "logout") {
|
||||
checkLogin(c)
|
||||
}
|
||||
})
|
||||
@@ -62,7 +63,11 @@ func (a *APIHandler) postHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = SetLoginUser(c, loginUser)
|
||||
logger.Info("user ", loginUser, " login success")
|
||||
if err == nil {
|
||||
logger.Info("user ", loginUser, " login success")
|
||||
} else {
|
||||
logger.Warning("login failed: ", err)
|
||||
}
|
||||
|
||||
jsonMsg(c, "", nil)
|
||||
case "save":
|
||||
|
||||
@@ -21,6 +21,7 @@ var defaultValueMap = map[string]string{
|
||||
"webSecret": common.Random(32),
|
||||
"webCertFile": "",
|
||||
"webKeyFile": "",
|
||||
"webPath": "/app/",
|
||||
"sessionMaxAge": "0",
|
||||
"timeLocation": "Asia/Tehran",
|
||||
"subListen": "",
|
||||
@@ -158,6 +159,20 @@ func (s *SettingService) GetKeyFile() (string, error) {
|
||||
return s.getString("webKeyFile")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetWebPath() (string, error) {
|
||||
webPath, err := s.getString("webPath")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !strings.HasPrefix(webPath, "/") {
|
||||
webPath = "/" + webPath
|
||||
}
|
||||
if !strings.HasSuffix(webPath, "/") {
|
||||
webPath += "/"
|
||||
}
|
||||
return webPath, nil
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSecret() ([]byte, error) {
|
||||
secret, err := s.getString("webSecret")
|
||||
if secret == defaultValueMap["webSecret"] {
|
||||
@@ -278,6 +293,17 @@ func (s *SettingService) Save(tx *gorm.DB, changes []model.Changes) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Correct Pathes start and ends with `/`
|
||||
if key == "webPath" ||
|
||||
key == "subPath" {
|
||||
if !strings.HasPrefix(obj, "/") {
|
||||
obj = "/" + obj
|
||||
}
|
||||
if !strings.HasSuffix(obj, "/") {
|
||||
obj += "/"
|
||||
}
|
||||
}
|
||||
|
||||
err = tx.Model(model.Setting{}).Where("key = ?", key).Update("value", obj).Error
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+18
-10
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user