working commit
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"mbase/pkg/client"
|
||||
|
||||
"go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultHostname = "localhost"
|
||||
|
||||
configFilename = "maind.yaml"
|
||||
logFilename = "maind.log"
|
||||
pidFilename = "maind.pid"
|
||||
)
|
||||
|
||||
var (
|
||||
buildVersion = "NONE"
|
||||
)
|
||||
|
||||
type Networks struct {
|
||||
Enabled []string `json:"enabled" yaml:"enabled"`
|
||||
Disabled []string `json:"disabled" yaml:"disabled"`
|
||||
}
|
||||
|
||||
type ServiceConfig struct {
|
||||
Portnum uint32 `json:"port" yaml:"port"`
|
||||
Address string `json:"address" yaml:"address"`
|
||||
Protocol string `json:"protocol" yaml:"protocol"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
PackageVersion string `json:"packageVersion" yaml:"packageVersion"`
|
||||
Service ServiceConfig `json:"service" yaml:"service"`
|
||||
Networks Networks `json:"networks" yaml:"networks"`
|
||||
Hostname string `json:"hostname" yaml:"hostname"`
|
||||
Debug bool `json:"debug" yaml:"debug"`
|
||||
Build string `json:"build" yaml:"build"`
|
||||
LogPath string `json:"logfile" yaml:"logfile"`
|
||||
RunPath string `json:"runfile" yaml:"runfile"`
|
||||
DataDir string `json:"datadir" yaml:"datadir"`
|
||||
Daemon bool `json:"daemon" yaml:"daemon"`
|
||||
}
|
||||
|
||||
var (
|
||||
defaultEnabledNetworks = []string{"0.0.0.0/0", "::/0"}
|
||||
defaultDisabledNetworks = []string{}
|
||||
)
|
||||
|
||||
const (
|
||||
defaultServiceAddress = "0.0.0.0"
|
||||
defaultServiceProtocol = "tcp"
|
||||
)
|
||||
|
||||
func NewConfig() *Config {
|
||||
conf := &Config{
|
||||
Service: ServiceConfig{
|
||||
Portnum: client.DefaultPort,
|
||||
Address: defaultServiceAddress,
|
||||
Protocol: defaultServiceProtocol,
|
||||
},
|
||||
DataDir: datadirPath,
|
||||
Debug: false,
|
||||
Hostname: defaultHostname,
|
||||
Build: buildVersion,
|
||||
Daemon: true,
|
||||
PackageVersion: packageVersion,
|
||||
|
||||
Networks: Networks{
|
||||
Enabled: defaultEnabledNetworks,
|
||||
Disabled: defaultDisabledNetworks,
|
||||
},
|
||||
}
|
||||
conf.LogPath = filepath.Join(logdirPath, logFilename)
|
||||
conf.RunPath = filepath.Join(rundirPath, pidFilename)
|
||||
return conf
|
||||
}
|
||||
|
||||
func (conf *Config) ReadFile() error {
|
||||
var err error
|
||||
confPath := filepath.Join(confdirPath, configFilename)
|
||||
confBytes, err := os.ReadFile(confPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = yaml.Unmarshal(confBytes, conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf.Normalize()
|
||||
err = conf.Validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (conf *Config) ReadEnv() error {
|
||||
var err error
|
||||
return err
|
||||
}
|
||||
|
||||
func (conf *Config) ReadOpts() error {
|
||||
var err error
|
||||
|
||||
exeName := filepath.Base(os.Args[0])
|
||||
|
||||
flag.BoolVar(&conf.Daemon, "daemon", conf.Daemon, "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
|
||||
}
|
||||
|
||||
func (conf *Config) String() (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
yamlBytes, err := yaml.Marshal(conf)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res = string(yamlBytes)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (conf *Config) Normalize() {
|
||||
if conf.Service.Portnum == 0 {
|
||||
conf.Service.Portnum = client.DefaultPort
|
||||
}
|
||||
}
|
||||
|
||||
func (conf *Config) Validate() error {
|
||||
var err []error
|
||||
return errors.Join(err...)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
const (
|
||||
confdirPath = "@srv_confdir@"
|
||||
rundirPath = "@srv_rundir@"
|
||||
logdirPath = "@srv_logdir@"
|
||||
datadirPath = "@srv_datadir@"
|
||||
packageVersion = "@PACKAGE_VERSION@"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user