environment

package
v0.0.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeEnvironmentNotFound                = "ENVIRONMENT_NOT_FOUND"
	CodeEnvironmentAlreadyExists           = "ENVIRONMENT_ALREADY_EXISTS"
	CodeEnvironmentSlugAlreadyExists       = "ENVIRONMENT_SLUG_ALREADY_EXISTS"
	CodeDefaultEnvironmentNotFound         = "DEFAULT_ENVIRONMENT_NOT_FOUND"
	CodeCannotDeleteDefaultEnvironment     = "CANNOT_DELETE_DEFAULT_ENVIRONMENT"
	CodeCannotDeleteProductionEnvironment  = "CANNOT_DELETE_PRODUCTION_ENVIRONMENT"
	CodeCannotModifyDefaultEnvironmentType = "CANNOT_MODIFY_DEFAULT_ENVIRONMENT_TYPE"
	CodeEnvironmentTypeForbidden           = "ENVIRONMENT_TYPE_FORBIDDEN"
	CodeEnvironmentLimitReached            = "ENVIRONMENT_LIMIT_REACHED"
	CodePromotionNotAllowed                = "PROMOTION_NOT_ALLOWED"
	CodePromotionFailed                    = "PROMOTION_FAILED"
	CodePromotionNotFound                  = "PROMOTION_NOT_FOUND"
	CodePromotionInProgress                = "PROMOTION_IN_PROGRESS"
	CodeInvalidEnvironmentStatus           = "INVALID_ENVIRONMENT_STATUS"
	CodeInvalidEnvironmentType             = "INVALID_ENVIRONMENT_TYPE"
	CodeSourceEnvironmentNotFound          = "SOURCE_ENVIRONMENT_NOT_FOUND"
	CodeTargetEnvironmentNotFound          = "TARGET_ENVIRONMENT_NOT_FOUND"
	CodeInvalidSlug                        = "INVALID_SLUG"
	CodeInvalidConfig                      = "INVALID_CONFIG"
)

Variables

View Source
var (
	ErrEnvironmentNotFound                = &errs.AuthsomeError{Code: CodeEnvironmentNotFound}
	ErrEnvironmentAlreadyExists           = &errs.AuthsomeError{Code: CodeEnvironmentAlreadyExists}
	ErrEnvironmentSlugAlreadyExists       = &errs.AuthsomeError{Code: CodeEnvironmentSlugAlreadyExists}
	ErrDefaultEnvironmentNotFound         = &errs.AuthsomeError{Code: CodeDefaultEnvironmentNotFound}
	ErrCannotDeleteDefaultEnvironment     = &errs.AuthsomeError{Code: CodeCannotDeleteDefaultEnvironment}
	ErrCannotDeleteProductionEnvironment  = &errs.AuthsomeError{Code: CodeCannotDeleteProductionEnvironment}
	ErrCannotModifyDefaultEnvironmentType = &errs.AuthsomeError{Code: CodeCannotModifyDefaultEnvironmentType}
	ErrEnvironmentTypeForbidden           = &errs.AuthsomeError{Code: CodeEnvironmentTypeForbidden}
	ErrEnvironmentLimitReached            = &errs.AuthsomeError{Code: CodeEnvironmentLimitReached}
	ErrPromotionNotAllowed                = &errs.AuthsomeError{Code: CodePromotionNotAllowed}
	ErrPromotionFailed                    = &errs.AuthsomeError{Code: CodePromotionFailed}
	ErrPromotionNotFound                  = &errs.AuthsomeError{Code: CodePromotionNotFound}
	ErrPromotionInProgress                = &errs.AuthsomeError{Code: CodePromotionInProgress}
	ErrInvalidEnvironmentStatus           = &errs.AuthsomeError{Code: CodeInvalidEnvironmentStatus}
	ErrInvalidEnvironmentType             = &errs.AuthsomeError{Code: CodeInvalidEnvironmentType}
	ErrSourceEnvironmentNotFound          = &errs.AuthsomeError{Code: CodeSourceEnvironmentNotFound}
	ErrTargetEnvironmentNotFound          = &errs.AuthsomeError{Code: CodeTargetEnvironmentNotFound}
	ErrInvalidSlug                        = &errs.AuthsomeError{Code: CodeInvalidSlug}
	ErrInvalidConfig                      = &errs.AuthsomeError{Code: CodeInvalidConfig}
)

