2019-10-27 03:13:49 +00:00
|
|
|
// +build !lambda
|
|
|
|
|
|
|
|
package gorush
|
|
|
|
|
|
|
|
import (
|
2020-02-04 09:10:12 +00:00
|
|
|
"context"
|
2019-10-27 03:13:49 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2020-02-04 09:10:12 +00:00
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
2019-10-27 03:13:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RunHTTPServer provide run http or https protocol.
|
2020-02-04 09:10:12 +00:00
|
|
|
func RunHTTPServer(ctx context.Context) (err error) {
|
2019-10-27 03:13:49 +00:00
|
|
|
if !PushConf.Core.Enabled {
|
2020-02-04 09:10:12 +00:00
|
|
|
LogAccess.Info("httpd server is disabled.")
|
2019-10-27 03:13:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: PushConf.Core.Address + ":" + PushConf.Core.Port,
|
|
|
|
Handler: routerEngine(),
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:27:27 +00:00
|
|
|
LogAccess.Info("HTTPD server is running on " + PushConf.Core.Port + " port.")
|
2019-10-27 03:13:49 +00:00
|
|
|
if PushConf.Core.AutoTLS.Enabled {
|
2020-02-04 09:10:12 +00:00
|
|
|
return startServer(ctx, autoTLSServer())
|
2019-10-27 03:13:49 +00:00
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
2020-02-04 09:10:12 +00:00
|
|
|
return startServer(ctx, server)
|
2019-10-27 03:13:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 09:10:12 +00:00
|
|
|
func listenAndServe(ctx context.Context, s *http.Server) error {
|
|
|
|
var g errgroup.Group
|
|
|
|
g.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return s.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
g.Go(func() error {
|
2019-10-27 03:13:49 +00:00
|
|
|
return s.ListenAndServe()
|
2020-02-04 09:10:12 +00:00
|
|
|
})
|
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func listenAndServeTLS(ctx context.Context, s *http.Server) error {
|
|
|
|
var g errgroup.Group
|
|
|
|
g.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return s.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
g.Go(func() error {
|
|
|
|
return s.ListenAndServeTLS("", "")
|
|
|
|
})
|
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func startServer(ctx context.Context, s *http.Server) error {
|
|
|
|
if s.TLSConfig == nil {
|
|
|
|
return listenAndServe(ctx, s)
|
2019-10-27 03:13:49 +00:00
|
|
|
}
|
2020-02-04 09:10:12 +00:00
|
|
|
|
|
|
|
return listenAndServeTLS(ctx, s)
|
2019-10-27 03:13:49 +00:00
|
|
|
}
|