From a088fa439aca16479baf28f296ffcc7473cd9b3f Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Thu, 10 Nov 2022 11:00:26 +0800 Subject: [PATCH 1/3] feat: format site url to only scheme and host --- internal/install/install_controller.go | 1 + internal/install/install_req.go | 11 ++++++++++- internal/schema/siteinfo_schema.go | 13 +++++++++++++ internal/service/siteinfo/siteinfo_service.go | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/internal/install/install_controller.go b/internal/install/install_controller.go index ab1d9790..25552ccf 100644 --- a/internal/install/install_controller.go +++ b/internal/install/install_controller.go @@ -143,6 +143,7 @@ func InitBaseInfo(ctx *gin.Context) { if handler.BindAndCheck(ctx, req) { return } + req.FormatSiteUrl() c, err := conf.ReadConfig(confPath) if err != nil { diff --git a/internal/install/install_req.go b/internal/install/install_req.go index f9b44a7b..7c836d5a 100644 --- a/internal/install/install_req.go +++ b/internal/install/install_req.go @@ -2,6 +2,7 @@ package install import ( "fmt" + "net/url" "strings" "xorm.io/xorm/schemas" @@ -73,9 +74,17 @@ type InitEnvironmentResp struct { type InitBaseInfoReq struct { Language string `validate:"required,gt=0,lte=30" json:"lang"` SiteName string `validate:"required,gt=0,lte=30" json:"site_name"` - SiteURL string `validate:"required,gt=0,lte=512" json:"site_url"` + SiteURL string `validate:"required,gt=0,lte=512,url" json:"site_url"` ContactEmail string `validate:"required,email,gt=0,lte=500" json:"contact_email"` AdminName string `validate:"required,gt=4,lte=30" json:"admin_name"` AdminPassword string `validate:"required,gte=8,lte=32" json:"admin_password"` AdminEmail string `validate:"required,email,gt=0,lte=500" json:"admin_email"` } + +func (r *InitBaseInfoReq) FormatSiteUrl() { + parsedUrl, err := url.Parse(r.SiteURL) + if err != nil { + return + } + r.SiteURL = fmt.Sprintf("%s://%s", parsedUrl.Scheme, parsedUrl.Host) +} diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go index 9a0a4839..85a643ed 100644 --- a/internal/schema/siteinfo_schema.go +++ b/internal/schema/siteinfo_schema.go @@ -1,5 +1,10 @@ package schema +import ( + "fmt" + "net/url" +) + // SiteGeneralReq site general request type SiteGeneralReq struct { Name string `validate:"required,gt=1,lte=128" form:"name" json:"name"` @@ -9,6 +14,14 @@ type SiteGeneralReq struct { ContactEmail string `validate:"required,gt=1,lte=512,email" form:"contact_email" json:"contact_email"` } +func (r *SiteGeneralReq) FormatSiteUrl() { + parsedUrl, err := url.Parse(r.SiteUrl) + if err != nil { + return + } + r.SiteUrl = fmt.Sprintf("%s://%s", parsedUrl.Scheme, parsedUrl.Host) +} + // SiteInterfaceReq site interface request type SiteInterfaceReq struct { Logo string `validate:"omitempty,gt=0,lte=256" form:"logo" json:"logo"` diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go index 822c7bde..66c49479 100644 --- a/internal/service/siteinfo/siteinfo_service.go +++ b/internal/service/siteinfo/siteinfo_service.go @@ -57,6 +57,7 @@ func (s *SiteInfoService) GetSiteInterface(ctx context.Context) (resp *schema.Si } func (s *SiteInfoService) SaveSiteGeneral(ctx context.Context, req schema.SiteGeneralReq) (err error) { + req.FormatSiteUrl() var ( siteType = "general" content []byte From 555c1936ade4a447158cee374d15c0bf707ff88b Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Thu, 10 Nov 2022 11:22:07 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20/install=20path=20will=20be=20redire?= =?UTF-8?q?cted=20to=20/=CB=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/router/ui.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/router/ui.go b/internal/router/ui.go index 3498265c..f1296d9f 100644 --- a/internal/router/ui.go +++ b/internal/router/ui.go @@ -78,6 +78,9 @@ func (a *UIRouter) Register(r *gin.Engine) { filePath = UIRootFilePath + name case "/manifest.json": filePath = UIRootFilePath + name + case "/install": + c.Redirect(http.StatusFound, "/") + return default: filePath = UIIndexFilePath c.Header("content-type", "text/html;charset=utf-8") From 3f5ac0a6144892416367f57c3b15b163a40adcbd Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Thu, 10 Nov 2022 11:31:53 +0800 Subject: [PATCH 3/3] feat: add /50x web page --- internal/install/install_server.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/install/install_server.go b/internal/install/install_server.go index b2ada51e..c0c38a7f 100644 --- a/internal/install/install_server.go +++ b/internal/install/install_server.go @@ -35,16 +35,16 @@ func NewInstallHTTPServer() *gin.Engine { installApi := r.Group("") installApi.GET("/install", WebPage) - + installApi.GET("/50x", WebPage) installApi.GET("/installation/language/options", LangOptions) - installApi.POST("/installation/db/check", CheckDatabase) - installApi.POST("/installation/config-file/check", CheckConfigFile) - installApi.POST("/installation/init", InitEnvironment) - installApi.POST("/installation/base-info", InitBaseInfo) + + r.NoRoute(func(ctx *gin.Context) { + ctx.Redirect(http.StatusFound, "/50x") + }) return r }