Initial Commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Config is the struct created by the server's config.json
|
||||
type Config struct {
|
||||
View struct {
|
||||
Title string `json:"title"`
|
||||
} `json:"view"`
|
||||
|
||||
DB struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Database string `json:"database"`
|
||||
} `json:"db"`
|
||||
|
||||
Server struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
UI string `json:"ui"`
|
||||
} `json:"server"`
|
||||
|
||||
Wiki struct {
|
||||
URL string `json:"url"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
} `json:"Wiki"`
|
||||
|
||||
Test struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
} `json:"Test"`
|
||||
}
|
||||
|
||||
// Load loads the config file from the paths, starting with the first path.
|
||||
// If it fails to load any of them, it will return false.
|
||||
func (config *Config) Load(paths ...string) error {
|
||||
for _, path := range paths {
|
||||
// Open a file, or continue if it doesn't exist
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// JSON parsing errors should not cause it to skip to the next file.
|
||||
// That's given me enough grief in the past because JSON is a fickle
|
||||
// format for human-editable config files
|
||||
err = json.NewDecoder(file).Decode(&config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("No configuration files found in either: " + strings.Join(paths, ","))
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.aiterp.net/gisle/wrouter"
|
||||
|
||||
// The SQL driver is used in Server.Init()
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// UserAgent is what the server will appear as when connecting to
|
||||
// an external service
|
||||
const UserAgent = "AiteStory/0.1.0 (story.aiterp.net, https://git.aiterp.net/AiteRP/aitestory)"
|
||||
|
||||
type server struct {
|
||||
DB *sql.DB
|
||||
Listener *http.Server
|
||||
Router wrouter.Router
|
||||
Config Config
|
||||
}
|
||||
|
||||
// Main is the main instance
|
||||
var Main = server{}
|
||||
|
||||
func init() {
|
||||
wd, _ := os.Getwd()
|
||||
err := Main.Config.Load(
|
||||
"/etc/aiterp/aitestory.json",
|
||||
path.Join(os.Getenv("HOME"), ".config/aiterp/aitestory.json"),
|
||||
path.Join(wd, "aitestory.json"),
|
||||
path.Join(wd, "../aitestory.json"),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalln("server.init:", err)
|
||||
}
|
||||
|
||||
dbConfig := Main.Config.DB
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/%s?charset=utf8", dbConfig.Username, dbConfig.Password, dbConfig.Database))
|
||||
if err != nil || db == nil {
|
||||
log.Fatalln("server.init:", err)
|
||||
}
|
||||
Main.DB = db
|
||||
}
|
||||
Reference in New Issue
Block a user