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

7 years ago
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. func (srv *server) Start() {
  23. log.Println(fmt.Sprintf("Running: http://%s:%d", srv.Config.Server.Host, srv.Config.Server.Port))
  24. _, err := srv.Router.Listen(srv.Config.Server.Host, srv.Config.Server.Port)
  25. if err != nil {
  26. log.Fatalf("Failed to init listen: %s", err)
  27. }
  28. }
  29. // Main is the main instance
  30. var Main = server{}
  31. func init() {
  32. wd, _ := os.Getwd()
  33. err := Main.Config.Load(
  34. "/etc/aiterp/aitestory.json",
  35. path.Join(os.Getenv("HOME"), ".config/aiterp/aitestory.json"),
  36. path.Join(wd, "aitestory.json"),
  37. path.Join(wd, "../aitestory.json"),
  38. )
  39. if err != nil {
  40. log.Fatalln("server.init:", err)
  41. }
  42. dbConfig := Main.Config.DB
  43. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/%s?charset=utf8", dbConfig.Username, dbConfig.Password, dbConfig.Database))
  44. if err != nil || db == nil {
  45. log.Fatalln("server.init:", err)
  46. }
  47. Main.DB = db
  48. }