Functions

func CannotDeleteDefaultEnvironment

func CannotDeleteDefaultEnvironment() *errs.AuthsomeError

CannotDeleteDefaultEnvironment returns an error when attempting to delete the default environment.

func CannotDeleteProductionEnvironment

func CannotDeleteProductionEnvironment() *errs.AuthsomeError

func CannotModifyDefaultEnvironmentType

func CannotModifyDefaultEnvironmentType() *errs.AuthsomeError

CannotModifyDefaultEnvironmentType returns an error when attempting to modify the default environment type.

func DefaultEnvironmentNotFound

func DefaultEnvironmentNotFound(appID string) *errs.AuthsomeError

func EnvironmentAlreadyExists

func EnvironmentAlreadyExists(name string) *errs.AuthsomeError

func EnvironmentLimitReached

func EnvironmentLimitReached(limit int) *errs.AuthsomeError

func EnvironmentNotFound

func EnvironmentNotFound(id string) *errs.AuthsomeError

EnvironmentNotFound returns an error when an environment is not found.

func EnvironmentSlugAlreadyExists

func EnvironmentSlugAlreadyExists(slug string) *errs.AuthsomeError

func EnvironmentTypeForbidden

func EnvironmentTypeForbidden(envType EnvironmentType) *errs.AuthsomeError

func GetEnvironment

func GetEnvironment(ctx context.Context) any

GetEnvironment retrieves full environment from context.

func GetEnvironmentID

func GetEnvironmentID(ctx context.Context) (xid.ID, bool)

GetEnvironmentID retrieves environment ID from context.

func InvalidConfig

func InvalidConfig(reason string) *errs.AuthsomeError

func InvalidEnvironmentStatus

func InvalidEnvironmentStatus(status string) *errs.AuthsomeError

InvalidEnvironmentStatus returns an error when an invalid environment status is provided.

func InvalidEnvironmentType

func InvalidEnvironmentType(envType string) *errs.AuthsomeError

func InvalidSlug

func InvalidSlug(slug string) *errs.AuthsomeError

func PromotionFailed

func PromotionFailed(reason string) *errs.AuthsomeError

func PromotionInProgress

func PromotionInProgress(id string) *errs.AuthsomeError

func PromotionNotAllowed

func PromotionNotAllowed() *errs.AuthsomeError

PromotionNotAllowed returns an error when environment promotion is not allowed.

func PromotionNotFound

func PromotionNotFound(id string) *errs.AuthsomeError

func SetEnvironment

func SetEnvironment(ctx context.Context, env any) context.Context

SetEnvironment sets full environment in context.

func SetEnvironmentID

func SetEnvironmentID(ctx context.Context, envID xid.ID) context.Context

SetEnvironmentID sets environment ID in context.

func SourceEnvironmentNotFound

func SourceEnvironmentNotFound(id string) *errs.AuthsomeError

func TargetEnvironmentNotFound

func TargetEnvironmentNotFound(id string) *errs.AuthsomeError

Types

type AppRepository

type AppRepository interface {
	Count(ctx context.Context) (int, error)
	Create(ctx context.Context, app *schema.App) error
	FindBySlug(ctx context.Context, slug string) (*schema.App, error)
}

AppRepository defines minimal interface needed for bootstrap.

type Bootstrap

type Bootstrap struct {
	// contains filtered or unexported fields
}

Bootstrap handles initial app and environment setup.

