working commit
This commit is contained in:
+22
-17
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -16,8 +17,8 @@ const protocol = "tcp"
|
||||
|
||||
type ServiceParams struct {
|
||||
Handler *handler.Handler
|
||||
X509Cert []byte
|
||||
X509Key []byte
|
||||
X509Cert string
|
||||
X509Key string
|
||||
Portnum int64
|
||||
Address string
|
||||
}
|
||||
@@ -40,10 +41,10 @@ func NewService(params *ServiceParams) (*Service, error) {
|
||||
var err error
|
||||
svc := &Service{
|
||||
hand: params.Handler,
|
||||
x509cert: conf.X509Cert,
|
||||
x509key: conf.X509Key,
|
||||
portnum: conf.Portnum,
|
||||
address: conf.Address,
|
||||
x509cert: []byte(params.X509Cert),
|
||||
x509key: []byte(params.X509Key),
|
||||
portnum: params.Portnum,
|
||||
address: params.Address,
|
||||
}
|
||||
svc.logg = logger.NewLogger("service")
|
||||
return svc, err
|
||||
@@ -81,23 +82,19 @@ func (svc *Service) Build() error {
|
||||
}
|
||||
|
||||
listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
|
||||
svc.listen, err = tls.Listen(svc.protocol, listenAddress, &tlsConfig)
|
||||
svc.listen, err = tls.Listen(protocol, listenAddress, &tlsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
|
||||
svc.listen, err = net.Listen(svc.protocol, listenAddress)
|
||||
svc.listen, err = net.Listen(protocol, listenAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
|
||||
//svc.listen, err = net.Listen(protocol, listenAddress)
|
||||
|
||||
svc.log.Infof("Service listening at %v", svc.listen.Addr())
|
||||
|
||||
svc.logg.Infof("Service listening at %v", svc.listen.Addr())
|
||||
svc.hsrv = &http.Server{
|
||||
Handler: svc.rout,
|
||||
}
|
||||
@@ -108,6 +105,10 @@ func (svc *Service) Run() error {
|
||||
var err error
|
||||
svc.logg.Infof("Service run")
|
||||
err = svc.hsrv.Serve(svc.listen)
|
||||
if err == http.ErrServerClosed {
|
||||
svc.logg.Warningf("Service Closed")
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -115,10 +116,14 @@ func (svc *Service) Run() error {
|
||||
}
|
||||
|
||||
func (svc *Service) Stop() {
|
||||
svc.logg.Infof("Service stop")
|
||||
if svc.hsrv != nil {
|
||||
downWaiting := 5 * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), downWaiting)
|
||||
svc.hsrv.Shutdown(ctx)
|
||||
svc.logg.Infof("Service stop")
|
||||
downWaiting := 10 * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), downWaiting)
|
||||
defer cancel()
|
||||
err := svc.hsrv.Shutdown(ctx)
|
||||
if err != nil {
|
||||
svc.logg.Errorf("Error service shutdown: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user