mercury/web/app/app.go

61 lines
1.3 KiB
Go
Raw Normal View History

2023-09-03 00:23:48 +02:00
package app
import (
2023-09-03 04:11:56 +02:00
"emperror.dev/errors"
2023-09-03 00:23:48 +02:00
"git.sleepycat.moe/sam/mercury/config"
"git.sleepycat.moe/sam/mercury/internal/database/sql"
2023-09-03 04:11:56 +02:00
"git.sleepycat.moe/sam/mercury/web/templates"
"github.com/flosch/pongo2/v6"
2023-09-03 00:23:48 +02:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
type App struct {
Router chi.Router
Config config.Config
Database *sql.Base
2023-09-03 04:11:56 +02:00
tmpl *pongo2.TemplateSet
2023-09-03 00:23:48 +02:00
}
2023-09-03 04:11:56 +02:00
func NewApp(cfg config.Config, db *sql.Base) (*App, error) {
2023-09-03 00:23:48 +02:00
app := &App{
Router: chi.NewRouter(),
Config: cfg,
Database: db,
}
2023-09-03 04:11:56 +02:00
tmpl, err := templates.New(cfg.Core.Dev)
if err != nil {
return nil, errors.Wrap(err, "creating templates")
}
app.tmpl = tmpl
2023-09-03 00:23:48 +02:00
app.Router.Use(app.Logger)
app.Router.Use(middleware.Recoverer)
2023-09-03 04:11:56 +02:00
return app, nil
2023-09-03 00:23:48 +02:00
}
func (a *App) Account(q ...sql.Querier) *sql.AccountStore {
if len(q) == 0 || q[0] == nil {
return sql.NewAccountStore(a.Database.PoolQuerier())
}
return sql.NewAccountStore(q[0])
}
func (a *App) Blog(q ...sql.Querier) *sql.BlogStore {
if len(q) == 0 || q[0] == nil {
return sql.NewBlogStore(a.Database.PoolQuerier())
}
return sql.NewBlogStore(q[0])
}
func (a *App) Post(q ...sql.Querier) *sql.PostStore {
if len(q) == 0 || q[0] == nil {
return sql.NewPostStore(a.Database.PoolQuerier())
}
return sql.NewPostStore(q[0])
}