func NewBootstrap

func NewBootstrap(appRepo AppRepository, envRepo Repository, config BootstrapConfig) *Bootstrap

NewBootstrap creates a new bootstrap instance.

func (*Bootstrap) EnsureAppEnvironment

func (b *Bootstrap) EnsureAppEnvironment(ctx context.Context, appID xid.ID) (*schema.Environment, error)

EnsureAppEnvironment ensures an app has a default environment.

func (*Bootstrap) EnsureDefaultApp

func (b *Bootstrap) EnsureDefaultApp(ctx context.Context) (*schema.App, *schema.Environment, error)

EnsureDefaultApp ensures the default app exists (non-multitenancy mode).

func (*Bootstrap) ValidateMultitenancyMode

func (b *Bootstrap) ValidateMultitenancyMode(ctx context.Context) error

ValidateMultitenancyMode ensures app count respects multitenancy setting.

type BootstrapConfig

type BootstrapConfig struct {
	DefaultAppName       string `json:"defaultAppName"`
	DefaultAppSlug       string `json:"defaultAppSlug"`
	AutoCreateDefaultApp bool   `json:"autoCreateDefaultApp"`
	MultitenancyEnabled  bool   `json:"multitenancyEnabled"`
}

BootstrapConfig holds configuration for bootstrap process.

type Config

type Config struct {
	AutoCreateDev                  bool              `json:"autoCreateDev"`
	DefaultDevName                 string            `json:"defaultDevName"`
	AllowPromotion                 bool              `json:"allowPromotion"`
	RequireConfirmationForDataCopy bool              `json:"requireConfirmationForDataCopy"`
	MaxEnvironmentsPerApp          int               `json:"maxEnvironmentsPerApp"`
	AllowedTypes                   []EnvironmentType `json:"allowedTypes"`
}

Config holds the environment service configuration.

type ContextKey

type ContextKey string

ContextKey type for environment context keys.

const (
	// EnvironmentKey is the context key for full environment object.
	EnvironmentKey ContextKey = "environment"
)

type CreateEnvironmentRequest

type CreateEnvironmentRequest struct {
	AppID  xid.ID          `json:"appId"            validate:"required"`
	Name   string          `json:"name"             validate:"required,min=1,max=100"`
	Slug   string          `json:"slug"             validate:"required,min=1,max=50,alphanum"`
	Type   EnvironmentType `json:"type"             validate:"required,oneof=development staging production preview test"`
	Config map[string]any  `json:"config,omitempty"`
}

CreateEnvironmentRequest represents the request to create an environment.

type Environment

type Environment struct {
	ID        xid.ID            `json:"id"`
	AppID     xid.ID            `json:"appId"`
	Name      string            `json:"name"`
	Slug      string            `json:"slug"`
	Type      EnvironmentType   `json:"type"`
	Status    EnvironmentStatus `json:"status"`
	Config    map[string]any    `json:"config,omitempty"`
	IsDefault bool              `json:"isDefault"`
	// Audit fields
	CreatedAt time.Time  `json:"createdAt"`
	UpdatedAt time.Time  `json:"updatedAt"`
	DeletedAt *time.Time `json:"deletedAt,omitempty"`
}

Environment represents an environment DTO This is separate from schema.Environment to maintain proper separation of concerns.

func FromSchemaEnvironment

func FromSchemaEnvironment(se *schema.Environment) *Environment

FromSchemaEnvironment converts a schema.Environment model to Environment DTO.

func FromSchemaEnvironments

func FromSchemaEnvironments(envs []*schema.Environment) []*Environment

FromSchemaEnvironments converts a slice of schema.Environment to Environment DTOs.

func (*Environment) IsActive

func (e *Environment) IsActive() bool

IsActive checks if the environment is active.

func (*Environment) IsDeleted

func (e *Environment) IsDeleted() bool

IsDeleted checks if the environment is soft-deleted.

