init
This commit is contained in:
commit
2586161abd
49 changed files with 4171 additions and 0 deletions
49
web/app/app.go
Normal file
49
web/app/app.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"git.sleepycat.moe/sam/mercury/config"
|
||||
"git.sleepycat.moe/sam/mercury/internal/database/sql"
|
||||
"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
|
||||
}
|
||||
|
||||
func NewApp(cfg config.Config, db *sql.Base) *App {
|
||||
app := &App{
|
||||
Router: chi.NewRouter(),
|
||||
Config: cfg,
|
||||
Database: db,
|
||||
}
|
||||
|
||||
app.Router.Use(app.Logger)
|
||||
app.Router.Use(middleware.Recoverer)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
33
web/app/errors.go
Normal file
33
web/app/errors.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
// WriteError writes an error message to w.
|
||||
// If one or more messages are passed, a JSON response is also sent,
|
||||
// with the first message becoming `error` and the second becoming `error_description`.
|
||||
func (a *App) WriteError(w http.ResponseWriter, r *http.Request, status int, messages ...string) {
|
||||
render.Status(r, status)
|
||||
|
||||
switch len(messages) {
|
||||
case 0:
|
||||
return
|
||||
case 1:
|
||||
render.JSON(w, r, errorMessage{
|
||||
Error: messages[0],
|
||||
})
|
||||
default:
|
||||
render.JSON(w, r, errorMessage{
|
||||
Error: messages[0],
|
||||
ErrorDescription: messages[1],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type errorMessage struct {
|
||||
Error string `json:"error"`
|
||||
ErrorDescription string `json:"error_description,omitempty"`
|
||||
}
|
48
web/app/log.go
Normal file
48
web/app/log.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (a *App) Logger(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
|
||||
t1 := time.Now()
|
||||
defer func() {
|
||||
t2 := time.Now()
|
||||
|
||||
// Recover and record stack traces in case of a panic
|
||||
if rec := recover(); rec != nil {
|
||||
log.Error().
|
||||
Str("type", "error").
|
||||
Timestamp().
|
||||
Interface("recover_info", rec).
|
||||
Bytes("debug_stack", debug.Stack()).
|
||||
Msg("Handler panicked")
|
||||
http.Error(ww, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// log end request
|
||||
log.Info().
|
||||
Timestamp().
|
||||
Str("remote_ip", r.RemoteAddr).
|
||||
Str("url", r.URL.Path).
|
||||
Str("proto", r.Proto).
|
||||
Str("method", r.Method).
|
||||
Int("status", ww.Status()).
|
||||
Dur("elapsed", t2.Sub(t1)).
|
||||
Int64("bytes_in", r.ContentLength).
|
||||
Int("bytes_out", ww.BytesWritten()).
|
||||
Msg(r.Method + " " + r.URL.Path)
|
||||
}()
|
||||
|
||||
next.ServeHTTP(ww, r)
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
13
web/frontend/app.html
Normal file
13
web/frontend/app.html
Normal file
|
@ -0,0 +1,13 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Config.Name}}</title>
|
||||
|
||||
{{.Vue.RenderTags}}
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
</body>
|
||||
</html>
|
96
web/frontend/frontend.go
Normal file
96
web/frontend/frontend.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package frontend
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.sleepycat.moe/sam/mercury/frontend"
|
||||
"git.sleepycat.moe/sam/mercury/internal/database"
|
||||
"git.sleepycat.moe/sam/mercury/web/app"
|
||||
"github.com/rs/zerolog/log"
|
||||
vueglue "github.com/torenware/vite-go"
|
||||
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed app.html
|
||||
var htmlTemplate string
|
||||
|
||||
type Frontend struct {
|
||||
*app.App
|
||||
glue *vueglue.VueGlue
|
||||
urlPrefix string
|
||||
fileServer http.Handler
|
||||
tmpl *template.Template
|
||||
}
|
||||
|
||||
func New(app *app.App) *Frontend {
|
||||
fe := &Frontend{
|
||||
App: app,
|
||||
}
|
||||
|
||||
if app.Config.Core.Dev {
|
||||
glue, err := vueglue.NewVueGlue(&vueglue.ViteConfig{
|
||||
Environment: "development",
|
||||
AssetsPath: "frontend",
|
||||
EntryPoint: "src/main.ts",
|
||||
Platform: "svelte",
|
||||
FS: os.DirFS("frontend"),
|
||||
})
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Creating vite glue")
|
||||
os.Exit(1)
|
||||
}
|
||||
fe.glue = glue
|
||||
fe.urlPrefix = "/src/*"
|
||||
} else {
|
||||
glue, err := vueglue.NewVueGlue(&vueglue.ViteConfig{
|
||||
Environment: "production",
|
||||
URLPrefix: "/assets/",
|
||||
AssetsPath: "dist",
|
||||
EntryPoint: "src/main.ts",
|
||||
Platform: "svelte",
|
||||
FS: frontend.Embed,
|
||||
})
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Creating vite glue")
|
||||
os.Exit(1)
|
||||
}
|
||||
fe.glue = glue
|
||||
fe.urlPrefix = "/assets/*"
|
||||
}
|
||||
|
||||
fileServer, err := fe.glue.FileServer()
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Creating vite file server")
|
||||
os.Exit(1)
|
||||
}
|
||||
fe.fileServer = fileServer
|
||||
|
||||
tmpl, err := template.New("app").Parse(htmlTemplate)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Parsing frontend HTML template")
|
||||
os.Exit(1)
|
||||
}
|
||||
fe.tmpl = tmpl
|
||||
|
||||
return fe
|
||||
}
|
||||
|
||||
func (app *Frontend) AssetsPath() string { return app.urlPrefix }
|
||||
|
||||
func (app *Frontend) ServeAssets(w http.ResponseWriter, r *http.Request) {
|
||||
app.fileServer.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (app *Frontend) ServeFrontend(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := app.tmpl.Execute(w, map[string]any{
|
||||
"Config": database.DefaultConfig,
|
||||
"Vue": app.glue,
|
||||
})
|
||||
if err != nil {
|
||||
log.Err(err).Msg("executing frontend template")
|
||||
}
|
||||
}
|
13
web/routes.go
Normal file
13
web/routes.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"git.sleepycat.moe/sam/mercury/web/app"
|
||||
"git.sleepycat.moe/sam/mercury/web/frontend"
|
||||
)
|
||||
|
||||
func Routes(app *app.App) {
|
||||
frontend := frontend.New(app)
|
||||
app.Router.HandleFunc(frontend.AssetsPath(), frontend.ServeAssets)
|
||||
app.Router.HandleFunc("/web", frontend.ServeFrontend)
|
||||
app.Router.HandleFunc("/web/*", frontend.ServeFrontend)
|
||||
}
|
24
web/run.go
Normal file
24
web/run.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.sleepycat.moe/sam/mercury/web/app"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func Run(ctx context.Context, app *app.App, port int) {
|
||||
listenAddr := ":" + strconv.Itoa(port)
|
||||
|
||||
ech := make(chan error)
|
||||
go func() {
|
||||
ech <- http.ListenAndServe(listenAddr, app.Router)
|
||||
}()
|
||||
|
||||
log.Info().Int("port", port).Msg("Running API server")
|
||||
if err := <-ech; err != nil {
|
||||
log.Err(err).Msg("Running API server")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue