Feat: Add bootstrap persistence and shell routes
This commit is contained in:
363
Backend/internal/httpx/api_bootstrap_routes.go
Normal file
363
Backend/internal/httpx/api_bootstrap_routes.go
Normal file
@@ -0,0 +1,363 @@
|
||||
// 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"`
|
||||
}
|
||||
|
||||
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) 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))
|
||||
|
||||
if payload.Mode != "personal" && payload.Mode != "organizational" {
|
||||
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Mode must be either 'personal' or 'organizational'.")
|
||||
return
|
||||
}
|
||||
|
||||
record, err := routes.bootstrapService().SaveMode(r.Context(), bootstrapservice.SaveModeInput{Mode: payload.Mode})
|
||||
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)
|
||||
WriteError(w, http.StatusInternalServerError, RequestIDFromContext(r.Context()), "bootstrap_persist_failed", "Failed to persist bootstrap data.")
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -19,6 +19,18 @@ func newAPIRoutes(cfg RouterConfig) routeRegistrar {
|
||||
func (routes apiRoutes) Register(router chi.Router) {
|
||||
router.Route("/v1", func(apiRouter chi.Router) {
|
||||
apiRouter.Get("/", routes.handleIndex)
|
||||
apiRouter.Get("/bootstrap", routes.handleBootstrapOverview)
|
||||
apiRouter.Get("/bootstrap/installation", routes.handleBootstrapInstallation)
|
||||
apiRouter.Get("/bootstrap/admin", routes.handleBootstrapAdmin)
|
||||
apiRouter.Get("/bootstrap/structure", routes.handleBootstrapStructure)
|
||||
apiRouter.Get("/bootstrap/state", routes.handleBootstrapState)
|
||||
apiRouter.Route("/bootstrap/steps", func(bootstrapRouter chi.Router) {
|
||||
bootstrapRouter.Post("/instance", routes.handleBootstrapInstanceStep)
|
||||
bootstrapRouter.Post("/mode", routes.handleBootstrapModeStep)
|
||||
bootstrapRouter.Post("/admin", routes.handleBootstrapAdminStep)
|
||||
bootstrapRouter.Post("/structure", routes.handleBootstrapStructureStep)
|
||||
})
|
||||
apiRouter.Get("/app-shell", routes.handleAppShellState)
|
||||
apiRouter.Get("/organizations", routes.handleOrganizations)
|
||||
apiRouter.Get("/workspaces", routes.handleWorkspaces)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user