add a bunch of frontend stuff
This commit is contained in:
parent
2586161abd
commit
bc85b7c340
30 changed files with 1459 additions and 136 deletions
|
@ -1,8 +1,11 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"emperror.dev/errors"
|
||||
"git.sleepycat.moe/sam/mercury/config"
|
||||
"git.sleepycat.moe/sam/mercury/internal/database/sql"
|
||||
"git.sleepycat.moe/sam/mercury/web/templates"
|
||||
"github.com/flosch/pongo2/v6"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
@ -12,19 +15,27 @@ type App struct {
|
|||
|
||||
Config config.Config
|
||||
Database *sql.Base
|
||||
|
||||
tmpl *pongo2.TemplateSet
|
||||
}
|
||||
|
||||
func NewApp(cfg config.Config, db *sql.Base) *App {
|
||||
func NewApp(cfg config.Config, db *sql.Base) (*App, error) {
|
||||
app := &App{
|
||||
Router: chi.NewRouter(),
|
||||
Config: cfg,
|
||||
Database: db,
|
||||
}
|
||||
|
||||
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)
|
||||
app.Router.Use(middleware.Recoverer)
|
||||
|
||||
return app
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func (a *App) Account(q ...sql.Querier) *sql.AccountStore {
|
||||
|
|
52
web/app/template.go
Normal file
52
web/app/template.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/flosch/pongo2/v6"
|
||||
)
|
||||
|
||||
func (app *App) Template(w http.ResponseWriter, r *http.Request, tmplName string, ctx pongo2.Context) error {
|
||||
tmpl, err := app.tmpl.FromCache(tmplName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tctx := pongo2.Context{
|
||||
"flash_message": app.getFlash(w, r),
|
||||
}
|
||||
tctx.Update(ctx)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
return tmpl.ExecuteWriter(tctx, w)
|
||||
}
|
||||
|
||||
const flashCookieName = "mercury-flash-message"
|
||||
|
||||
func (app *App) Flash(w http.ResponseWriter, msg string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: flashCookieName,
|
||||
Value: msg,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Expires: time.Now().Add(time.Minute),
|
||||
})
|
||||
}
|
||||
|
||||
func (app *App) getFlash(w http.ResponseWriter, r *http.Request) string {
|
||||
cookie, err := r.Cookie(flashCookieName)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
defer http.SetCookie(w, &http.Cookie{
|
||||
Name: flashCookieName,
|
||||
Value: "",
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Expires: time.Now(),
|
||||
})
|
||||
|
||||
return cookie.Value
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue