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.
91 lines
2.1 KiB
91 lines
2.1 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gisle/stufflog/api"
|
|
"github.com/gisle/stufflog/config"
|
|
"github.com/gisle/stufflog/database"
|
|
"github.com/gisle/stufflog/services"
|
|
"github.com/urfave/cli"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
func main() {
|
|
configPath := "/etc/stufflog.yaml"
|
|
uiRoot := ""
|
|
|
|
app := cli.NewApp()
|
|
app.Name = "stufflog"
|
|
app.Usage = "Prioritize stuff, do stuff, log stuff"
|
|
|
|
app.Flags = []cli.Flag{
|
|
cli.StringFlag{
|
|
Name: "conf, c",
|
|
Value: configPath,
|
|
Usage: "Configuration file locations",
|
|
Destination: &configPath,
|
|
},
|
|
cli.StringFlag{
|
|
Name: "ui",
|
|
Value: uiRoot,
|
|
Usage: "UI Root (blank for no UI)",
|
|
Destination: &uiRoot,
|
|
},
|
|
}
|
|
|
|
app.Action = func(c *cli.Context) error {
|
|
// Load configuration
|
|
conf, err := config.Load(configPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load config: %s", err)
|
|
}
|
|
|
|
// Load database
|
|
db, err := database.Init(conf.Database)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to init database: %s", err)
|
|
}
|
|
|
|
// Setup gin
|
|
if !conf.Server.Debug {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
router := gin.New()
|
|
|
|
// Setup services
|
|
auth := services.NewAuthService(db)
|
|
scoring := services.NewScoringService(db)
|
|
|
|
// Setup APIs
|
|
api.User(router.Group("/api/user"), auth)
|
|
api.Activity(router.Group("/api/activity"), db, auth)
|
|
api.Period(router.Group("/api/period"), db, scoring, auth)
|
|
api.Items(router.Group("/api/item"), db, auth)
|
|
|
|
// Setup UI
|
|
if uiRoot != "" {
|
|
router.NoRoute(gin.WrapH(http.FileServer(http.Dir(uiRoot))))
|
|
router.GET("/activities/", func(c *gin.Context) {
|
|
http.ServeFile(c.Writer, c.Request, path.Join(uiRoot, "index.html"))
|
|
})
|
|
router.GET("/items/", func(c *gin.Context) {
|
|
http.ServeFile(c.Writer, c.Request, path.Join(uiRoot, "index.html"))
|
|
})
|
|
}
|
|
|
|
// Start listening
|
|
log.Println("Starting server on", conf.Server.Listen)
|
|
return router.Run(conf.Server.Listen)
|
|
|
|
// TODO: Handle interrupts because docker and sigint aren't the best of friends.
|
|
}
|
|
|
|
err := app.Run(os.Args)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|