mapserver/server/app/config.go

165 lines
3.7 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
)
type Config struct {
2019-02-22 21:13:47 +03:00
Port int `json:"port"`
EnablePrometheus bool `json:"enableprometheus"`
EnableRendering bool `json:"enablerendering"`
2019-02-27 19:36:30 +03:00
EnableTransparency bool `json:"enabletransparency"`
2019-02-22 21:13:47 +03:00
Webdev bool `json:"webdev"`
WebApi *WebApiConfig `json:"webapi"`
Layers []*layer.Layer `json:"layers"`
RenderingFetchLimit int `json:"renderingfetchlimit"`
RenderingJobs int `json:"renderingjobs"`
RenderingQueue int `json:"renderingqueue"`
MapObjects *MapObjectConfig `json:"mapobjects"`
MapBlockAccessorCfg *MapBlockAccessorConfig `json:"mapblockaccessor"`
}
type MapBlockAccessorConfig struct {
Expiretime string `json:"expiretime"`
Purgetime string `json:"purgetime"`
2019-02-12 15:40:33 +03:00
}
type MapObjectConfig struct {
2019-03-13 10:55:59 +03:00
Areas bool `json:"areas"`
2019-02-14 16:16:32 +03:00
Bones bool `json:"bones"`
Protector bool `json:"protector"`
XPProtector bool `json:"xpprotector"`
PrivProtector bool `json:"privprotector"`
2019-02-14 16:16:32 +03:00
Technic bool `json:"technic"`
LuaController bool `json:"luacontroller"`
Digiterms bool `json:"digiterms"`
Digilines bool `json:"digilines"`
Travelnet bool `json:"travelnet"`
Mapserver bool `json:"mapserver"`
2019-02-14 16:21:47 +03:00
Mission bool `json:"mission"`
Jumpdrive bool `json:"jumpdrive"`
Smartshop bool `json:"smartshop"`
Fancyvend bool `json:"fancyvend"`
ATM bool `json:"atm"`
2019-02-24 22:06:20 +03:00
Train bool `json:"train"`
2019-01-18 13:04:37 +03:00
}
type WebApiConfig struct {
2019-01-29 20:00:00 +03:00
//mapblock debugging
2019-01-18 13:04:37 +03:00
EnableMapblock bool `json:"enablemapblock"`
2019-01-29 20:00:00 +03:00
//mod http bridge secret
SecretKey string `json:"secretkey"`
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",
2019-01-21 13:13:00 +03:00
From: -16,
To: 160,
},
2019-02-25 15:01:15 +03:00
&layer.Layer{
Id: 1,
Name: "Sky",
From: 160,
To: 320,
},
2019-01-21 13:13:00 +03:00
}
2019-02-12 15:40:33 +03:00
mapobjs := MapObjectConfig{
2019-03-13 10:55:59 +03:00
Areas: true,
2019-02-14 16:16:32 +03:00
Bones: true,
Protector: true,
XPProtector: true,
PrivProtector: true,
2019-02-14 16:16:32 +03:00
Technic: true,
LuaController: true,
Digiterms: true,
Digilines: true,
Travelnet: true,
Mapserver: true,
2019-02-14 16:21:47 +03:00
Mission: true,
Jumpdrive: true,
Smartshop: true,
Fancyvend: true,
ATM: true,
2019-02-24 22:06:20 +03:00
Train: true,
2019-02-12 15:40:33 +03:00
}
2019-02-22 21:13:47 +03:00
mapblockaccessor := MapBlockAccessorConfig{
Expiretime: "500ms",
Purgetime: "1000ms",
}
2019-01-18 11:13:37 +03:00
cfg := Config{
2019-01-24 09:52:25 +03:00
Port: 8080,
EnableRendering: true,
2019-02-08 18:02:24 +03:00
EnablePrometheus: true,
2019-02-27 19:36:30 +03:00
EnableTransparency: true,
2019-01-24 09:52:25 +03:00
Webdev: false,
WebApi: &webapi,
Layers: layers,
RenderingFetchLimit: 10000,
2019-01-24 09:52:25 +03:00
RenderingJobs: runtime.NumCPU(),
RenderingQueue: 100,
2019-02-12 15:40:33 +03:00
MapObjects: &mapobjs,
2019-02-22 21:13:47 +03:00
MapBlockAccessorCfg: &mapblockaccessor,
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
}