解决beego中同时开启http和https时,https端口占用问题

时间:2023-03-09 03:31:58
解决beego中同时开启http和https时,https端口占用问题

在beego的app.go文件中, 找到

// run normal mode
if BConfig.Listen.EnableHTTPS {
go func() {
time.Sleep( * time.Microsecond) //这里是我修改后的代码, 原本只是睡眠了 20 * Microsecond,我这里改到了1000, 可以继续往大改
if BConfig.Listen.HTTPSPort != {
app.Server.Addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort)
} else if BConfig.Listen.EnableHTTP {
BeeLogger.Info("Start https server error, confict with http.Please reset https port")
return
}
logs.Info("https server Running on https://%s", app.Server.Addr)
if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
logs.Critical("ListenAndServeTLS: ", err)
time.Sleep( * time.Microsecond)
endRunning <- true
}
}()
}

  问题原因分析: 初步判断为http监听地址和https监听地址有一定几率互相覆盖导致, 所以加一个睡眠时间, 让两个协程执行时间错开.