From db8906e2391c7894240fbbe9fe2379385237fc18 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 16 Nov 2022 15:31:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 1 - main.go | 18 ++++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index e80df84..ff4b312 100644 --- a/config/config.go +++ b/config/config.go @@ -37,7 +37,6 @@ type Config struct { PostCommentUrl string `yaml:"postCommentUrl"` TrustIps []string `yaml:"trustIps"` TrustServerNames []string `yaml:"trustServerNames"` - Port string `yaml:"port"` } type Mail struct { diff --git a/main.go b/main.go index 4fb7b77..a03e7a6 100644 --- a/main.go +++ b/main.go @@ -16,16 +16,27 @@ import ( "math/rand" "os" "os/signal" + "regexp" + "strings" "syscall" "time" ) var confPath string +var port string var middleWareReloadFn func() +var intReg = regexp.MustCompile(`^\d`) func init() { flag.StringVar(&confPath, "c", "config.yaml", "config file") + flag.StringVar(&port, "p", "", "port") flag.Parse() + if port == "" && os.Getenv("PORT") == "" { + port = "80" + } + if intReg.MatchString(port) && !strings.Contains(port, ":") { + port = ":" + port + } rand.Seed(time.Now().UnixNano()) err := initConf(confPath) if err != nil { @@ -117,15 +128,10 @@ func signalNotify() { } func main() { - c := config.Conf.Load() - if c.Port == "" { - c.Port = "80" - config.Conf.Store(c) - } go signalNotify() Gin, reloadFn := route.SetupRouter() middleWareReloadFn = reloadFn - err := Gin.Run(c.Port) + err := Gin.Run(port) if err != nil { panic(err) }