146 lines
3.4 KiB
Go
146 lines
3.4 KiB
Go
package config
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"hamlogger/pkg/aux509"
|
|
|
|
"github.com/go-yaml/yaml"
|
|
)
|
|
|
|
const (
|
|
defaultHostname = "localhost"
|
|
defaultCertFile = "hamlogger.cert"
|
|
defaultKeyFile = "hamlogger.key"
|
|
applicationName = "hamlogger"
|
|
|
|
defaultPort int = 8081
|
|
)
|
|
|
|
var (
|
|
defaultBuild = "NODATE"
|
|
)
|
|
|
|
type ServiceConfig struct {
|
|
PortNum int `json:"port" yaml:"port"`
|
|
}
|
|
|
|
type Config struct {
|
|
Service ServiceConfig `json:"service" yaml:"service"`
|
|
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"`
|
|
DataPath string `json:"datadir" yaml:"datadir"`
|
|
SharePath string `json:"sharedir" yaml:"sharedir"`
|
|
Daemon bool `json:"daemon" yaml:"daemon"`
|
|
CertPath string `json:"certPath" yaml:"certPath"`
|
|
KeyPath string `json:"keyPath" yaml:"keyPath"`
|
|
X509Cert string `json:"x509Cert" yaml:"x509Cert"`
|
|
X509Key string `json:"x509Key" yaml:"x509Key"`
|
|
}
|
|
|
|
func NewConfig() *Config {
|
|
conf := &Config{
|
|
Service: ServiceConfig{
|
|
PortNum: defaultPort,
|
|
},
|
|
Hostname: defaultHostname,
|
|
Build: defaultBuild,
|
|
DataPath: datadirPath,
|
|
SharePath: sharedirPath,
|
|
Daemon: false,
|
|
Debug: false,
|
|
}
|
|
conf.LogPath = filepath.Join(logdirPath, fmt.Sprintf("%s.log", applicationName))
|
|
conf.RunPath = filepath.Join(rundirPath, fmt.Sprintf("%s.pid", applicationName))
|
|
return conf
|
|
}
|
|
|
|
func (conf *Config) ReadFile() error {
|
|
var err error
|
|
configPath := filepath.Join(confdirPath, fmt.Sprintf("%sd.yaml", applicationName))
|
|
confBytes, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = yaml.Unmarshal(confBytes, conf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (conf *Config) ReadEnv() error {
|
|
var err error
|
|
if conf.CertPath != "" && conf.KeyPath != "" {
|
|
if !filepath.IsAbs(conf.CertPath) {
|
|
conf.CertPath = filepath.Join(confdirPath, conf.CertPath)
|
|
}
|
|
certBytes, err := os.ReadFile(conf.CertPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !filepath.IsAbs(conf.KeyPath) {
|
|
conf.KeyPath = filepath.Join(confdirPath, conf.KeyPath)
|
|
}
|
|
keyBytes, err := os.ReadFile(conf.KeyPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
conf.X509Cert = string(certBytes)
|
|
conf.X509Key = string(keyBytes)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (conf *Config) ReadOpts() error {
|
|
var err error
|
|
exeName := filepath.Base(os.Args[0])
|
|
|
|
flag.IntVar(&conf.Service.PortNum, "port", conf.Service.PortNum, "listen port")
|
|
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) Postproc() error {
|
|
var err error
|
|
if conf.X509Cert == "" || conf.X509Key == "" {
|
|
certBytes, keyBytes, err := aux509.CreateX509Cert(conf.Hostname)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
conf.X509Cert = string(certBytes)
|
|
conf.X509Key = string(keyBytes)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (conf *Config) Yaml() (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
|
|
}
|