From a95d9dda769744445ce6086e48af85377d7bc9ed Mon Sep 17 00:00:00 2001 From: "zhangwenbing.zwb" Date: Wed, 25 Oct 2023 20:43:11 +0800 Subject: [PATCH 1/3] feat: make admin path configurable --- config/config.go | 2 ++ config/model.go | 1 + event/listener/start.go | 7 ++++++- service/impl/option.go | 4 ++++ service/option.go | 1 + 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index ec6ace98..7148dc93 100644 --- a/config/config.go +++ b/config/config.go @@ -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) diff --git a/config/model.go b/config/model.go index 755fcfa8..a25657c7 100644 --- a/config/model.go +++ b/config/model.go @@ -49,4 +49,5 @@ type Sonic struct { TemplateDir string `mapstructure:"template_dir"` ThemeDir string AdminResourcesDir string + AdminUrlPath string `mapstructure:"admin_url_path"` } diff --git a/event/listener/start.go b/event/listener/start.go index 3cf1acf6..209de4e0 100644 --- a/event/listener/start.go +++ b/event/listener/start.go @@ -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 diff --git a/service/impl/option.go b/service/impl/option.go index a3131461..b2b96c16 100644 --- a/service/impl/option.go +++ b/service/impl/option.go @@ -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 +} diff --git a/service/option.go b/service/option.go index 028f49c0..4a46fb58 100644 --- a/service/option.go +++ b/service/option.go @@ -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 { From a50ae1669fc80b9ba687a71fd68edee098fa3b88 Mon Sep 17 00:00:00 2001 From: "zhangwenbing.zwb" Date: Thu, 9 Nov 2023 12:12:16 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fmt:=20adminURLPath=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/model.go | 2 +- event/listener/start.go | 4 ++-- service/impl/option.go | 4 ++-- service/option.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config/model.go b/config/model.go index a25657c7..44ec9677 100644 --- a/config/model.go +++ b/config/model.go @@ -49,5 +49,5 @@ type Sonic struct { TemplateDir string `mapstructure:"template_dir"` ThemeDir string AdminResourcesDir string - AdminUrlPath string `mapstructure:"admin_url_path"` + AdminURLPath string `mapstructure:"admin_url_path"` } diff --git a/event/listener/start.go b/event/listener/start.go index 209de4e0..a2e00497 100644 --- a/event/listener/start.go +++ b/event/listener/start.go @@ -87,11 +87,11 @@ func (s *StartListener) printStartInfo(ctx context.Context) error { log.Info(site) fmt.Println(site) - adminUrlPath, err := s.optionService.GetAdminUrlPath(ctx) + adminURLPath, err := s.optionService.GetAdminURLPath(ctx) if err != nil { return err } - adminSite := logger.BlueBold + "Sonic admin started at " + blogURL + "/" + adminUrlPath + logger.Reset + adminSite := logger.BlueBold + "Sonic admin started at " + blogURL + "/" + adminURLPath + logger.Reset log.Info(adminSite) fmt.Println(adminSite) return nil diff --git a/service/impl/option.go b/service/impl/option.go index b2b96c16..3186049c 100644 --- a/service/impl/option.go +++ b/service/impl/option.go @@ -470,6 +470,6 @@ func (o *optionServiceImpl) GetAttachmentType(ctx context.Context) consts.Attach } } -func (o *optionServiceImpl) GetAdminUrlPath(ctx context.Context) (string, error) { - return o.Config.Sonic.AdminUrlPath, nil +func (o *optionServiceImpl) GetAdminURLPath(ctx context.Context) (string, error) { + return o.Config.Sonic.AdminURLPath, nil } diff --git a/service/option.go b/service/option.go index 4a46fb58..60afc02e 100644 --- a/service/option.go +++ b/service/option.go @@ -32,7 +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) + GetAdminURLPath(ctx context.Context) (string, error) } type ClientOptionService interface { From c24c27aa3f9224640a27653c15dc79b28d99de51 Mon Sep 17 00:00:00 2001 From: "zhangwenbing.zwb" Date: Thu, 9 Nov 2023 13:56:50 +0800 Subject: [PATCH 3/3] feat: support customize admin url path --- conf/config.dev.yaml | 3 ++- handler/content/view.go | 3 ++- handler/router.go | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/conf/config.dev.yaml b/conf/config.dev.yaml index 7dc4eb4c..70fbea04 100644 --- a/conf/config.dev.yaml +++ b/conf/config.dev.yaml @@ -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) \ No newline at end of file + log_dir: "./logs" # 不填则使用work_dir 路径下的log路径 (If it is empty, use the "log" path under work_dir) + admin_url_path: admin_random \ No newline at end of file diff --git a/handler/content/view.go b/handler/content/view.go index ed4c0c46..a617593e 100644 --- a/handler/content/view.go +++ b/handler/content/view.go @@ -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) { diff --git a/handler/router.go b/handler/router.go index a4b1f376..7ca3a6db 100644 --- a/handler/router.go +++ b/handler/router.go @@ -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))