chore: remove facebook graceful shutdown package. (#408)
* chore: remove facebook graceful shutdown package. Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -2,6 +2,8 @@ package gorush
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
@@ -124,3 +126,66 @@ func routerEngine() *gin.Engine {
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// RunHTTPServer provide run http or https protocol.
|
||||
func RunHTTPServer() (err error) {
|
||||
if !PushConf.Core.Enabled {
|
||||
LogAccess.Debug("httpd server is disabled.")
|
||||
return nil
|
||||
}
|
||||
|
||||
server := &http.Server{
|
||||
Addr: PushConf.Core.Address + ":" + PushConf.Core.Port,
|
||||
Handler: routerEngine(),
|
||||
}
|
||||
|
||||
LogAccess.Debug("HTTPD server is running on " + PushConf.Core.Port + " port.")
|
||||
if PushConf.Core.AutoTLS.Enabled {
|
||||
return startServer(autoTLSServer())
|
||||
} else if PushConf.Core.SSL {
|
||||
config := &tls.Config{
|
||||
MinVersion: tls.VersionTLS10,
|
||||
}
|
||||
|
||||
if config.NextProtos == nil {
|
||||
config.NextProtos = []string{"http/1.1"}
|
||||
}
|
||||
|
||||
config.Certificates = make([]tls.Certificate, 1)
|
||||
if PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" {
|
||||
config.Certificates[0], err = tls.LoadX509KeyPair(PushConf.Core.CertPath, PushConf.Core.KeyPath)
|
||||
if err != nil {
|
||||
LogError.Error("Failed to load https cert file: ", err)
|
||||
return err
|
||||
}
|
||||
} else if PushConf.Core.CertBase64 != "" && PushConf.Core.KeyBase64 != "" {
|
||||
cert, err := base64.StdEncoding.DecodeString(PushConf.Core.CertBase64)
|
||||
if err != nil {
|
||||
LogError.Error("base64 decode error:", err.Error())
|
||||
return err
|
||||
}
|
||||
key, err := base64.StdEncoding.DecodeString(PushConf.Core.KeyBase64)
|
||||
if err != nil {
|
||||
LogError.Error("base64 decode error:", err.Error())
|
||||
return err
|
||||
}
|
||||
if config.Certificates[0], err = tls.X509KeyPair(cert, key); err != nil {
|
||||
LogError.Error("tls key pair error:", err.Error())
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.New("missing https cert config")
|
||||
}
|
||||
|
||||
server.TLSConfig = config
|
||||
}
|
||||
|
||||
return startServer(server)
|
||||
}
|
||||
|
||||
func startServer(s *http.Server) error {
|
||||
if s.TLSConfig == nil {
|
||||
return s.ListenAndServe()
|
||||
}
|
||||
return s.ListenAndServeTLS("", "")
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
// +build !windows,!lambda
|
||||
|
||||
package gorush
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/facebookgo/grace/gracehttp"
|
||||
)
|
||||
|
||||
// RunHTTPServer provide run http or https protocol.
|
||||
func RunHTTPServer() (err error) {
|
||||
if !PushConf.Core.Enabled {
|
||||
LogAccess.Debug("httpd server is disabled.")
|
||||
return nil
|
||||
}
|
||||
|
||||
LogAccess.Debug("HTTPD server is running on " + PushConf.Core.Port + " port.")
|
||||
if PushConf.Core.AutoTLS.Enabled {
|
||||
err = gracehttp.Serve(autoTLSServer())
|
||||
} else if PushConf.Core.SSL {
|
||||
config := &tls.Config{
|
||||
MinVersion: tls.VersionTLS10,
|
||||
}
|
||||
|
||||
if config.NextProtos == nil {
|
||||
config.NextProtos = []string{"http/1.1"}
|
||||
}
|
||||
|
||||
config.Certificates = make([]tls.Certificate, 1)
|
||||
if PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" {
|
||||
config.Certificates[0], err = tls.LoadX509KeyPair(PushConf.Core.CertPath, PushConf.Core.KeyPath)
|
||||
if err != nil {
|
||||
LogError.Error("Failed to load https cert file: ", err)
|
||||
return err
|
||||
}
|
||||
} else if PushConf.Core.CertBase64 != "" && PushConf.Core.KeyBase64 != "" {
|
||||
cert, err := base64.StdEncoding.DecodeString(PushConf.Core.CertBase64)
|
||||
if err != nil {
|
||||
LogError.Error("base64 decode error:", err.Error())
|
||||
return err
|
||||
}
|
||||
key, err := base64.StdEncoding.DecodeString(PushConf.Core.KeyBase64)
|
||||
if err != nil {
|
||||
LogError.Error("base64 decode error:", err.Error())
|
||||
return err
|
||||
}
|
||||
config.Certificates[0], err = tls.X509KeyPair(cert, key)
|
||||
} else {
|
||||
return errors.New("missing https cert config")
|
||||
}
|
||||
|
||||
err = gracehttp.Serve(&http.Server{
|
||||
Addr: PushConf.Core.Address + ":" + PushConf.Core.Port,
|
||||
Handler: routerEngine(),
|
||||
TLSConfig: config,
|
||||
})
|
||||
} else {
|
||||
err = gracehttp.Serve(&http.Server{
|
||||
Addr: PushConf.Core.Address + ":" + PushConf.Core.Port,
|
||||
Handler: routerEngine(),
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
// +build windows,!lambda
|
||||
|
||||
package gorush
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// RunHTTPServer provide run http or https protocol.
|
||||
func RunHTTPServer() (err error) {
|
||||
if !PushConf.Core.Enabled {
|
||||
LogAccess.Debug("httpd server is disabled.")
|
||||
return nil
|
||||
}
|
||||
|
||||
if PushConf.Core.AutoTLS.Enabled {
|
||||
s := autoTLSServer()
|
||||
err = s.ListenAndServeTLS("", "")
|
||||
} else if PushConf.Core.SSL && PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" {
|
||||
err = http.ListenAndServeTLS(PushConf.Core.Address+":"+PushConf.Core.Port, PushConf.Core.CertPath, PushConf.Core.KeyPath, routerEngine())
|
||||
} else {
|
||||
err = http.ListenAndServe(PushConf.Core.Address+":"+PushConf.Core.Port, routerEngine())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user