diff --git a/app/app.go b/app/app.go index 0f14230..5c60f24 100644 --- a/app/app.go +++ b/app/app.go @@ -17,6 +17,7 @@ const ( type App struct { Params params.ParamsType + Config *Config Worldconfig worldconfig.WorldConfig Blockdb db.DBAccessor diff --git a/app/config.go b/app/config.go new file mode 100644 index 0000000..6caf74a --- /dev/null +++ b/app/config.go @@ -0,0 +1,32 @@ +package app + +import ( + "encoding/json" + "io/ioutil" + "os" +) + +type Config struct { + Port int `json:"port"` +} + +func ParseConfig(filename string) (*Config, error) { + cfg := Config{ + Port: 80, + } + + info, err := os.Stat(filename) + if info != nil && err == nil { + data, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } + + err = json.Unmarshal(data, &cfg) + if err != nil { + return nil, err + } + } + + return &cfg, nil +} diff --git a/app/setup.go b/app/setup.go index d7f5ba9..9dd1d3d 100644 --- a/app/setup.go +++ b/app/setup.go @@ -16,13 +16,14 @@ import ( "errors" ) -func Setup(p params.ParamsType) (*App, error) { +func Setup(p params.ParamsType, cfg *Config) (*App, error) { a := App{} a.Params = p + a.Config = cfg //Parse world config - a.Worldconfig = worldconfig.Parse(a.Params.Worlddir + "world.mt") + a.Worldconfig = worldconfig.Parse("world.mt") logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver") if a.Worldconfig.Backend != worldconfig.BACKEND_SQLITE3 { diff --git a/main.go b/main.go index 0c4af29..a6f11ee 100644 --- a/main.go +++ b/main.go @@ -29,9 +29,15 @@ func main() { return } + //parse Config + cfg, err := app.ParseConfig("mapserver.json") + if err != nil { + panic(err) + } + //setup app context - ctx, err := app.Setup(p) + ctx, err := app.Setup(p, cfg) if err != nil { //error case diff --git a/mapserver.json b/mapserver.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/mapserver.json @@ -0,0 +1 @@ +{} diff --git a/params/params.go b/params/params.go index 42d52da..29affe0 100644 --- a/params/params.go +++ b/params/params.go @@ -5,8 +5,6 @@ import ( ) type ParamsType struct { - Worlddir string - Port int Help bool Version bool } @@ -14,8 +12,6 @@ type ParamsType struct { func Parse() ParamsType { params := ParamsType{} - flag.StringVar(&(params.Worlddir), "worlddir", "./", "world directory") - flag.IntVar(&(params.Port), "port", 8080, "port to use") flag.BoolVar(&(params.Help), "help", false, "Show help") flag.BoolVar(&(params.Version), "version", false, "Show version") flag.Parse()