2023-09-03 00:23:48 +02:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"emperror.dev/errors"
|
|
|
|
"git.sleepycat.moe/sam/mercury/config"
|
|
|
|
"git.sleepycat.moe/sam/mercury/internal/database/sql"
|
|
|
|
"git.sleepycat.moe/sam/mercury/web"
|
|
|
|
"git.sleepycat.moe/sam/mercury/web/app"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Command = &cli.Command{
|
|
|
|
Name: "web",
|
|
|
|
Usage: "Run the Mercury web server",
|
|
|
|
Action: run,
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(c *cli.Context) error {
|
|
|
|
log.Debug().Msg("Reading configuration")
|
|
|
|
cfg, err := config.Parse("config.toml")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "reading configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Bool("dev", cfg.Core.Dev).Str("domain", cfg.Web.Domain).Msg("Starting server")
|
|
|
|
|
|
|
|
if cfg.Core.Dev {
|
|
|
|
log.Info().Msg("Running migrations")
|
|
|
|
err = sql.Migrate(cfg.Core.Postgres)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "performing migrations")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msg("Connecting to database")
|
|
|
|
db, err := sql.NewBase(c.Context, cfg.Core.Postgres)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating postgres database")
|
|
|
|
}
|
|
|
|
|
2023-09-04 03:33:13 +02:00
|
|
|
a, err := app.NewApp(c.Context, cfg, db)
|
2023-09-03 04:11:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating app")
|
|
|
|
}
|
2023-09-03 00:23:48 +02:00
|
|
|
|
|
|
|
log.Debug().Msg("Mounting routes")
|
|
|
|
web.Routes(a)
|
|
|
|
|
|
|
|
web.Run(c.Context, a, cfg.Web.Port)
|
|
|
|
return nil
|
|
|
|
}
|