[beego]https & http模式是不能够同时双开的

2024-01-30 636 views
4

因为EnableHttpListen默认是开的,所以当用户尝试打开EnableHttpTLS的时候,就一定会失败。 解决方案有两种

  1. 明确互斥两个开关
  2. 启动两个http server

回答

2

可以同时开,为什么肯定会失败?

1
Beego Test

beego使用最新版本289f050c047112e7569082da0e93c7c49a86a5d4

用beego建立一个简单的rest api 项目

Test case 1:

Conf:

appname = test1

EnableHttpListen = false
runmode = dev
autorender = false
copyrequestbody = true
EnableDocs = true

EnableHttpTLS = true
HttpCertFile = conf/server.crt
HttpKeyFile = conf/server.key

2014/08/11 16:27:42 [I] Running on :8080 2014/08/11 16:27:42 [C] ListenAndServeTLS: %!(EXTRA *net.OpError=listen tcp :10443: bind: address already in use)

用浏览器尝试打开http://127.0.0.1:8080, https://127.0.0.1:10443 都失败.

修复方法,patch

diff --git a/app.go b/app.go
index d0a4766..c479304 100644
--- a/app.go
+++ b/app.go
@@ -63,7 +63,7 @@ func (app *App) Run() {
        }
        err = fcgi.Serve(l, app.Handlers)
    } else {
-       app.Server.Addr = addr
+
        app.Server.Handler = app.Handlers
        app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
        app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
@@ -73,6 +73,7 @@ func (app *App) Run() {
                if HttpsPort != 0 {
                    app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
                }
+               BeeLogger.Info("Running HTTPS on %s", app.Server.Addr)
                err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
                if err != nil {
                    BeeLogger.Critical("ListenAndServeTLS: ", err)
@@ -84,6 +85,7 @@ func (app *App) Run() {

        if EnableHttpListen {
            go func() {
+               app.Server.Addr = addr
                err := app.Server.ListenAndServe()
                if err != nil {
                    BeeLogger.Critical("ListenAndServe: ", err)
7

请参考新版本