beego:support https & http listen
Showing
4 changed files
with
39 additions
and
9 deletions
| ... | @@ -88,7 +88,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) { | ... | @@ -88,7 +88,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) { |
| 88 | fmt.Fprintln(rw, "StaticExtensionsToGzip:", StaticExtensionsToGzip) | 88 | fmt.Fprintln(rw, "StaticExtensionsToGzip:", StaticExtensionsToGzip) |
| 89 | fmt.Fprintln(rw, "HttpAddr:", HttpAddr) | 89 | fmt.Fprintln(rw, "HttpAddr:", HttpAddr) |
| 90 | fmt.Fprintln(rw, "HttpPort:", HttpPort) | 90 | fmt.Fprintln(rw, "HttpPort:", HttpPort) |
| 91 | fmt.Fprintln(rw, "HttpTLS:", HttpTLS) | 91 | fmt.Fprintln(rw, "HttpTLS:", EnableHttpTLS) |
| 92 | fmt.Fprintln(rw, "HttpCertFile:", HttpCertFile) | 92 | fmt.Fprintln(rw, "HttpCertFile:", HttpCertFile) |
| 93 | fmt.Fprintln(rw, "HttpKeyFile:", HttpKeyFile) | 93 | fmt.Fprintln(rw, "HttpKeyFile:", HttpKeyFile) |
| 94 | fmt.Fprintln(rw, "RecoverPanic:", RecoverPanic) | 94 | fmt.Fprintln(rw, "RecoverPanic:", RecoverPanic) | ... | ... |
| ... | @@ -45,6 +45,7 @@ func (app *App) Run() { | ... | @@ -45,6 +45,7 @@ func (app *App) Run() { |
| 45 | err error | 45 | err error |
| 46 | l net.Listener | 46 | l net.Listener |
| 47 | ) | 47 | ) |
| 48 | endRunning := make(chan bool) | ||
| 48 | 49 | ||
| 49 | if UseFcgi { | 50 | if UseFcgi { |
| 50 | if HttpPort == 0 { | 51 | if HttpPort == 0 { |
| ... | @@ -83,18 +84,34 @@ func (app *App) Run() { | ... | @@ -83,18 +84,34 @@ func (app *App) Run() { |
| 83 | ReadTimeout: time.Duration(HttpServerTimeOut) * time.Second, | 84 | ReadTimeout: time.Duration(HttpServerTimeOut) * time.Second, |
| 84 | WriteTimeout: time.Duration(HttpServerTimeOut) * time.Second, | 85 | WriteTimeout: time.Duration(HttpServerTimeOut) * time.Second, |
| 85 | } | 86 | } |
| 86 | if HttpTLS { | 87 | if EnableHttpTLS { |
| 87 | err = s.ListenAndServeTLS(HttpCertFile, HttpKeyFile) | 88 | go func() { |
| 88 | } else { | 89 | if HttpsPort != 0 { |
| 89 | err = s.ListenAndServe() | 90 | s.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort) |
| 90 | } | 91 | } |
| 92 | err := s.ListenAndServeTLS(HttpCertFile, HttpKeyFile) | ||
| 93 | if err != nil { | ||
| 94 | BeeLogger.Critical("ListenAndServe: ", err) | ||
| 95 | time.Sleep(100 * time.Microsecond) | ||
| 96 | endRunning <- true | ||
| 91 | } | 97 | } |
| 98 | }() | ||
| 92 | } | 99 | } |
| 93 | 100 | ||
| 101 | if EnableHttpListen { | ||
| 102 | go func() { | ||
| 103 | err := s.ListenAndServe() | ||
| 94 | if err != nil { | 104 | if err != nil { |
| 95 | BeeLogger.Critical("ListenAndServe: ", err) | 105 | BeeLogger.Critical("ListenAndServe: ", err) |
| 96 | time.Sleep(100 * time.Microsecond) | 106 | time.Sleep(100 * time.Microsecond) |
| 107 | endRunning <- true | ||
| 97 | } | 108 | } |
| 109 | }() | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | <-endRunning | ||
| 98 | } | 115 | } |
| 99 | 116 | ||
| 100 | // Router adds a url-patterned controller handler. | 117 | // Router adds a url-patterned controller handler. | ... | ... |
| ... | @@ -270,7 +270,7 @@ func initBeforeHttpRun() { | ... | @@ -270,7 +270,7 @@ func initBeforeHttpRun() { |
| 270 | sessionConfig = `{"cookieName":"` + SessionName + `",` + | 270 | sessionConfig = `{"cookieName":"` + SessionName + `",` + |
| 271 | `"gclifetime":` + strconv.FormatInt(SessionGCMaxLifetime, 10) + `,` + | 271 | `"gclifetime":` + strconv.FormatInt(SessionGCMaxLifetime, 10) + `,` + |
| 272 | `"providerConfig":"` + SessionSavePath + `",` + | 272 | `"providerConfig":"` + SessionSavePath + `",` + |
| 273 | `"secure":` + strconv.FormatBool(HttpTLS) + `,` + | 273 | `"secure":` + strconv.FormatBool(EnableHttpTLS) + `,` + |
| 274 | `"sessionIDHashFunc":"` + SessionHashFunc + `",` + | 274 | `"sessionIDHashFunc":"` + SessionHashFunc + `",` + |
| 275 | `"sessionIDHashKey":"` + SessionHashKey + `",` + | 275 | `"sessionIDHashKey":"` + SessionHashKey + `",` + |
| 276 | `"enableSetCookie":` + strconv.FormatBool(SessionAutoSetCookie) + `,` + | 276 | `"enableSetCookie":` + strconv.FormatBool(SessionAutoSetCookie) + `,` + | ... | ... |
| ... | @@ -30,9 +30,11 @@ var ( | ... | @@ -30,9 +30,11 @@ var ( |
| 30 | StaticDir map[string]string | 30 | StaticDir map[string]string |
| 31 | TemplateCache map[string]*template.Template // template caching map | 31 | TemplateCache map[string]*template.Template // template caching map |
| 32 | StaticExtensionsToGzip []string // files with should be compressed with gzip (.js,.css,etc) | 32 | StaticExtensionsToGzip []string // files with should be compressed with gzip (.js,.css,etc) |
| 33 | EnableHttpListen bool | ||
| 33 | HttpAddr string | 34 | HttpAddr string |
| 34 | HttpPort int | 35 | HttpPort int |
| 35 | HttpTLS bool | 36 | EnableHttpTLS bool |
| 37 | HttpsPort int | ||
| 36 | HttpCertFile string | 38 | HttpCertFile string |
| 37 | HttpKeyFile string | 39 | HttpKeyFile string |
| 38 | RecoverPanic bool // flag of auto recover panic | 40 | RecoverPanic bool // flag of auto recover panic |
| ... | @@ -98,9 +100,12 @@ func init() { | ... | @@ -98,9 +100,12 @@ func init() { |
| 98 | TemplateCache = make(map[string]*template.Template) | 100 | TemplateCache = make(map[string]*template.Template) |
| 99 | 101 | ||
| 100 | // set this to 0.0.0.0 to make this app available to externally | 102 | // set this to 0.0.0.0 to make this app available to externally |
| 103 | EnableHttpListen = true //default enable http Listen | ||
| 101 | HttpAddr = "" | 104 | HttpAddr = "" |
| 102 | HttpPort = 8080 | 105 | HttpPort = 8080 |
| 103 | 106 | ||
| 107 | HttpsPort = 443 | ||
| 108 | |||
| 104 | AppName = "beego" | 109 | AppName = "beego" |
| 105 | 110 | ||
| 106 | RunMode = "dev" //default runmod | 111 | RunMode = "dev" //default runmod |
| ... | @@ -176,6 +181,10 @@ func ParseConfig() (err error) { | ... | @@ -176,6 +181,10 @@ func ParseConfig() (err error) { |
| 176 | HttpPort = v | 181 | HttpPort = v |
| 177 | } | 182 | } |
| 178 | 183 | ||
| 184 | if v, err := AppConfig.Bool("EnableHttpListen"); err == nil { | ||
| 185 | EnableHttpListen = v | ||
| 186 | } | ||
| 187 | |||
| 179 | if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil { | 188 | if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil { |
| 180 | MaxMemory = maxmemory | 189 | MaxMemory = maxmemory |
| 181 | } | 190 | } |
| ... | @@ -281,8 +290,12 @@ func ParseConfig() (err error) { | ... | @@ -281,8 +290,12 @@ func ParseConfig() (err error) { |
| 281 | TemplateRight = tplright | 290 | TemplateRight = tplright |
| 282 | } | 291 | } |
| 283 | 292 | ||
| 284 | if httptls, err := AppConfig.Bool("HttpTLS"); err == nil { | 293 | if httptls, err := AppConfig.Bool("EnableHttpTLS"); err == nil { |
| 285 | HttpTLS = httptls | 294 | EnableHttpTLS = httptls |
| 295 | } | ||
| 296 | |||
| 297 | if httpsport, err := AppConfig.Int("HttpsPort"); err == nil { | ||
| 298 | HttpsPort = httpsport | ||
| 286 | } | 299 | } |
| 287 | 300 | ||
| 288 | if certfile := AppConfig.String("HttpCertFile"); certfile != "" { | 301 | if certfile := AppConfig.String("HttpCertFile"); certfile != "" { | ... | ... |
-
Please register or sign in to post a comment