mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-09-16 01:56:55 +00:00
Start of statsweb program
This commit is contained in:
parent
56b86ad4e3
commit
9fc946e373
7 changed files with 130 additions and 10 deletions
74
socketserver/cmd/statsweb/config.go
Normal file
74
socketserver/cmd/statsweb/config.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type ConfigFile struct {
|
||||
ListenAddr string
|
||||
DatabaseLocation string
|
||||
GobFilesLocation string
|
||||
}
|
||||
|
||||
func makeConfig() {
|
||||
config.ListenAddr = "localhost:3000"
|
||||
home, ok := os.LookupEnv("HOME")
|
||||
if ok {
|
||||
config.DatabaseLocation = fmt.Sprintf("%s/.ffzstatsweb/database.sqlite", home)
|
||||
config.GobFilesLocation = fmt.Sprintf("%s/.ffzstatsweb/gobcache", home)
|
||||
os.MkdirAll(config.GobFilesLocation, 0644)
|
||||
} else {
|
||||
config.DatabaseLocation = "./database.sqlite"
|
||||
config.GobFilesLocation = "./gobcache"
|
||||
os.MkdirAll(config.GobFilesLocation, 0644)
|
||||
}
|
||||
file, err := os.Create(*configLocation)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: could not create config file: %v\n", err)
|
||||
os.Exit(ExitCodeBadConfig)
|
||||
return
|
||||
}
|
||||
enc := json.NewEncoder(file)
|
||||
err = enc.Encode(config)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: could not write config file: %v\n", err)
|
||||
os.Exit(ExitCodeBadConfig)
|
||||
return
|
||||
}
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: could not write config file: %v\n", err)
|
||||
os.Exit(ExitCodeBadConfig)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func loadConfig() {
|
||||
file, err := os.Open(*configLocation)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("You must create a config file with -genconf")
|
||||
} else {
|
||||
fmt.Printf("Error: could not load config file: %v", err)
|
||||
}
|
||||
os.Exit(ExitCodeBadConfig)
|
||||
return
|
||||
}
|
||||
dec := json.NewDecoder(file)
|
||||
err = dec.Decode(&config)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: could not load config file: %v\n", err)
|
||||
os.Exit(ExitCodeBadConfig)
|
||||
return
|
||||
}
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: could not load config file: %v\n", err)
|
||||
os.Exit(ExitCodeBadConfig)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue