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.

49 lines
1.0 KiB

7 years ago
  1. package server
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "path"
  9. "git.aiterp.net/gisle/wrouter"
  10. // The SQL driver is used in Server.Init()
  11. _ "github.com/go-sql-driver/mysql"
  12. )
  13. // UserAgent is what the server will appear as when connecting to
  14. // an external service
  15. const UserAgent = "AiteStory/0.1.0 (story.aiterp.net, https://git.aiterp.net/AiteRP/aitestory)"
  16. type server struct {
  17. DB *sql.DB
  18. Listener *http.Server
  19. Router wrouter.Router
  20. Config Config
  21. }
  22. // Main is the main instance
  23. var Main = server{}
  24. func init() {
  25. wd, _ := os.Getwd()
  26. err := Main.Config.Load(
  27. "/etc/aiterp/aitestory.json",
  28. path.Join(os.Getenv("HOME"), ".config/aiterp/aitestory.json"),
  29. path.Join(wd, "aitestory.json"),
  30. path.Join(wd, "../aitestory.json"),
  31. )
  32. if err != nil {
  33. log.Fatalln("server.init:", err)
  34. }
  35. dbConfig := Main.Config.DB
  36. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/%s?charset=utf8", dbConfig.Username, dbConfig.Password, dbConfig.Database))
  37. if err != nil || db == nil {
  38. log.Fatalln("server.init:", err)
  39. }
  40. Main.DB = db
  41. }