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 12:53:58 +03:00
|
|
|
"mapserver/coords"
|
2019-01-21 13:13:00 +03:00
|
|
|
"mapserver/layer"
|
2019-01-18 11:13:37 +03:00
|
|
|
"os"
|
2019-01-21 12:53:58 +03:00
|
|
|
"sync"
|
2019-01-18 11:03:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2019-01-21 15:45:35 +03:00
|
|
|
Port int `json:"port"`
|
|
|
|
EnableInitialRendering bool `json:"enableinitialrendering"`
|
|
|
|
EnableIncrementalUpdate bool `json:"enableincrementalupdate"`
|
|
|
|
Webdev bool `json:"webdev"`
|
|
|
|
WebApi *WebApiConfig `json:"webapi"`
|
|
|
|
RenderState *RenderStateType `json:"renderstate"`
|
|
|
|
Layers []layer.Layer `json:"layers"`
|
|
|
|
InitialRenderingFetchLimit int `json:"initialrenderingfetchlimit"`
|
|
|
|
UpdateRenderingFetchLimit int `json:"updaterenderingfetchlimit"`
|
2019-01-18 13:04:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebApiConfig struct {
|
|
|
|
EnableMapblock bool `json:"enablemapblock"`
|
2019-01-18 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 12:53:58 +03:00
|
|
|
type RenderStateType struct {
|
|
|
|
//Initial rendering flag (true=still active)
|
2019-01-23 15:13:32 +03:00
|
|
|
InitialRun bool `json:"initialrun"`
|
|
|
|
LegacyProcessed int `json:"legacyprocessed"`
|
2019-01-21 12:53:58 +03:00
|
|
|
|
|
|
|
//Last initial rendering coords
|
|
|
|
LastX int `json:"lastx"`
|
|
|
|
LastY int `json:"lasty"`
|
|
|
|
LastZ int `json:"lastz"`
|
|
|
|
|
|
|
|
//Last mtime of incremental rendering
|
|
|
|
LastMtime int64 `json:"lastmtime"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 12:53:58 +03:00
|
|
|
rstate := RenderStateType{
|
|
|
|
InitialRun: true,
|
2019-01-21 15:45:35 +03:00
|
|
|
LastX: coords.MinCoord - 1,
|
|
|
|
LastY: coords.MinCoord - 1,
|
|
|
|
LastZ: coords.MinCoord - 1,
|
2019-01-21 13:18:27 +03:00
|
|
|
LastMtime: 1,
|
2019-01-21 12:53:58 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:13:00 +03:00
|
|
|
layers := []layer.Layer{
|
|
|
|
layer.Layer{
|
|
|
|
Id: 0,
|
|
|
|
Name: "Base",
|
|
|
|
From: -16,
|
|
|
|
To: 160,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:13:37 +03:00
|
|
|
cfg := Config{
|
2019-01-21 15:45:35 +03:00
|
|
|
Port: 8080,
|
|
|
|
EnableInitialRendering: true,
|
|
|
|
EnableIncrementalUpdate: true,
|
|
|
|
Webdev: false,
|
|
|
|
WebApi: &webapi,
|
|
|
|
RenderState: &rstate,
|
|
|
|
Layers: layers,
|
2019-01-21 15:55:20 +03:00
|
|
|
InitialRenderingFetchLimit: 1000,
|
2019-01-22 23:13:16 +03:00
|
|
|
UpdateRenderingFetchLimit: 1000,
|
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
|
|
|
}
|