be6ad15efc
Security: apply AdminOnly middleware to mutating routes, require ENCRYPTION_KEY and ADMIN_PASSWORD (no insecure defaults), restrict CORS to same-origin, fix OIDC token delivery via cookie instead of URL query param, add rate limiting on login, add MaxBytesReader, validate volume paths against traversal, add security headers, validate user roles, add Secure flag to OIDC cookie. Performance: set SQLite MaxOpenConns(1) to prevent SQLITE_BUSY, add FK indexes on 8 columns, track notifier goroutines with WaitGroup for graceful shutdown, use GetRegistryByName instead of GetAllRegistries in deployer, pass basePath param to avoid redundant settings query, return empty slices from store to remove reflection. Quality: refactor TriggerDeploy to delegate to runDeploy (~100 lines removed), consolidate duplicated utilities (extractPort, boolToInt, now, isTerminalStatus) into shared exports, migrate all log.Printf to slog structured logging, use consistent webhook response envelope, remove dead code (parseEnvVars, duplicate auth types). UX: clean up NPM proxy on instance removal via API, add README with quickstart guide, add .env.example, require ADMIN_PASSWORD in docker-compose, document staging-net prerequisite.
48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
)
|
|
|
|
// envelope is the standard API response wrapper.
|
|
type envelope struct {
|
|
Success bool `json:"success"`
|
|
Data any `json:"data,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// respondJSON writes a JSON success response with the given status code and data.
|
|
func respondJSON(w http.ResponseWriter, status int, data any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if err := json.NewEncoder(w).Encode(envelope{Success: true, Data: data}); err != nil {
|
|
slog.Error("encode response", "error", err)
|
|
}
|
|
}
|
|
|
|
// respondError writes a JSON error response with the given status code and message.
|
|
func respondError(w http.ResponseWriter, status int, msg string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if err := json.NewEncoder(w).Encode(envelope{Success: false, Error: msg}); err != nil {
|
|
slog.Error("encode error response", "error", err)
|
|
}
|
|
}
|
|
|
|
// respondNotFound writes a 404 JSON error response for the given entity type.
|
|
func respondNotFound(w http.ResponseWriter, entity string) {
|
|
respondError(w, http.StatusNotFound, entity+" not found")
|
|
}
|
|
|
|
// decodeJSON reads and decodes the request body into the given value.
|
|
// Returns false and writes a 400 error response if decoding fails.
|
|
func decodeJSON(w http.ResponseWriter, r *http.Request, v any) bool {
|
|
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
|
|
respondError(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
|
return false
|
|
}
|
|
return true
|
|
}
|