working commit

This commit is contained in:
2026-06-09 22:33:18 +02:00
parent 56a73e87b6
commit f61b9b87e2
5 changed files with 106 additions and 50 deletions
-19
View File
@@ -2,8 +2,6 @@ package config
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
@@ -102,23 +100,6 @@ func (conf *Config) ReadEnv() error {
func (conf *Config) ReadOpts() error {
var err error
exeName := filepath.Base(os.Args[0])
flag.BoolVar(&conf.AsDaemon, "asDaemon", conf.AsDaemon, "run as daemon")
flag.BoolVar(&conf.Debug, "debug", conf.Debug, "on debug mode")
help := func() {
fmt.Println("")
fmt.Printf("Usage: %s [option]\n", exeName)
fmt.Println("")
fmt.Println("Options:")
flag.PrintDefaults()
fmt.Println("")
}
flag.Usage = help
flag.Parse()
return err
}
+11 -29
View File
@@ -1,6 +1,7 @@
package server
import (
"context"
"fmt"
"io/ioutil"
"os"
@@ -8,10 +9,9 @@ import (
"os/user"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"
"context"
"sync"
"mbase/app/config"
"mbase/app/maindb"
@@ -41,40 +41,21 @@ type Server struct {
state descr.Server
sfile string
logf *os.File
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
func NewServer() (*Server, error) {
func NewServer(conf *config.Config) (*Server, error) {
var err error
srv := &Server{}
srv := &Server{
conf: conf,
}
srv.log = logger.NewLogger("server")
srv.ctx, srv.cancel = context.WithCancel(context.Background())
return srv, err
}
func (srv *Server) Configure() error {
var err error
srv.conf = config.NewConfig()
err = srv.conf.ReadFile()
if err != nil {
return err
}
err = srv.conf.ReadEnv()
if err != nil {
return err
}
err = srv.conf.ReadOpts()
if err != nil {
return err
}
srv.sfile = filepath.Join(srv.conf.DataDir, "mbase.yaml")
return err
}
func (srv *Server) LoadState() error {
var err error
_, err = os.Stat(srv.sfile)
@@ -125,6 +106,8 @@ func (srv *Server) Build() error {
var err error
srv.log.Infof("Build server")
srv.sfile = filepath.Join(srv.conf.DataDir, "mbase.yaml")
currUser, err := user.Current()
if err != nil {
err = fmt.Errorf("Error getting current user: %v\n", err)
@@ -458,7 +441,6 @@ func (srv *Server) Daemonize() error {
return err
}
func (srv *Server) Rotator() {
srv.wg.Add(1)
var counter uint64
+93
View File
@@ -0,0 +1,93 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package starter
import (
"os"
"path/filepath"
"mbase/app/config"
"mbase/app/server"
"github.com/spf13/cobra"
)
type Starter struct {
//runAsDaemon bool
//port uint32
cmd *cobra.Command
srv *server.Server
conf *config.Config
}
func NewStarter() *Starter {
execName := filepath.Base(os.Args[0])
sta := &Starter{}
cmd := &cobra.Command{
Use: execName,
Short: "\nService",
SilenceUsage: true,
RunE: sta.run,
}
cmd.CompletionOptions.DisableDefaultCmd = true
cmd.Flags().BoolVarP(&sta.conf.AsDaemon, "asDaemon", "D", sta.conf.AsDaemon, "Run service as daemon")
cmd.Flags().Uint32VarP(&sta.conf.Service.Portnum, "port", "P", sta.conf.Service.Portnum, "Service port")
sta.cmd = cmd
return sta
}
func (sta *Starter) GetCmd() *cobra.Command {
return sta.cmd
}
func (sta *Starter) run(cmd *cobra.Command, args []string) error {
var err error
err = sta.configure()
if err != nil {
return err
}
srv, err := server.NewServer(sta.conf)
if err != nil {
return err
}
err = srv.Daemonize()
if err != nil {
return err
}
err = srv.Build()
if err != nil {
return err
}
err = srv.Run()
if err != nil {
return err
}
sta.srv = srv
return err
}
func (sta *Starter) configure() error {
var err error
sta.conf = config.NewConfig()
err = sta.conf.ReadFile()
if err != nil {
return err
}
err = sta.conf.ReadEnv()
if err != nil {
return err
}
err = sta.conf.ReadOpts()
if err != nil {
return err
}
return err
}
func (sta *Starter) Exec() error {
return sta.cmd.Execute()
}