mercury/web/app/app.go

113 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-09-03 00:23:48 +02:00
package app
import (
2023-09-04 03:33:13 +02:00
"context"
"encoding/base64"
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"
2023-09-04 03:33:13 +02:00
"git.sleepycat.moe/sam/mercury/internal/concurrent"
"git.sleepycat.moe/sam/mercury/internal/database"
2023-09-03 00:23:48 +02:00
"git.sleepycat.moe/sam/mercury/internal/database/sql"
2023-10-15 17:08:55 +02:00
"git.sleepycat.moe/sam/mercury/internal/processor"
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"
)
2023-09-04 03:33:13 +02:00
const (
ErrSecretKeyEmpty = errors.Sentinel("core.secret_key cannot be empty")
)
2023-09-03 00:23:48 +02:00
type App struct {
Router chi.Router
2023-09-04 03:33:13 +02:00
AppConfig config.Config
DBConfig *concurrent.Value[database.Config]
Database *sql.Base
2023-10-15 17:08:55 +02:00
Processor *processor.Processor
2023-09-03 04:11:56 +02:00
2023-09-04 03:33:13 +02:00
tmpl *pongo2.TemplateSet
tokenKey []byte
2023-09-03 00:23:48 +02:00
}
2023-09-04 03:33:13 +02:00
func NewApp(ctx context.Context, cfg config.Config, db *sql.Base) (*App, error) {
2023-09-03 00:23:48 +02:00
app := &App{
2023-09-04 03:33:13 +02:00
Router: chi.NewRouter(),
AppConfig: cfg,
Database: db,
2023-10-15 17:08:55 +02:00
Processor: processor.New(db),
2023-09-04 03:33:13 +02:00
}
if cfg.Core.SecretKey == "" {
return nil, ErrSecretKeyEmpty
2023-09-03 00:23:48 +02:00
}
2023-09-04 03:33:13 +02:00
tokenKey, err := base64.RawStdEncoding.DecodeString(cfg.Core.SecretKey)
if err != nil {
return nil, errors.Wrap(err, "decoding core.secret_key")
}
app.tokenKey = tokenKey
2023-09-03 00:23:48 +02:00
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-04 03:33:13 +02:00
if cfg.Core.Dev {
app.Router.Use(app.Logger)
}
2023-09-03 00:23:48 +02:00
app.Router.Use(middleware.Recoverer)
2023-09-04 03:33:13 +02:00
dbCfg, err := app.Config().Get(ctx)
if err != nil {
return nil, errors.Wrap(err, "getting database config")
}
app.DBConfig = concurrent.NewValue(dbCfg)
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])
}
2023-09-04 03:33:13 +02:00
func (a *App) Config(q ...sql.Querier) *sql.ConfigStore {
if len(q) == 0 || q[0] == nil {
return sql.NewConfigStore(a.Database.PoolQuerier())
}
return sql.NewConfigStore(q[0])
}
2023-09-03 00:23:48 +02:00
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])
}
2023-09-04 03:33:13 +02:00
func (a *App) Timeline(q ...sql.Querier) *sql.TimelineStore {
if len(q) == 0 || q[0] == nil {
return sql.NewTimelineStore(a.Database.PoolQuerier())
}
return sql.NewTimelineStore(q[0])
}
2023-09-04 03:33:13 +02:00
func (a *App) Token(q ...sql.Querier) *sql.TokenStore {
if len(q) == 0 || q[0] == nil {
return sql.NewTokenStore(a.Database.PoolQuerier())
}
return sql.NewTokenStore(q[0])
}