Skip to content

Commit

Permalink
feat: make admin path configurable (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
textworld authored Nov 16, 2023
1 parent a7249f3 commit b201e0d
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion conf/config.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ mysql:
sonic:
mode: "development"
work_dir: "./" # 不填默认为当前路径,用来存放日志文件、数据库文件、模板、上传的附件等(The default is the current directory. Used to store log files, database files, templates, upload files)
log_dir: "./logs" # 不填则使用work_dir 路径下的log路径 (If it is empty, use the "log" path under work_dir)
log_dir: "./logs" # 不填则使用work_dir 路径下的log路径 (If it is empty, use the "log" path under work_dir)
admin_url_path: admin_random
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func NewConfig() *Config {
viper.SetConfigName("config")
}

viper.SetDefault("sonic.admin_url_path", "admin")

conf := &Config{}
if err := viper.ReadInConfig(); err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ type Sonic struct {
TemplateDir string `mapstructure:"template_dir"`
ThemeDir string
AdminResourcesDir string
AdminURLPath string `mapstructure:"admin_url_path"`
}
7 changes: 6 additions & 1 deletion event/listener/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ func (s *StartListener) printStartInfo(ctx context.Context) error {
site := logger.BlueBold + "Sonic started at " + blogURL + logger.Reset
log.Info(site)
fmt.Println(site)
adminSite := logger.BlueBold + "Sonic admin started at " + blogURL + "/admin" + logger.Reset

adminURLPath, err := s.optionService.GetAdminURLPath(ctx)
if err != nil {
return err
}
adminSite := logger.BlueBold + "Sonic admin started at " + blogURL + "/" + adminURLPath + logger.Reset
log.Info(adminSite)
fmt.Println(adminSite)
return nil
Expand Down
3 changes: 2 additions & 1 deletion handler/content/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ func (v *ViewHandler) Install(ctx *gin.Context) {
if isInstall {
return
}
ctx.Redirect(http.StatusTemporaryRedirect, "admin/#install")
adminURLPath, _ := v.OptionService.GetAdminURLPath(ctx)
ctx.Redirect(http.StatusTemporaryRedirect, adminURLPath+"/#install")
}

func (v *ViewHandler) Logo(ctx *gin.Context) (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *Server) RegisterRouters() {
})
{
staticRouter := router.Group("/")
staticRouter.StaticFS("admin", gin.Dir(s.Config.Sonic.AdminResourcesDir, false))
staticRouter.StaticFS(s.Config.Sonic.AdminURLPath, gin.Dir(s.Config.Sonic.AdminResourcesDir, false))
staticRouter.StaticFS("/css", gin.Dir(filepath.Join(s.Config.Sonic.AdminResourcesDir, "css"), false))
staticRouter.StaticFS("/js", gin.Dir(filepath.Join(s.Config.Sonic.AdminResourcesDir, "js"), false))
staticRouter.StaticFS("/images", gin.Dir(filepath.Join(s.Config.Sonic.AdminResourcesDir, "images"), false))
Expand Down
4 changes: 4 additions & 0 deletions service/impl/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,7 @@ func (o *optionServiceImpl) GetAttachmentType(ctx context.Context) consts.Attach
return consts.AttachmentTypeLocal
}
}

func (o *optionServiceImpl) GetAdminURLPath(ctx context.Context) (string, error) {
return o.Config.Sonic.AdminURLPath, nil
}
1 change: 1 addition & 0 deletions service/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type OptionService interface {
GetLinkPrefix(ctx context.Context) (string, error)
GetSheetPrefix(ctx context.Context) (string, error)
GetAttachmentType(ctx context.Context) consts.AttachmentType
GetAdminURLPath(ctx context.Context) (string, error)
}

type ClientOptionService interface {
Expand Down

0 comments on commit b201e0d

Please sign in to comment.