rdev/internal/domain/component.go
jordan 8282d60c69 feat: implement composable monorepo template system with component architecture
Adds the composable monorepo template system that generates project skeletons
with pluggable components (service, worker, app-react, app-astro, cli).

Key changes:
- Monorepo skeleton templates with shared pkg/, scripts/, and git hooks
- Component templates (service, worker, app-react, app-astro, cli) with
  Dockerfiles, CI steps, and component.yaml manifests
- Component domain model with validation and dependency resolution
- Component handler endpoints for CRUD and composition
- Template provider extended with BuildComposableProject and component assembly
- Deployer extended with composable project deployment support
- Handler timeout constants (TimeoutFastLookup through TimeoutLongRunning)
- envutil package for centralized env var reads with defaults
- api.DecodeJSON helper for standardized request body decoding
- Standardized response helpers (WriteBadRequest, WriteNotFound, etc.)
- Replaced fullstack-app cookbook with composable-app cookbook
- Hardened handler timeouts, logging, and error responses across all handlers

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 19:11:42 -07:00

104 lines
3.1 KiB
Go

// Package domain contains pure domain models with no external dependencies.
package domain
import "regexp"
// ComponentType represents the type of component in a monorepo.
type ComponentType string
const (
ComponentTypeService ComponentType = "service"
ComponentTypeWorker ComponentType = "worker"
ComponentTypeAppAstro ComponentType = "app-astro"
ComponentTypeAppReact ComponentType = "app-react"
ComponentTypeCLI ComponentType = "cli"
)
// ValidComponentTypes lists all valid component types.
var ValidComponentTypes = []ComponentType{
ComponentTypeService,
ComponentTypeWorker,
ComponentTypeAppAstro,
ComponentTypeAppReact,
ComponentTypeCLI,
}
// IsValidComponentType checks if a string is a valid component type.
func IsValidComponentType(t string) bool {
for _, valid := range ValidComponentTypes {
if string(valid) == t {
return true
}
}
return false
}
// Component represents a component in a monorepo project.
type Component struct {
Type ComponentType `json:"type"`
Name string `json:"name"`
Path string `json:"path"` // e.g., "services/auth-api"
Port int `json:"port"` // 0 if not applicable
Template string `json:"template"` // template used
Dependencies []string `json:"dependencies"` // e.g., ["postgres", "redis"]
}
// DestDir returns the destination directory for this component type.
func (c ComponentType) DestDir() string {
switch c {
case ComponentTypeService:
return "services"
case ComponentTypeWorker:
return "workers"
case ComponentTypeAppAstro, ComponentTypeAppReact:
return "apps"
case ComponentTypeCLI:
return "cli"
default:
return ""
}
}
// StartingPort returns the starting port number for this component type.
// Workers and CLIs don't expose ports (return 0).
func (c ComponentType) StartingPort() int {
switch c {
case ComponentTypeService:
return 8001
case ComponentTypeAppAstro, ComponentTypeAppReact:
return 3001
case ComponentTypeWorker, ComponentTypeCLI:
return 0
default:
return 0
}
}
// NeedsPort returns true if this component type requires a port assignment.
func (c ComponentType) NeedsPort() bool {
return c == ComponentTypeService || c == ComponentTypeAppAstro || c == ComponentTypeAppReact
}
// IsGoComponent returns true if this component type uses Go (and needs go.work entry).
func (c ComponentType) IsGoComponent() bool {
return c == ComponentTypeService || c == ComponentTypeWorker || c == ComponentTypeCLI
}
// componentNameRegex validates component names (slug format: lowercase, alphanumeric, dashes).
var componentNameRegex = regexp.MustCompile(`^[a-z][a-z0-9-]*$`)
// ValidateComponentName validates that a component name is in slug format.
// Must be lowercase, start with a letter, and contain only letters, numbers, and dashes.
func ValidateComponentName(name string) error {
if name == "" {
return ErrInvalidComponentName
}
if len(name) > MaxProjectNameLen { // Reuse the 63-char limit from K8s
return ErrInvalidComponentName
}
if !componentNameRegex.MatchString(name) {
return ErrInvalidComponentName
}
return nil
}