func (*Environment) IsDevelopment

func (e *Environment) IsDevelopment() bool

IsDevelopment checks if the environment is development.

func (*Environment) IsProduction

func (e *Environment) IsProduction() bool

IsProduction checks if the environment is production.

func (*Environment) ToSchema

func (e *Environment) ToSchema() *schema.Environment

ToSchema converts the Environment DTO to a schema.Environment model.

type EnvironmentService

type EnvironmentService interface {
	// Environment operations
	CreateEnvironment(ctx context.Context, req *CreateEnvironmentRequest) (*Environment, error)
	CreateDefaultEnvironment(ctx context.Context, appID xid.ID) (*Environment, error)
	GetEnvironment(ctx context.Context, id xid.ID) (*Environment, error)
	GetEnvironmentBySlug(ctx context.Context, appID xid.ID, slug string) (*Environment, error)
	GetDefaultEnvironment(ctx context.Context, appID xid.ID) (*Environment, error)
	ListEnvironments(ctx context.Context, filter *ListEnvironmentsFilter) (*ListEnvironmentsResponse, error)
	UpdateEnvironment(ctx context.Context, id xid.ID, req *UpdateEnvironmentRequest) (*Environment, error)
	DeleteEnvironment(ctx context.Context, id xid.ID) error

	// Promotion operations
	PromoteEnvironment(ctx context.Context, req *PromoteEnvironmentRequest) (*Promotion, error)
	GetPromotion(ctx context.Context, id xid.ID) (*Promotion, error)
	ListPromotions(ctx context.Context, filter *ListPromotionsFilter) (*ListPromotionsResponse, error)
}

EnvironmentService defines the contract for environment service operations This allows plugins to decorate the service with additional behavior Following the pattern from core/jwt and core/app architecture.

type EnvironmentStatus added in v0.0.15

type EnvironmentStatus string
const (
	EnvironmentStatusActive   EnvironmentStatus = "active"
	EnvironmentStatusInactive EnvironmentStatus = "inactive"
)

type EnvironmentType added in v0.0.15

type EnvironmentType string
const (
	EnvironmentTypeDevelopment EnvironmentType = "development"
	EnvironmentTypeProduction  EnvironmentType = "production"
	EnvironmentTypeStaging     EnvironmentType = "staging"
	EnvironmentTypePreview     EnvironmentType = "preview"
	EnvironmentTypeTest        EnvironmentType = "test"
)

type ListEnvironmentsFilter

type ListEnvironmentsFilter struct {
	pagination.PaginationParams

	AppID     xid.ID  `json:"appId"               query:"app_id"`
	Type      *string `json:"type,omitempty"      query:"type"`
	Status    *string `json:"status,omitempty"    query:"status"`
	IsDefault *bool   `json:"isDefault,omitempty" query:"is_default"`
}

ListEnvironmentsFilter represents filter parameters for listing environments.

type ListEnvironmentsResponse

type ListEnvironmentsResponse = pagination.PageResponse[*Environment]

ListEnvironmentsResponse represents paginated environment response.

type ListPromotionsFilter

type ListPromotionsFilter struct {
	pagination.PaginationParams

	AppID       xid.ID  `json:"appId"                 query:"app_id"`
	SourceEnvID *xid.ID `json:"sourceEnvId,omitempty" query:"source_env_id"`
	TargetEnvID *xid.ID `json:"targetEnvId,omitempty" query:"target_env_id"`
	Status      *string `json:"status,omitempty"      query:"status"`
	PromotedBy  *xid.ID `json:"promotedBy,omitempty"  query:"promoted_by"`
}

ListPromotionsFilter represents filter parameters for listing promotions.

type ListPromotionsResponse

type ListPromotionsResponse = pagination.PageResponse[*Promotion]

ListPromotionsResponse represents paginated promotion response.

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

Middleware extracts environment context from the request.

