Skip to content

Commit

Permalink
connect in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
cj123 committed Feb 18, 2019
1 parent 32ddcdd commit ad8bd8a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 23 deletions.
8 changes: 8 additions & 0 deletions accounts.go
@@ -1 +1,9 @@
package servermanager

type Account struct {
Name string `yaml:"name"`
Group Group `yaml:"group"`
PasswordMD5Hash string `yaml:"password"`
}

type Group string
16 changes: 13 additions & 3 deletions cmd/server-manager/config.yml
Expand Up @@ -31,6 +31,14 @@ steam:
# set this to true to force an install every time the server manager is loaded
force_update: false

################################################################################
#
# http settings
#
################################################################################
http:
hostname: localhost:8772

################################################################################
#
# data storage - where server manager keeps your data!
Expand Down Expand Up @@ -62,10 +70,12 @@ store:
# delete anything though.
#
# specify a username, and md5 hashed password for the user.
# you can generate passwords using sites such as: https://www.md5hashgenerator.com/
# you can generate password hashes using sites such as:
# https://www.md5hashgenerator.com/
#
# add as many users as you like.
users:
accounts:
- name: superadmin
group: admin
password: 21232f297a57a5a743894a0e4a801fc3 # 'admin' - DEFINITELY CHANGE THIS
Expand All @@ -76,5 +86,5 @@ users:
group: read
password: 4f321b12ec27a87579ae91ea3287cb33 # 'spectate' - CHANGE THIS

# set this to true to allow ANYONE to have read permissions
read_open: false
# set this to true to allow ANYONE to have read permissions
read_open: false
29 changes: 13 additions & 16 deletions cmd/server-manager/main.go
Expand Up @@ -2,32 +2,29 @@ package main

import (
"net/http"
"os"

"github.com/cj123/assetto-server-manager"
"github.com/etcd-io/bbolt"

"github.com/sirupsen/logrus"
)

var (
steamUsername = os.Getenv("STEAM_USERNAME")
steamPassword = os.Getenv("STEAM_PASSWORD")

serverAddress = os.Getenv("SERVER_ADDRESS")
func main() {
config, err := servermanager.ReadConfig("config.yml")

storeLocation = os.Getenv("STORE_LOCATION")
)
if err != nil {
logrus.Fatalf("could not open config file, err: %s", err)
}

func main() {
bboltdb, err := bbolt.Open(storeLocation, 0644, nil)
store, err := config.Store.BuildStore()

if err != nil {
logrus.Fatalf("could not open bbolt store at: '%s', err: %s", storeLocation, err)
logrus.Fatalf("could not open store, err: %s", err)
}

servermanager.SetupRaceManager(servermanager.NewBoltRaceStore(bboltdb))
servermanager.SetupRaceManager(store)
servermanager.SetAssettoInstallPath(config.Steam.InstallPath)

err = servermanager.InstallAssettoCorsaServer(steamUsername, steamPassword, os.Getenv("FORCE_UPDATE") == "true")
err = servermanager.InstallAssettoCorsaServer(config.Steam.Username, config.Steam.Password, config.Steam.ForceUpdate)

if err != nil {
logrus.Fatalf("could not install assetto corsa server, err: %s", err)
Expand All @@ -39,6 +36,6 @@ func main() {
logrus.Fatalf("could not initialise view renderer, err: %s", err)
}

logrus.Infof("starting assetto server manager on: %s", serverAddress)
logrus.Fatal(http.ListenAndServe(serverAddress, servermanager.Router()))
logrus.Infof("starting assetto server manager on: %s", config.HTTP.Hostname)
logrus.Fatal(http.ListenAndServe(config.HTTP.Hostname, servermanager.Router()))
}
Binary file added cmd/server-manager/server-manager
Binary file not shown.
2 changes: 1 addition & 1 deletion cmd/server-manager/views/layout/base.html
Expand Up @@ -61,7 +61,7 @@
</a>
<div class="dropdown-menu" aria-labelledby="navBarChampionshipsDropdown">
<a class="dropdown-item" href="/championships">View Existing</a>
<a class="dropdown-item" href="/championships/new">New</a>
<a class="dropdown-item" href="/championships/new">Create New</a>
</div>
</li>

Expand Down
8 changes: 5 additions & 3 deletions server_install.go
Expand Up @@ -22,15 +22,17 @@ var (
ServerConfigPath = "cfg"
)

func init() {
if !filepath.IsAbs(ServerInstallPath) {
func SetAssettoInstallPath(installPath string) {
if !filepath.IsAbs(installPath) {
wd, err := os.Getwd()

if err == nil {
ServerInstallPath = filepath.Join(wd, ServerInstallPath)
ServerInstallPath = filepath.Join(wd, installPath)
} else {
panic("unable to get working directory. can't install server")
}
} else {
ServerInstallPath = installPath
}
}

Expand Down
69 changes: 69 additions & 0 deletions servermanager_config.go
@@ -0,0 +1,69 @@
package servermanager

import (
"fmt"
"os"

"github.com/etcd-io/bbolt"
"gopkg.in/yaml.v2"
)

type Config struct {
HTTP HTTPConfig `yaml:"http"`
Steam SteamConfig `yaml:"steam"`
Store StoreConfig `yaml:"store"`
Users UsersConfig `yaml:"users"`
}

type HTTPConfig struct {
Hostname string `yaml:"hostname"`
}

type SteamConfig struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
InstallPath string `yaml:"install_path"`
ForceUpdate bool `yaml:"force_update"`
}

type StoreConfig struct {
Type string `yaml:"type"`
Path string `yaml:"path"`
}

func (s *StoreConfig) BuildStore() (RaceStore, error) {
switch s.Type {
case "boltdb":
bbdb, err := bbolt.Open(s.Path, 0644, nil)

if err != nil {
return nil, err
}

return NewBoltRaceStore(bbdb), nil
case "json":
return NewJSONRaceStore(s.Path), nil
default:
return nil, fmt.Errorf("invalid store type (%s), must be either boltdb/json", s.Type)
}
}

type UsersConfig struct {
Accounts []Account `yaml:"accounts"`

ReadOpen bool `yaml:"read_open"`
}

func ReadConfig(location string) (conf *Config, err error) {
f, err := os.Open(location)

if err != nil {
return nil, err
}

defer f.Close()

err = yaml.NewDecoder(f).Decode(&conf)

return conf, err
}

0 comments on commit ad8bd8a

Please sign in to comment.