397 lines
12 KiB
Go
397 lines
12 KiB
Go
// Path: Backend/internal/httpx/api_bootstrap_routes.go
|
|
|
|
package httpx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
bootstrapservice "moku-backend/internal/bootstrap"
|
|
)
|
|
|
|
type bootstrapInstanceStepRequest struct {
|
|
Protocol string `json:"protocol"`
|
|
Access string `json:"access"`
|
|
Host string `json:"host"`
|
|
}
|
|
|
|
type bootstrapModeStepRequest struct {
|
|
Mode string `json:"mode"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type bootstrapAdminStepRequest struct {
|
|
DisplayName string `json:"displayName"`
|
|
Email string `json:"email"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
type bootstrapStructureStepRequest struct {
|
|
OrganizationName string `json:"organizationName"`
|
|
DepartmentName string `json:"departmentName"`
|
|
TeamName string `json:"teamName"`
|
|
ProjectName string `json:"projectName"`
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapOverview(w http.ResponseWriter, _ *http.Request) {
|
|
WriteJSON(w, http.StatusOK, map[string]any{
|
|
"data": map[string]any{
|
|
"resource": "bootstrap",
|
|
"status": "persisted",
|
|
"steps": []map[string]string{
|
|
{
|
|
"id": "instance",
|
|
"method": http.MethodPost,
|
|
"path": "/v1/bootstrap/steps/instance",
|
|
},
|
|
{
|
|
"id": "mode",
|
|
"method": http.MethodPost,
|
|
"path": "/v1/bootstrap/steps/mode",
|
|
},
|
|
{
|
|
"id": "admin",
|
|
"method": http.MethodPost,
|
|
"path": "/v1/bootstrap/steps/admin",
|
|
},
|
|
{
|
|
"id": "structure",
|
|
"method": http.MethodPost,
|
|
"path": "/v1/bootstrap/steps/structure",
|
|
},
|
|
{
|
|
"id": "installation",
|
|
"method": http.MethodGet,
|
|
"path": "/v1/bootstrap/installation",
|
|
},
|
|
{
|
|
"id": "admin-state",
|
|
"method": http.MethodGet,
|
|
"path": "/v1/bootstrap/admin",
|
|
},
|
|
{
|
|
"id": "structure-state",
|
|
"method": http.MethodGet,
|
|
"path": "/v1/bootstrap/structure",
|
|
},
|
|
{
|
|
"id": "bootstrap-state",
|
|
"method": http.MethodGet,
|
|
"path": "/v1/bootstrap/state",
|
|
},
|
|
{
|
|
"id": "app-shell",
|
|
"method": http.MethodGet,
|
|
"path": "/v1/app-shell",
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapInstallation(w http.ResponseWriter, r *http.Request) {
|
|
record, err := routes.bootstrapService().GetInstallation(r.Context())
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
WriteJSON(w, http.StatusOK, map[string]any{
|
|
"data": record,
|
|
"meta": map[string]any{
|
|
"resource": "bootstrap-installation",
|
|
},
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapAdmin(w http.ResponseWriter, r *http.Request) {
|
|
record, err := routes.bootstrapService().GetAdmin(r.Context())
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
WriteJSON(w, http.StatusOK, map[string]any{
|
|
"data": record,
|
|
"meta": map[string]any{
|
|
"resource": "bootstrap-admin",
|
|
},
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapStructure(w http.ResponseWriter, r *http.Request) {
|
|
record, err := routes.bootstrapService().GetStructure(r.Context())
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
WriteJSON(w, http.StatusOK, map[string]any{
|
|
"data": record,
|
|
"meta": map[string]any{
|
|
"resource": "bootstrap-structure",
|
|
},
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapState(w http.ResponseWriter, r *http.Request) {
|
|
record, err := routes.bootstrapService().GetState(r.Context())
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
WriteJSON(w, http.StatusOK, map[string]any{
|
|
"data": record,
|
|
"meta": map[string]any{
|
|
"resource": "bootstrap-state",
|
|
},
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleAppShellState(w http.ResponseWriter, r *http.Request) {
|
|
record, err := routes.bootstrapService().GetAppShellState(r.Context())
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
WriteJSON(w, http.StatusOK, map[string]any{
|
|
"data": record,
|
|
"meta": map[string]any{
|
|
"resource": "app-shell",
|
|
},
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleDevelopmentBootstrapReset(w http.ResponseWriter, r *http.Request) {
|
|
if !routes.cfg.Config.IsDevelopment() {
|
|
WriteError(w, http.StatusNotFound, RequestIDFromContext(r.Context()), "not_found", "The requested endpoint does not exist.")
|
|
return
|
|
}
|
|
|
|
if err := routes.bootstrapService().ResetDevelopmentState(r.Context()); err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
WriteJSON(w, http.StatusOK, map[string]any{
|
|
"data": map[string]any{
|
|
"reset": true,
|
|
},
|
|
"meta": map[string]any{
|
|
"resource": "development-bootstrap-reset",
|
|
"developmentOnly": true,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapInstanceStep(w http.ResponseWriter, r *http.Request) {
|
|
payload, ok := decodeBootstrapRequest[bootstrapInstanceStepRequest](w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
payload.Protocol = strings.ToLower(strings.TrimSpace(payload.Protocol))
|
|
payload.Access = strings.ToLower(strings.TrimSpace(payload.Access))
|
|
payload.Host = strings.TrimSpace(payload.Host)
|
|
|
|
if payload.Protocol != "http" && payload.Protocol != "https" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Protocol must be either 'http' or 'https'.")
|
|
return
|
|
}
|
|
|
|
if payload.Access != "local" && payload.Access != "remote" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Access must be either 'local' or 'remote'.")
|
|
return
|
|
}
|
|
|
|
if payload.Host == "" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Host is required.")
|
|
return
|
|
}
|
|
|
|
record, err := routes.bootstrapService().SaveInstance(r.Context(), bootstrapservice.SaveInstanceInput{
|
|
Protocol: payload.Protocol,
|
|
Access: payload.Access,
|
|
Host: payload.Host,
|
|
})
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
routes.writeBootstrapStepResponse(w, http.StatusOK, "instance", map[string]any{
|
|
"request": payload,
|
|
"installation": record,
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapModeStep(w http.ResponseWriter, r *http.Request) {
|
|
payload, ok := decodeBootstrapRequest[bootstrapModeStepRequest](w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
payload.Mode = strings.ToLower(strings.TrimSpace(payload.Mode))
|
|
payload.Name = strings.TrimSpace(payload.Name)
|
|
|
|
if payload.Mode != "personal" && payload.Mode != "organizational" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Mode must be either 'personal' or 'organizational'.")
|
|
return
|
|
}
|
|
|
|
if payload.Name == "" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Name is required.")
|
|
return
|
|
}
|
|
|
|
record, err := routes.bootstrapService().SaveMode(r.Context(), bootstrapservice.SaveModeInput{Mode: payload.Mode, Name: payload.Name})
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
routes.writeBootstrapStepResponse(w, http.StatusOK, "mode", map[string]any{
|
|
"request": payload,
|
|
"installation": record,
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapAdminStep(w http.ResponseWriter, r *http.Request) {
|
|
payload, ok := decodeBootstrapRequest[bootstrapAdminStepRequest](w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
payload.DisplayName = strings.TrimSpace(payload.DisplayName)
|
|
payload.Email = strings.ToLower(strings.TrimSpace(payload.Email))
|
|
|
|
if payload.DisplayName == "" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Display name is required.")
|
|
return
|
|
}
|
|
|
|
if payload.Email == "" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Email is required.")
|
|
return
|
|
}
|
|
|
|
if strings.TrimSpace(payload.Password) == "" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Password is required.")
|
|
return
|
|
}
|
|
|
|
record, err := routes.bootstrapService().SaveAdmin(r.Context(), bootstrapservice.SaveAdminInput{
|
|
DisplayName: payload.DisplayName,
|
|
Email: payload.Email,
|
|
Password: payload.Password,
|
|
})
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
routes.writeBootstrapStepResponse(w, http.StatusOK, "admin", map[string]any{
|
|
"request": payload,
|
|
"admin": record,
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) handleBootstrapStructureStep(w http.ResponseWriter, r *http.Request) {
|
|
payload, ok := decodeBootstrapRequest[bootstrapStructureStepRequest](w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
payload.OrganizationName = strings.TrimSpace(payload.OrganizationName)
|
|
payload.DepartmentName = strings.TrimSpace(payload.DepartmentName)
|
|
payload.TeamName = strings.TrimSpace(payload.TeamName)
|
|
payload.ProjectName = strings.TrimSpace(payload.ProjectName)
|
|
|
|
if payload.DepartmentName == "" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Department name is required.")
|
|
return
|
|
}
|
|
|
|
if payload.TeamName == "" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Team name is required.")
|
|
return
|
|
}
|
|
|
|
if payload.ProjectName == "" {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Project name is required.")
|
|
return
|
|
}
|
|
|
|
record, err := routes.bootstrapService().SaveStructure(r.Context(), bootstrapservice.SaveStructureInput{
|
|
OrganizationName: payload.OrganizationName,
|
|
DepartmentName: payload.DepartmentName,
|
|
TeamName: payload.TeamName,
|
|
ProjectName: payload.ProjectName,
|
|
})
|
|
if err != nil {
|
|
routes.writeBootstrapPersistenceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
routes.writeBootstrapStepResponse(w, http.StatusOK, "structure", map[string]any{
|
|
"request": payload,
|
|
"structure": record,
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) bootstrapService() *bootstrapservice.Service {
|
|
return bootstrapservice.NewService(routes.cfg.Database)
|
|
}
|
|
|
|
func (routes apiRoutes) writeBootstrapStepResponse(w http.ResponseWriter, status int, step string, payload any) {
|
|
WriteJSON(w, status, map[string]any{
|
|
"data": map[string]any{
|
|
"step": step,
|
|
"result": payload,
|
|
},
|
|
"meta": map[string]any{
|
|
"resource": "bootstrap-step",
|
|
"persisted": true,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (routes apiRoutes) writeBootstrapPersistenceError(w http.ResponseWriter, r *http.Request, err error) {
|
|
switch {
|
|
case errors.Is(err, bootstrapservice.ErrInstallationNotConfigured), errors.Is(err, bootstrapservice.ErrAdminNotConfigured):
|
|
WriteError(w, http.StatusConflict, RequestIDFromContext(r.Context()), "bootstrap_prerequisite_missing", err.Error())
|
|
default:
|
|
routes.cfg.Logger.Error("persist bootstrap step", "error", err, "path", r.URL.Path)
|
|
message := "Failed to persist bootstrap data."
|
|
if routes.cfg.Config.IsDevelopment() {
|
|
message = message + " " + err.Error()
|
|
}
|
|
WriteError(w, http.StatusInternalServerError, RequestIDFromContext(r.Context()), "bootstrap_persist_failed", message)
|
|
}
|
|
}
|
|
|
|
func decodeBootstrapRequest[T any](w http.ResponseWriter, r *http.Request) (T, bool) {
|
|
var payload T
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
decoder.DisallowUnknownFields()
|
|
|
|
if err := decoder.Decode(&payload); err != nil {
|
|
if errors.Is(err, io.EOF) {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_json", "The request body is required and must be valid JSON for this bootstrap step.")
|
|
return payload, false
|
|
}
|
|
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_json", "The request body must be valid JSON for this bootstrap step.")
|
|
return payload, false
|
|
}
|
|
|
|
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
|
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_json", "The request body must contain a single JSON object.")
|
|
return payload, false
|
|
}
|
|
|
|
return payload, true
|
|
}
|