add working signup + login
This commit is contained in:
parent
bc85b7c340
commit
d8cb8c8fa8
27 changed files with 600 additions and 39 deletions
|
@ -1,8 +1,13 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"git.sleepycat.moe/sam/mercury/config"
|
||||
"git.sleepycat.moe/sam/mercury/internal/concurrent"
|
||||
"git.sleepycat.moe/sam/mercury/internal/database"
|
||||
"git.sleepycat.moe/sam/mercury/internal/database/sql"
|
||||
"git.sleepycat.moe/sam/mercury/web/templates"
|
||||
"github.com/flosch/pongo2/v6"
|
||||
|
@ -10,31 +15,54 @@ import (
|
|||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrSecretKeyEmpty = errors.Sentinel("core.secret_key cannot be empty")
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Router chi.Router
|
||||
|
||||
Config config.Config
|
||||
Database *sql.Base
|
||||
AppConfig config.Config
|
||||
DBConfig *concurrent.Value[database.Config]
|
||||
Database *sql.Base
|
||||
|
||||
tmpl *pongo2.TemplateSet
|
||||
tmpl *pongo2.TemplateSet
|
||||
tokenKey []byte
|
||||
}
|
||||
|
||||
func NewApp(cfg config.Config, db *sql.Base) (*App, error) {
|
||||
func NewApp(ctx context.Context, cfg config.Config, db *sql.Base) (*App, error) {
|
||||
app := &App{
|
||||
Router: chi.NewRouter(),
|
||||
Config: cfg,
|
||||
Database: db,
|
||||
Router: chi.NewRouter(),
|
||||
AppConfig: cfg,
|
||||
Database: db,
|
||||
}
|
||||
|
||||
if cfg.Core.SecretKey == "" {
|
||||
return nil, ErrSecretKeyEmpty
|
||||
}
|
||||
tokenKey, err := base64.RawStdEncoding.DecodeString(cfg.Core.SecretKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "decoding core.secret_key")
|
||||
}
|
||||
app.tokenKey = tokenKey
|
||||
|
||||
tmpl, err := templates.New(cfg.Core.Dev)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "creating templates")
|
||||
}
|
||||
app.tmpl = tmpl
|
||||
|
||||
app.Router.Use(app.Logger)
|
||||
if cfg.Core.Dev {
|
||||
app.Router.Use(app.Logger)
|
||||
}
|
||||
app.Router.Use(middleware.Recoverer)
|
||||
|
||||
dbCfg, err := app.Config().Get(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "getting database config")
|
||||
}
|
||||
app.DBConfig = concurrent.NewValue(dbCfg)
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
|
@ -52,9 +80,23 @@ func (a *App) Blog(q ...sql.Querier) *sql.BlogStore {
|
|||
return sql.NewBlogStore(q[0])
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue