working commit
This commit is contained in:
+72
-8
@@ -1,10 +1,14 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
Service Service `json:"service" yaml:"service"`
|
||||
Database Database `json:"database" yaml:"database"`
|
||||
Storage Storage `json:"storage" yaml:"storage"`
|
||||
}
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
Address string `json:"address" yaml:"address"`
|
||||
@@ -19,8 +23,23 @@ type Storage struct {
|
||||
Basepath string `json:"basepath" yaml:"basepath"`
|
||||
}
|
||||
|
||||
func NewConfig() (*Config, error) {
|
||||
var err error
|
||||
type Config struct {
|
||||
Service Service `json:"service" yaml:"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"`
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
logfile := fmt.Sprintf("%s.log", srvname)
|
||||
logpath := filepath.Join(logdir, logfile)
|
||||
|
||||
runfile := fmt.Sprintf("%s.run", srvname)
|
||||
runpath := filepath.Join(rundir, runfile)
|
||||
|
||||
return &Config{
|
||||
Service: Service{
|
||||
Address: "0.0.0.0",
|
||||
@@ -32,5 +51,50 @@ func NewConfig() (*Config, error) {
|
||||
Storage: Storage{
|
||||
Basepath: datadir,
|
||||
},
|
||||
}, err
|
||||
AsDaemon: false,
|
||||
Logpath: logpath,
|
||||
Runpath: runpath,
|
||||
Version: version,
|
||||
}
|
||||
}
|
||||
|
||||
func (conf *Config) String() string {
|
||||
confbytes, _ := yaml.Marshal(conf)
|
||||
return string(confbytes)
|
||||
}
|
||||
|
||||
func (conf *Config) ReadConfigfile() error {
|
||||
conffile := fmt.Sprintf("%sd.yaml", srvname)
|
||||
confpath := filepath.Join(confdir, conffile)
|
||||
|
||||
confdata, err := ioutil.ReadFile(confpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = yaml.Unmarshal(confdata, conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (conf *Config) ReadOptions() error {
|
||||
var err error
|
||||
exename := filepath.Base(os.Args[0])
|
||||
|
||||
flag.Int64Var(&conf.Service.Port, "port", conf.Service.Port, "listen port")
|
||||
flag.BoolVar(&conf.AsDaemon, "daemon", conf.AsDaemon, "run as daemon")
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user