2019-01-04 15:02:17 +03:00
|
|
|
package worldconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BACKEND_SQLITE3 string = "sqlite3"
|
|
|
|
BACKEND_FILES string = "files"
|
|
|
|
BACKEND_POSTGRES string = "postgresql"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CONFIG_BACKEND string = "backend"
|
|
|
|
CONFIG_PLAYER_BACKEND string = "player_backend"
|
2019-01-04 17:02:25 +03:00
|
|
|
CONFIG_PSQL_CONNECTION string = "pgsql_connection"
|
|
|
|
CONFIG_PSQL_PLAYER_CONNECTION string = "pgsql_player_connection"
|
2019-01-04 15:02:17 +03:00
|
|
|
)
|
2019-01-04 12:44:41 +03:00
|
|
|
|
2019-01-04 18:10:21 +03:00
|
|
|
type PsqlConfig struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
DbName string
|
|
|
|
}
|
|
|
|
|
2019-01-04 12:44:41 +03:00
|
|
|
type WorldConfig struct {
|
2019-01-04 15:02:17 +03:00
|
|
|
Backend string
|
|
|
|
PlayerBackend string
|
2019-01-04 17:02:25 +03:00
|
|
|
|
2019-01-04 18:10:21 +03:00
|
|
|
PsqlConnection *PsqlConfig
|
|
|
|
PsqlPlayerConnection *PsqlConfig
|
2019-01-04 17:02:25 +03:00
|
|
|
}
|
|
|
|
|
2019-01-04 18:10:21 +03:00
|
|
|
func parseConnectionString(str string) *PsqlConfig {
|
|
|
|
return &PsqlConfig{}
|
2019-01-04 12:44:41 +03:00
|
|
|
}
|
|
|
|
|
2019-01-04 15:02:17 +03:00
|
|
|
func Parse(filename string) WorldConfig {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
cfg := WorldConfig{}
|
2019-01-04 17:02:25 +03:00
|
|
|
|
2019-01-04 15:02:17 +03:00
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
sc := bufio.NewScanner(strings.NewReader(scanner.Text()))
|
|
|
|
sc.Split(bufio.ScanWords)
|
|
|
|
lastPart := ""
|
|
|
|
for sc.Scan() {
|
|
|
|
switch (lastPart) {
|
|
|
|
case CONFIG_BACKEND:
|
|
|
|
cfg.Backend = sc.Text()
|
|
|
|
case CONFIG_PLAYER_BACKEND:
|
|
|
|
cfg.PlayerBackend = sc.Text()
|
2019-01-04 17:02:25 +03:00
|
|
|
case CONFIG_PSQL_CONNECTION:
|
|
|
|
cfg.PsqlConnection = parseConnectionString(sc.Text())
|
|
|
|
case CONFIG_PSQL_PLAYER_CONNECTION:
|
|
|
|
cfg.PsqlPlayerConnection = parseConnectionString(sc.Text())
|
2019-01-04 15:02:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if sc.Text() != "=" {
|
|
|
|
lastPart = sc.Text()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 13:00:49 +03:00
|
|
|
|
2019-01-04 15:02:17 +03:00
|
|
|
return cfg
|
2019-01-04 13:00:49 +03:00
|
|
|
}
|