esway/internal/opt/opt.go
2024-12-19 15:03:36 +08:00

63 lines
1.1 KiB
Go

package opt
import (
"encoding/json"
"fmt"
"os"
"esway/internal/tool"
"github.com/loveuer/nf/nft/log"
)
type listen struct {
Gateway string `json:"gateway"`
Dashboard string `json:"dashboard"`
}
type db struct {
Type string `json:"-"` // postgres, mysql, sqlite
Uri string `json:"uri"`
}
type cache struct {
Uri string `json:"uri"`
}
type config struct {
Name string `json:"name"`
Debug bool `json:"-"`
Dev bool `json:"-"`
Config string `json:"-"`
Listen listen `json:"listen"`
Endpoints []string `json:"endpoints"`
DB db `json:"db"`
Cache cache `json:"cache"`
}
var (
Mode string
Cfg = &config{}
)
func Init(filename string) error {
var (
err error
bs []byte
)
log.Info("opt.Init: start reading config file: %s", filename)
if bs, err = os.ReadFile(filename); err != nil {
return fmt.Errorf("opt.Init: read config file=%s err=%v", filename, err)
}
if err = json.Unmarshal(bs, Cfg); err != nil {
return fmt.Errorf("opt.Init: json marshal config=%s err=%v", string(bs), err)
}
tool.TablePrinter(Cfg)
return nil
}