The backend for the AiteStory website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

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
}
func (srv *server) Start() {
log.Println(fmt.Sprintf("Running: http://%s:%d", srv.Config.Server.Host, srv.Config.Server.Port))
_, err := srv.Router.Listen(srv.Config.Server.Host, srv.Config.Server.Port)
if err != nil {
log.Fatalf("Failed to init listen: %s", err)
}
}
// 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
}