func NewMiddleware

func NewMiddleware(service *Service, config MiddlewareConfig) *Middleware

NewMiddleware creates a new environment middleware.

func (*Middleware) Handler

func (m *Middleware) Handler(next http.Handler) http.Handler

Handler returns the middleware handler function.

type MiddlewareConfig

type MiddlewareConfig struct {
	HeaderName        string   `json:"headerName"`
	QueryParamName    string   `json:"queryParamName"`
	SubdomainEnabled  bool     `json:"subdomainEnabled"`
	DefaultToDevEnv   bool     `json:"defaultToDevEnv"`
	AllowedSubdomains []string `json:"allowedSubdomains"`
}

MiddlewareConfig holds configuration for environment middleware.

type PromoteEnvironmentRequest

type PromoteEnvironmentRequest struct {
	SourceEnvID xid.ID          `json:"sourceEnvId"      validate:"required"`
	TargetName  string          `json:"targetName"       validate:"required,min=1,max=100"`
	TargetSlug  string          `json:"targetSlug"       validate:"required,min=1,max=50,alphanum"`
	TargetType  EnvironmentType `json:"targetType"       validate:"required,oneof=development staging production preview test"`
	IncludeData bool            `json:"includeData"`
	PromotedBy  xid.ID          `json:"promotedBy"       validate:"required"`
	Config      map[string]any  `json:"config,omitempty"`
}

PromoteEnvironmentRequest represents the request to promote an environment.

type Promotion

type Promotion struct {
	ID           xid.ID     `json:"id"`
	AppID        xid.ID     `json:"appId"`
	SourceEnvID  xid.ID     `json:"sourceEnvId"`
	TargetEnvID  xid.ID     `json:"targetEnvId"`
	PromotedBy   xid.ID     `json:"promotedBy"`
	Status       string     `json:"status"`
	IncludeData  bool       `json:"includeData"`
	ErrorMessage string     `json:"errorMessage,omitempty"`
	CompletedAt  *time.Time `json:"completedAt,omitempty"`
	CreatedAt    time.Time  `json:"createdAt"`
	UpdatedAt    time.Time  `json:"updatedAt"`
}

Promotion represents an environment promotion DTO.

func FromSchemaPromotion

func FromSchemaPromotion(sp *schema.EnvironmentPromotion) *Promotion

FromSchemaPromotion converts a schema.EnvironmentPromotion model to Promotion DTO.

func FromSchemaPromotions

func FromSchemaPromotions(promotions []*schema.EnvironmentPromotion) []*Promotion

FromSchemaPromotions converts a slice of schema.EnvironmentPromotion to Promotion DTOs.

func (*Promotion) IsCompleted

func (p *Promotion) IsCompleted() bool

IsCompleted checks if the promotion is completed.

func (*Promotion) IsFailed

func (p *Promotion) IsFailed() bool

IsFailed checks if the promotion failed.

func (*Promotion) IsInProgress

func (p *Promotion) IsInProgress() bool

IsInProgress checks if the promotion is in progress.

func (*Promotion) IsPending

func (p *Promotion) IsPending() bool

IsPending checks if the promotion is pending.

func (*Promotion) ToSchema

func (p *Promotion) ToSchema() *schema.EnvironmentPromotion

ToSchema converts the Promotion DTO to a schema.EnvironmentPromotion model.

type Repository

