mapserver/app/config.go

142 lines
2.8 KiB
Go
Raw Normal View History

2019-01-18 11:03:57 +03:00
package app
import (
2019-01-18 11:13:37 +03:00
"encoding/json"
"io/ioutil"
2019-01-21 13:13:00 +03:00
"mapserver/layer"
2019-01-18 11:13:37 +03:00
"os"
2019-01-23 18:29:14 +03:00
"runtime"
2019-01-24 09:52:25 +03:00
"sync"
2019-01-18 11:03:57 +03:00
)
2019-01-21 12:53:58 +03:00
var lock sync.Mutex
const ConfigFile = "mapserver.json"
func (cfg *Config) Save() error {
return WriteConfig(ConfigFile, cfg)
}
func WriteConfig(filename string, cfg *Config) error {
lock.Lock()
defer lock.Unlock()
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
str, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
f.Write(str)
return nil
}
2019-01-18 11:03:57 +03:00
func ParseConfig(filename string) (*Config, error) {
2019-01-18 13:04:37 +03:00
webapi := WebApiConfig{
EnableMapblock: false,
2019-01-29 20:00:00 +03:00
SecretKey: RandStringRunes(16),
2019-01-18 13:04:37 +03:00
}
2019-02-14 10:34:16 +03:00
layers := []*layer.Layer{
&layer.Layer{
2019-01-21 13:13:00 +03:00
Id: 0,
2019-02-25 15:01:15 +03:00
Name: "Ground",
From: -1,
To: 10,
2019-01-21 13:13:00 +03:00
},
2019-02-25 15:01:15 +03:00
&layer.Layer{
Id: 1,
Name: "Sky",
From: 11,
To: 24,
2019-02-25 15:01:15 +03:00
},
2019-01-21 13:13:00 +03:00
}
2019-02-12 15:40:33 +03:00
mapobjs := MapObjectConfig{
2019-04-04 13:50:40 +03:00
Areas: true,
Bones: true,
Protector: true,
XPProtector: true,
PrivProtector: true,
TechnicQuarry: true,
TechnicSwitch: true,
TechnicAnchor: true,
TechnicReactor: true,
LuaController: true,
Digiterms: true,
Digilines: true,
Travelnet: true,
MapserverPlayer: true,
MapserverPOI: true,
2019-06-11 12:57:48 +03:00
MapserverLabel: true,
2019-04-04 13:50:40 +03:00
MapserverTrainline: true,
2019-06-11 12:57:48 +03:00
MapserverBorder: true,
2019-04-04 13:50:40 +03:00
TileServerLegacy: true,
Mission: true,
Jumpdrive: true,
Smartshop: true,
Fancyvend: true,
ATM: true,
Train: true,
2019-07-30 14:44:12 +03:00
TrainSignal: true,
2019-04-24 17:21:04 +03:00
Minecart: false,
2019-05-08 16:50:51 +03:00
Locator: false,
Signs: true,
2019-02-12 15:40:33 +03:00
}
2019-02-22 21:13:47 +03:00
mapblockaccessor := MapBlockAccessorConfig{
2019-03-13 11:12:22 +03:00
Expiretime: "15s",
Purgetime: "30s",
2019-07-01 16:25:19 +03:00
MaxItems: 500,
2019-02-22 21:13:47 +03:00
}
2019-04-04 13:50:40 +03:00
defaultoverlays := []string{
"mapserver_poi",
"mapserver_label",
"mapserver_player",
}
2019-01-18 11:13:37 +03:00
cfg := Config{
2020-04-14 14:43:07 +03:00
ConfigVersion: 1,
Port: 8080,
EnableRendering: true,
EnablePrometheus: true,
EnableSearch: true,
EnableInitialRendering: true,
EnableTransparency: false,
EnableMediaRepository: false,
Webdev: false,
WebApi: &webapi,
Layers: layers,
RenderingFetchLimit: 10000,
RenderingJobs: runtime.NumCPU(),
RenderingQueue: 100,
IncrementalRenderingTimer: "5s",
MapObjects: &mapobjs,
MapBlockAccessorCfg: &mapblockaccessor,
DefaultOverlays: defaultoverlays,
2019-01-18 11:13:37 +03:00
}
2019-01-18 11:03:57 +03:00
2019-01-18 11:13:37 +03:00
info, err := os.Stat(filename)
if info != nil && err == nil {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
2019-01-18 11:03:57 +03:00
2019-01-18 11:13:37 +03:00
err = json.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}
}
2019-01-18 11:03:57 +03:00
2019-01-18 11:13:37 +03:00
return &cfg, nil
2019-01-18 11:03:57 +03:00
}