Plan stuff. Log stuff.
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.

87 lines
1.9 KiB

4 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/gisle/stufflog/api"
  6. "github.com/gisle/stufflog/config"
  7. "github.com/gisle/stufflog/database"
  8. "github.com/gisle/stufflog/services"
  9. "github.com/urfave/cli"
  10. "log"
  11. "net/http"
  12. "os"
  13. "path"
  14. )
  15. func main() {
  16. configPath := "/etc/stufflog.yaml"
  17. uiRoot := ""
  18. app := cli.NewApp()
  19. app.Name = "stufflog"
  20. app.Usage = "Prioritize stuff, do stuff, log stuff"
  21. app.Flags = []cli.Flag{
  22. cli.StringFlag{
  23. Name: "conf, c",
  24. Value: configPath,
  25. Usage: "Configuration file locations",
  26. Destination: &configPath,
  27. },
  28. cli.StringFlag{
  29. Name: "ui",
  30. Value: uiRoot,
  31. Usage: "UI Root (blank for no UI)",
  32. Destination: &uiRoot,
  33. },
  34. }
  35. app.Action = func(c *cli.Context) error {
  36. // Load configuration
  37. conf, err := config.Load(configPath)
  38. if err != nil {
  39. return fmt.Errorf("failed to load config: %s", err)
  40. }
  41. // Load database
  42. db, err := database.Init(conf.Database)
  43. if err != nil {
  44. return fmt.Errorf("failed to init database: %s", err)
  45. }
  46. // Setup gin
  47. if !conf.Server.Debug {
  48. gin.SetMode(gin.ReleaseMode)
  49. }
  50. router := gin.New()
  51. // Setup services
  52. auth := services.NewAuthService(db)
  53. scoring := services.NewScoringService(db)
  54. // Setup APIs
  55. api.User(router.Group("/api/user"), auth)
  56. api.Activity(router.Group("/api/activity"), db, auth)
  57. api.Period(router.Group("/api/period"), db, scoring, auth)
  58. // Setup UI
  59. if uiRoot != "" {
  60. router.NoRoute(gin.WrapH(http.FileServer(http.Dir(uiRoot))))
  61. router.GET("/activities/", func(c *gin.Context) {
  62. http.ServeFile(c.Writer, c.Request, path.Join(uiRoot, "index.html"))
  63. })
  64. }
  65. // Start listening
  66. log.Println("Starting server on", conf.Server.Listen)
  67. return router.Run(conf.Server.Listen)
  68. // TODO: Handle interrupts because docker and sigint aren't the best of friends.
  69. }
  70. err := app.Run(os.Args)
  71. if err != nil {
  72. log.Fatal(err)
  73. }
  74. }