1
0
forked from MTSR/mapserver

json config

This commit is contained in:
NatureFreshMilk 2019-01-18 09:03:57 +01:00
parent 6943f10f59
commit 0a13747950
6 changed files with 44 additions and 7 deletions

View File

@ -17,6 +17,7 @@ const (
type App struct { type App struct {
Params params.ParamsType Params params.ParamsType
Config *Config
Worldconfig worldconfig.WorldConfig Worldconfig worldconfig.WorldConfig
Blockdb db.DBAccessor Blockdb db.DBAccessor

32
app/config.go Normal file
View File

@ -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
}

View File

@ -16,13 +16,14 @@ import (
"errors" "errors"
) )
func Setup(p params.ParamsType) (*App, error) { func Setup(p params.ParamsType, cfg *Config) (*App, error) {
a := App{} a := App{}
a.Params = p a.Params = p
a.Config = cfg
//Parse world config //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") logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver")
if a.Worldconfig.Backend != worldconfig.BACKEND_SQLITE3 { if a.Worldconfig.Backend != worldconfig.BACKEND_SQLITE3 {

View File

@ -29,9 +29,15 @@ func main() {
return return
} }
//parse Config
cfg, err := app.ParseConfig("mapserver.json")
if err != nil {
panic(err)
}
//setup app context //setup app context
ctx, err := app.Setup(p) ctx, err := app.Setup(p, cfg)
if err != nil { if err != nil {
//error case //error case

1
mapserver.json Normal file
View File

@ -0,0 +1 @@
{}

View File

@ -5,8 +5,6 @@ import (
) )
type ParamsType struct { type ParamsType struct {
Worlddir string
Port int
Help bool Help bool
Version bool Version bool
} }
@ -14,8 +12,6 @@ type ParamsType struct {
func Parse() ParamsType { func Parse() ParamsType {
params := 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.Help), "help", false, "Show help")
flag.BoolVar(&(params.Version), "version", false, "Show version") flag.BoolVar(&(params.Version), "version", false, "Show version")
flag.Parse() flag.Parse()