Add renderTemplate helper method on Handlers that renders templates to a bytes.Buffer first, then writes to the ResponseWriter only on success. This prevents partial/corrupt HTML when template execution fails partway through. Applied to all template rendering call sites in: - setup.go (HandleSetupGET, renderSetupError) - auth.go (HandleLoginGET, HandleLoginPOST error paths) - dashboard.go (HandleDashboard) - app.go (HandleAppNew, HandleAppCreate, HandleAppDetail, HandleAppEdit, HandleAppUpdate, HandleAppDeployments)
123 lines
3.0 KiB
Go
123 lines
3.0 KiB
Go
// Package handlers provides HTTP request handlers.
|
|
package handlers
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/csrf"
|
|
"go.uber.org/fx"
|
|
|
|
"git.eeqj.de/sneak/upaas/internal/database"
|
|
"git.eeqj.de/sneak/upaas/internal/docker"
|
|
"git.eeqj.de/sneak/upaas/internal/globals"
|
|
"git.eeqj.de/sneak/upaas/internal/healthcheck"
|
|
"git.eeqj.de/sneak/upaas/internal/logger"
|
|
"git.eeqj.de/sneak/upaas/internal/service/app"
|
|
"git.eeqj.de/sneak/upaas/internal/service/auth"
|
|
"git.eeqj.de/sneak/upaas/internal/service/deploy"
|
|
"git.eeqj.de/sneak/upaas/internal/service/webhook"
|
|
"git.eeqj.de/sneak/upaas/templates"
|
|
)
|
|
|
|
// Params contains dependencies for Handlers.
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Logger *logger.Logger
|
|
Globals *globals.Globals
|
|
Database *database.Database
|
|
Healthcheck *healthcheck.Healthcheck
|
|
Auth *auth.Service
|
|
App *app.Service
|
|
Deploy *deploy.Service
|
|
Webhook *webhook.Service
|
|
Docker *docker.Client
|
|
}
|
|
|
|
// Handlers provides HTTP request handlers.
|
|
type Handlers struct {
|
|
log *slog.Logger
|
|
params *Params
|
|
db *database.Database
|
|
hc *healthcheck.Healthcheck
|
|
auth *auth.Service
|
|
appService *app.Service
|
|
deploy *deploy.Service
|
|
webhook *webhook.Service
|
|
docker *docker.Client
|
|
globals *globals.Globals
|
|
}
|
|
|
|
// New creates a new Handlers instance.
|
|
func New(_ fx.Lifecycle, params Params) (*Handlers, error) {
|
|
return &Handlers{
|
|
log: params.Logger.Get(),
|
|
params: ¶ms,
|
|
db: params.Database,
|
|
hc: params.Healthcheck,
|
|
auth: params.Auth,
|
|
appService: params.App,
|
|
deploy: params.Deploy,
|
|
webhook: params.Webhook,
|
|
docker: params.Docker,
|
|
globals: params.Globals,
|
|
}, nil
|
|
}
|
|
|
|
// addGlobals adds version info and CSRF token to template data map.
|
|
func (h *Handlers) addGlobals(
|
|
data map[string]any,
|
|
request *http.Request,
|
|
) map[string]any {
|
|
data["Version"] = h.globals.Version
|
|
data["Appname"] = h.globals.Appname
|
|
|
|
if request != nil {
|
|
data["CSRFField"] = csrf.TemplateField(request)
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
// renderTemplate executes the named template into a buffer first, then writes
|
|
// to the ResponseWriter only on success. This prevents partial/corrupt HTML
|
|
// responses when template execution fails partway through.
|
|
func (h *Handlers) renderTemplate(
|
|
writer http.ResponseWriter,
|
|
tmpl *templates.TemplateExecutor,
|
|
name string,
|
|
data any,
|
|
) {
|
|
var buf bytes.Buffer
|
|
|
|
err := tmpl.ExecuteTemplate(&buf, name, data)
|
|
if err != nil {
|
|
h.log.Error("template execution failed", "error", err)
|
|
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
}
|
|
|
|
_, _ = buf.WriteTo(writer)
|
|
}
|
|
|
|
func (h *Handlers) respondJSON(
|
|
writer http.ResponseWriter,
|
|
_ *http.Request,
|
|
data any,
|
|
status int,
|
|
) {
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
writer.WriteHeader(status)
|
|
|
|
if data != nil {
|
|
err := json.NewEncoder(writer).Encode(data)
|
|
if err != nil {
|
|
h.log.Error("json encode error", "error", err)
|
|
}
|
|
}
|
|
}
|