type Repository interface {
	// Environment CRUD
	Create(ctx context.Context, env *schema.Environment) error
	FindByID(ctx context.Context, id xid.ID) (*schema.Environment, error)
	FindByAppAndSlug(ctx context.Context, appID xid.ID, slug string) (*schema.Environment, error)
	FindDefaultByApp(ctx context.Context, appID xid.ID) (*schema.Environment, error)

	// ListEnvironments lists environments with pagination and filtering
	ListEnvironments(ctx context.Context, filter *ListEnvironmentsFilter) (*pagination.PageResponse[*schema.Environment], error)

	// CountByApp counts environments for an app
	CountByApp(ctx context.Context, appID xid.ID) (int, error)

	Update(ctx context.Context, env *schema.Environment) error
	Delete(ctx context.Context, id xid.ID) error

	// Promotion operations
	CreatePromotion(ctx context.Context, promotion *schema.EnvironmentPromotion) error
	FindPromotionByID(ctx context.Context, id xid.ID) (*schema.EnvironmentPromotion, error)

	// ListPromotions lists promotions with pagination and filtering
	ListPromotions(ctx context.Context, filter *ListPromotionsFilter) (*pagination.PageResponse[*schema.EnvironmentPromotion], error)

	UpdatePromotion(ctx context.Context, promotion *schema.EnvironmentPromotion) error
}

Repository defines the interface for environment data access This follows the Interface Segregation Principle from core/app and core/jwt architecture.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service handles environment-related business logic.

func NewService

func NewService(repo Repository, config Config) *Service

NewService creates a new environment service.

func (*Service) CreateDefaultEnvironment

func (s *Service) CreateDefaultEnvironment(ctx context.Context, appID xid.ID) (*Environment, error)

CreateDefaultEnvironment creates the default dev environment for an app.

func (*Service) CreateEnvironment

func (s *Service) CreateEnvironment(ctx context.Context, req *CreateEnvironmentRequest) (*Environment, error)

CreateEnvironment creates a new environment.

func (*Service) DeleteEnvironment

func (s *Service) DeleteEnvironment(ctx context.Context, id xid.ID) error

DeleteEnvironment deletes an environment.

func (*Service) GetDefaultEnvironment

func (s *Service) GetDefaultEnvironment(ctx context.Context, appID xid.ID) (*Environment, error)

GetDefaultEnvironment retrieves the default environment for an app.

func (*Service) GetEnvironment

func (s *Service) GetEnvironment(ctx context.Context, id xid.ID) (*Environment, error)

GetEnvironment retrieves an environment by ID.

func (*Service) GetEnvironmentBySlug

func (s *Service) GetEnvironmentBySlug(ctx context.Context, appID xid.ID, slug string) (*Environment, error)

GetEnvironmentBySlug retrieves an environment by app and slug.

func (*Service) GetPromotion

func (s *Service) GetPromotion(ctx context.Context, id xid.ID) (*Promotion, error)

GetPromotion retrieves a promotion by ID.

func (*Service) ListEnvironments

func (s *Service) ListEnvironments(ctx context.Context, filter *ListEnvironmentsFilter) (*ListEnvironmentsResponse, error)

ListEnvironments lists environments for an app with pagination.

func (*Service) ListPromotions

func (s *Service) ListPromotions(ctx context.Context, filter *ListPromotionsFilter) (*ListPromotionsResponse, error)

ListPromotions lists promotions for an app with pagination.

func (*Service) PromoteEnvironment

func (s *Service) PromoteEnvironment(ctx context.Context, req *PromoteEnvironmentRequest) (*Promotion, error)

PromoteEnvironment promotes/clones one environment to another.

func (*Service) UpdateEnvironment

func (s *Service) UpdateEnvironment(ctx context.Context, id xid.ID, req *UpdateEnvironmentRequest) (*Environment, error)

UpdateEnvironment updates an environment.

type UpdateEnvironmentRequest

type UpdateEnvironmentRequest struct {
	Name   *string            `json:"name,omitempty"   validate:"omitempty,min=1,max=100"`
	Status *EnvironmentStatus `json:"status,omitempty" validate:"omitempty,oneof=active inactive maintenance"`
	Config map[string]any     `json:"config,omitempty"`
	Type   *EnvironmentType   `json:"type,omitempty"   validate:"omitempty,oneof=development staging production preview test"`
}

UpdateEnvironmentRequest represents the request to update an environment.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL