parsimony/internal/app/app.go
tgiachi 5a0dd7f6d6 feat(adminui): embed the admin SPA and serve it next to the API
- Add internal/adminui with a SPA handler: index fallback for client routes, immutable caching for hashed assets, 404 for missing assets, security headers
- Answer 503 with a hint when the UI has not been built, so go build always works
- Mount the UI at / and the API at /api/ on the same listener, no CORS needed
- Scaffold admin/ with Vite, React, TypeScript, Tailwind v4, shadcn/ui and Vitest
2026-11-09 15:29:12 +01:00

153 lines
3.8 KiB
Go

// Package app wires the store, router, LMTP server and REST API together.
package app
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/emersion/go-smtp"
"codeberg.org/squidd/parsimony/internal/adminui"
"codeberg.org/squidd/parsimony/internal/api"
"codeberg.org/squidd/parsimony/internal/config"
"codeberg.org/squidd/parsimony/internal/ingest"
"codeberg.org/squidd/parsimony/internal/outbound"
"codeberg.org/squidd/parsimony/internal/router"
"codeberg.org/squidd/parsimony/internal/store"
"codeberg.org/squidd/parsimony/internal/web"
)
// App is a running daemon.
type App struct {
// LMTPAddr and APIAddr are the addresses actually bound.
// APIAddr is empty when the API is disabled.
LMTPAddr string
APIAddr string
WebAddr string // empty when the public web is disabled
store *store.Store
lmtp *smtp.Server
lns []net.Listener // closed by Close so Serve returns even if Close wins the race with it
web *http.Server
pub *http.Server
wg sync.WaitGroup
once sync.Once
}
// releaser lets the API approve held messages through the router.
type releaser struct {
rt *router.Router
out outbound.Sender
}
func (r releaser) Release(ctx context.Context, id int64) error { return r.rt.Release(ctx, r.out, id) }
// Start opens the database and begins serving. The REST API runs only when
// apiToken is not empty.
func Start(cfg config.Config, apiToken string) (*App, error) {
st, err := store.Open(cfg.DB)
if err != nil {
return nil, fmt.Errorf("store: %w", err)
}
a := &App{store: st}
rt := router.New(st)
rt.BaseURL = cfg.BaseURL
relay := &outbound.SMTPRelay{Addr: cfg.Relay.Addr}
a.lmtp = ingest.NewServer(rt, relay)
if cfg.LMTP.Network == "unix" {
os.Remove(cfg.LMTP.Addr)
}
lmtpLn, err := net.Listen(cfg.LMTP.Network, cfg.LMTP.Addr)
if err != nil {
st.Close()
return nil, fmt.Errorf("listen lmtp: %w", err)
}
a.LMTPAddr = lmtpLn.Addr().String()
a.lns = append(a.lns, lmtpLn)
var apiLn net.Listener
if apiToken != "" {
apiLn, err = net.Listen("tcp", cfg.API.Listen)
if err != nil {
lmtpLn.Close()
st.Close()
return nil, fmt.Errorf("listen api: %w", err)
}
a.APIAddr = apiLn.Addr().String()
a.lns = append(a.lns, apiLn)
// The admin UI and the API share a listener, so the UI needs no CORS.
mux := http.NewServeMux()
mux.Handle("/api/", api.New(st, apiToken, releaser{rt, relay}))
mux.Handle("/", adminui.Embedded())
a.web = &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second}
}
var pubLn net.Listener
if cfg.Web.Listen != "" {
pubLn, err = net.Listen("tcp", cfg.Web.Listen)
if err != nil {
for _, ln := range a.lns {
ln.Close()
}
st.Close()
return nil, fmt.Errorf("listen web: %w", err)
}
a.WebAddr = pubLn.Addr().String()
a.lns = append(a.lns, pubLn)
a.pub = &http.Server{Handler: web.New(st, rt), ReadHeaderTimeout: 10 * time.Second}
}
a.wg.Add(1)
go func() {
defer a.wg.Done()
if err := a.lmtp.Serve(lmtpLn); err != nil && !errors.Is(err, smtp.ErrServerClosed) && !errors.Is(err, net.ErrClosed) {
log.Printf("lmtp: %v", err)
}
}()
if a.web != nil {
a.wg.Add(1)
go func() {
defer a.wg.Done()
if err := a.web.Serve(apiLn); err != nil && !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, net.ErrClosed) {
log.Printf("api: %v", err)
}
}()
}
if a.pub != nil {
a.wg.Add(1)
go func() {
defer a.wg.Done()
if err := a.pub.Serve(pubLn); err != nil && !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, net.ErrClosed) {
log.Printf("web: %v", err)
}
}()
}
return a, nil
}
// Close stops the servers and closes the database. It is safe to call twice.
func (a *App) Close() {
a.once.Do(func() {
for _, ln := range a.lns {
ln.Close()
}
a.lmtp.Close()
if a.web != nil {
a.web.Close()
}
if a.pub != nil {
a.pub.Close()
}
a.wg.Wait()
a.store.Close()
})
}