mstore server: moving creating listener from service to service

This commit is contained in:
2026-03-25 12:21:58 +02:00
parent 8e7a0fffa7
commit 229a3a97fc
6 changed files with 97 additions and 81 deletions
+6 -49
View File
@@ -11,8 +11,6 @@ package service
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"time"
@@ -22,26 +20,15 @@ import (
"mstore/app/router"
)
const protocol = "tcp"
type ServiceParams struct {
Handler *handler.Handler
X509Cert string
X509Key string
Portnum int64
Address string
Listener net.Listener
}
type Service struct {
hand *handler.Handler
rout *router.Router
logg *logger.Logger
address string
portnum int64
x509cert []byte
x509key []byte
hand *handler.Handler
rout *router.Router
logg *logger.Logger
listen net.Listener
hsrv *http.Server
}
@@ -49,11 +36,8 @@ type Service struct {
func NewService(params *ServiceParams) (*Service, error) {
var err error
svc := &Service{
hand: params.Handler,
x509cert: []byte(params.X509Cert),
x509key: []byte(params.X509Key),
portnum: params.Portnum,
address: params.Address,
hand: params.Handler,
listen: params.Listener,
}
svc.logg = logger.NewLoggerWithSubject("service")
return svc, err
@@ -126,33 +110,6 @@ func (svc *Service) Build() error {
svc.logg.Infof("%s\t%s", item.Method, item.RawPath)
}
const useTLS = true
if useTLS {
tlsCert, err := tls.X509KeyPair(svc.x509cert, svc.x509key)
if err != nil {
return err
}
tlsConfig := tls.Config{
Certificates: []tls.Certificate{tlsCert},
ClientAuth: tls.NoClientCert,
InsecureSkipVerify: true,
}
listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
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(protocol, listenAddress)
if err != nil {
return err
}
}
svc.logg.Infof("Service listening at %v", svc.listen.Addr())
svc.hsrv = &http.Server{
Handler: svc.rout,