working commit

This commit is contained in:
2026-01-30 13:40:42 +02:00
parent 95aa086327
commit c4172ac1f8
5 changed files with 163 additions and 42 deletions
+12 -11
View File
@@ -26,17 +26,17 @@ type Storage struct {
}
type Config struct {
Service Service `json:"service" yaml:"service"`
Service Service `json:"service"`
Database Database `json:"database" yaml:"database"`
Storage Storage `json:"storage" yaml:"storage"`
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
Logpath string `json:"logpath" yaml:"logpath"`
Runpath string `json:"runpath" yaml:"runpath"`
Version string `json:"version" yaml:"version"`
Certpath string `json:"certpath" yaml:"certath"`
Keypath string `json:"keypath" yaml:"keypath"`
X509Cert string `json:"X509cert" yaml:"X509cert"`
X509Key string `json:"X509key" yaml:"X509key"`
Certpath string `json:"certpath,omitempty" yaml:"certath"`
Keypath string `json:"keypath,omitempty" yaml:"keypath"`
X509Cert string `json:"-" yaml:"-"`
X509Key string `json:"-" yaml:"-"`
}
func NewConfig() *Config {
@@ -46,11 +46,11 @@ func NewConfig() *Config {
runfile := fmt.Sprintf("%s.run", srvname)
runpath := filepath.Join(rundir, runfile)
certpath := fmt.Sprintf("%s.crt", srvname)
certpath = filepath.Join(confdir, certpath)
//certpath := fmt.Sprintf("%s.crt", srvname)
//certpath = filepath.Join(confdir, certpath)
keypath := fmt.Sprintf("%s.crt", srvname)
keypath = filepath.Join(confdir, keypath)
//keypath := fmt.Sprintf("%s.crt", srvname)
//keypath = filepath.Join(confdir, keypath)
return &Config{
Service: Service{
@@ -67,8 +67,9 @@ func NewConfig() *Config {
Logpath: logpath,
Runpath: runpath,
Version: version,
Certpath: certpath,
Keypath: keypath,
//Certpath: certpath,
//Keypath: keypath,
}
}
+24 -13
View File
@@ -44,7 +44,7 @@ func (srv *Server) Handler() *handler.Handler {
func (srv *Server) Configure() error {
var err error
srv.logg.Infof("Server configure")
srv.logg.Infof("Configuration server")
srv.conf = config.NewConfig()
if err != nil {
return err
@@ -54,6 +54,11 @@ func (srv *Server) Configure() error {
//srv.logg.Warningf("Error loading config file: %v", err)
//return err
}
err = srv.conf.ReadX509Cert()
if err != nil {
return err
}
err = srv.conf.ReadOptions()
if err != nil {
return err
@@ -83,7 +88,7 @@ func (srv *Server) Build() error {
}
}
// Database create
// Creating database
dbdir := srv.conf.Database.Basepath
srv.logg.Infof("Create database directory %s ", dbdir)
err = os.MkdirAll(dbdir, 0750)
@@ -104,19 +109,20 @@ func (srv *Server) Build() error {
}
srv.mdb = mdb
// Storage create
// Creating storage
srv.logg.Infof("Create storage directory")
datadir := srv.conf.Database.Basepath
err = os.MkdirAll(datadir, 0750)
if err != nil {
return err
}
srv.logg.Infof("Create storage")
srv.logg.Infof("Creating storage")
store := storage.NewStorage(datadir)
srv.stor = store
// Operator create
srv.logg.Infof("Create operator")
// Creating operator
srv.logg.Infof("Creating operator")
operatorParams := &operator.OperatorParams{
MainDB: srv.mdb,
Store: srv.stor,
@@ -125,8 +131,8 @@ func (srv *Server) Build() error {
if err != nil {
return err
}
// Handler create
srv.logg.Infof("Create handler")
// Creating handler
srv.logg.Infof("Creating handler")
handlerParams := &handler.HandlerParams{
Operator: srv.oper,
}
@@ -134,16 +140,20 @@ func (srv *Server) Build() error {
if err != nil {
return err
}
// Service create
// Creating service
serviceParams := &service.ServiceParams{
Handler: srv.hand,
Handler: srv.hand,
X509Cert: srv.conf.X509Cert,
X509Key: srv.conf.X509Key,
Address: srv.conf.Service.Address,
Portnum: srv.conf.Service.Port,
}
srv.logg.Infof("Create service")
srv.logg.Infof("Creating service")
srv.svc, err = service.NewService(serviceParams)
if err != nil {
return err
}
// Service build
// Building service
err = srv.svc.Build()
if err != nil {
return err
@@ -161,7 +171,6 @@ func (srv *Server) Run() error {
}
srv.logg.Infof("Server run as user %s", currUser.Username)
sigs := make(chan os.Signal, 1)
svcDone := make(chan error, 1)
// Service run
@@ -175,6 +184,8 @@ func (srv *Server) Run() error {
}
go startService(srv.svc, svcDone)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var signal os.Signal
+56
View File
@@ -0,0 +1,56 @@
package server
import (
"context"
"sync"
"testing"
"time"
"mstore/pkg/client"
"github.com/stretchr/testify/require"
)
func TestService(t *testing.T) {
srv, err := NewServer()
require.NoError(t, err)
{
err = srv.Configure()
require.NoError(t, err)
err = srv.Build()
require.NoError(t, err)
var svcWG sync.WaitGroup
errPipe := make(chan error, 5)
startFunc := func() {
err := srv.svc.Run()
errPipe <- err
svcWG.Done()
}
stopFunc := func() {
srv.svc.Stop()
svcWG.Wait()
err = <-errPipe
require.NoError(t, err)
}
defer stopFunc()
svcWG.Add(1)
go startFunc()
time.Sleep(1 * time.Second)
}
{
cli := client.NewClient()
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
helloRes, err := cli.ServiceHello(ctx, "127.0.0.1:1025/hello")
require.NoError(t, err)
require.True(t, helloRes)
}
}
+22 -17
View File
@@ -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)
}
}
}