api

package
v0.0.0-...-ee1a901 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package api provides the HTTP API server for the lunar platform.

The API implements the OpenAPI specification defined in docs/openapi.yaml and provides endpoints for managing functions, versions, executions, and runtime execution.

Main endpoint groups:

  • /api/functions - Function management (CRUD)
  • /api/functions/{id}/versions - Version management
  • /api/executions - Execution history and logs
  • /fn/{function_id} - Runtime function execution

Index

Constants

View Source
const (
	// MaxPageSize is the maximum allowed page size for pagination
	MaxPageSize = 100
	// MaxFunctionNameLength is the maximum length for function names
	MaxFunctionNameLength = 100
	// MaxDescriptionLength is the maximum length for function descriptions
	MaxDescriptionLength = 500
	// MaxCodeLength is the maximum length for function code
	MaxCodeLength = 1024 * 1024 // 1MB
	// MaxEnvVarKeyLength is the maximum length for environment variable keys
	MaxEnvVarKeyLength = 100
	// MaxEnvVarValueLength is the maximum length for environment variable values
	MaxEnvVarValueLength = 10000
	// MaxEnvVars is the maximum number of environment variables per function
	MaxEnvVars = 100
)

Variables

View Source
var AllowedRetentionDays = []int{7, 15, 30, 365}

Functions

func ActivateVersionHandler

func ActivateVersionHandler(database store.DB) http.HandlerFunc

ActivateVersionHandler returns a handler for activating a version

func AuthMiddleware

func AuthMiddleware(apiKey string) func(http.Handler) http.Handler

AuthMiddleware validates authentication via cookie or Bearer token

func CORSMiddleware

func CORSMiddleware(next http.Handler) http.Handler

CORSMiddleware adds CORS headers

func Chain

func Chain(h http.Handler, middlewares ...Middleware) http.Handler

Chain applies middlewares in order

func CreateFunctionHandler

func CreateFunctionHandler(database store.DB) http.HandlerFunc

CreateFunctionHandler returns a handler for creating functions

func DeleteFunctionHandler

func DeleteFunctionHandler(database store.DB) http.HandlerFunc

DeleteFunctionHandler returns a handler for deleting functions

func DeleteVersionHandler

func DeleteVersionHandler(database store.DB) http.HandlerFunc

DeleteVersionHandler returns a handler for deleting a version

func ExecuteFunctionHandler

func ExecuteFunctionHandler(deps ExecuteFunctionDeps) http.HandlerFunc

ExecuteFunctionHandler returns a handler for executing functions

func GetExecutionAIRequestsHandler

func GetExecutionAIRequestsHandler(database store.DB, aiTracker ai.Tracker) http.HandlerFunc

GetExecutionAIRequestsHandler returns a handler for getting AI requests for an execution

func GetExecutionEmailRequestsHandler

func GetExecutionEmailRequestsHandler(database store.DB, emailTracker email.Tracker) http.HandlerFunc

GetExecutionEmailRequestsHandler returns a handler for getting email requests for an execution

func GetExecutionHandler

func GetExecutionHandler(database store.DB) http.HandlerFunc

GetExecutionHandler returns a handler for getting a specific execution

func GetExecutionLogsHandler

func GetExecutionLogsHandler(database store.DB, appLogger logger.Logger) http.HandlerFunc

GetExecutionLogsHandler returns a handler for getting execution logs

func GetFunctionHandler

func GetFunctionHandler(database store.DB, envStore env.Store) http.HandlerFunc

GetFunctionHandler returns a handler for getting a specific function

func GetNextRunHandler

func GetNextRunHandler(database store.DB) http.HandlerFunc

GetNextRunHandler returns a handler for getting the next scheduled run time

func GetVersionDiffHandler

func GetVersionDiffHandler(database store.DB) http.HandlerFunc

GetVersionDiffHandler returns a handler for getting diff between versions

func GetVersionHandler

func GetVersionHandler(database store.DB) http.HandlerFunc

GetVersionHandler returns a handler for getting a specific version

func HandleLogin

func HandleLogin(apiKey string) http.HandlerFunc

HandleLogin validates the API key and sets an HttpOnly cookie

func HandleLogout

func HandleLogout() http.HandlerFunc

HandleLogout clears the authentication cookie

func ListExecutionsHandler

func ListExecutionsHandler(database store.DB) http.HandlerFunc

ListExecutionsHandler returns a handler for listing executions

func ListFunctionsHandler

func ListFunctionsHandler(database store.DB) http.HandlerFunc

ListFunctionsHandler returns a handler for listing functions

func ListVersionsHandler

func ListVersionsHandler(database store.DB) http.HandlerFunc

ListVersionsHandler returns a handler for listing function versions

func LoggingMiddleware

func LoggingMiddleware(next http.Handler) http.Handler

LoggingMiddleware logs HTTP requests

func RecoveryMiddleware

func RecoveryMiddleware(next http.Handler) http.Handler

RecoveryMiddleware recovers from panics and returns 500

func UpdateEnvVarsHandler

func UpdateEnvVarsHandler(database store.DB, envStore env.Store) http.HandlerFunc

UpdateEnvVarsHandler returns a handler for updating environment variables

func UpdateFunctionHandler

func UpdateFunctionHandler(database store.DB, scheduler *internalcron.FunctionScheduler) http.HandlerFunc

UpdateFunctionHandler returns a handler for updating functions

func ValidateCreateFunctionRequest

func ValidateCreateFunctionRequest(req *CreateFunctionRequest) error

ValidateCreateFunctionRequest validates a CreateFunctionRequest

func ValidateUpdateEnvVarsRequest

func ValidateUpdateEnvVarsRequest(req *UpdateEnvVarsRequest) error

ValidateUpdateEnvVarsRequest validates an UpdateEnvVarsRequest

func ValidateUpdateFunctionRequest

func ValidateUpdateFunctionRequest(req *store.UpdateFunctionRequest) error

ValidateUpdateFunctionRequest validates an UpdateFunctionRequest

Types

type CreateFunctionRequest

type CreateFunctionRequest struct {
	Name        string  `json:"name"`
	Description *string `json:"description,omitempty"`
	Code        string  `json:"code"`
}

CreateFunctionRequest is the request body for creating a function

type DiffLine

type DiffLine struct {
	LineType DiffLineType `json:"line_type"`
	OldLine  *int         `json:"old_line,omitempty"`
	NewLine  *int         `json:"new_line,omitempty"`
	Content  string       `json:"content"`
}

DiffLine represents a line in a version diff

type DiffLineType

type DiffLineType string

DiffLineType represents the type of change in a diff line

const (
	DiffLineUnchanged DiffLineType = "unchanged"
	DiffLineAdded     DiffLineType = "added"
	DiffLineRemoved   DiffLineType = "removed"
)

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

ErrorResponse is the standard error response

type ExecuteFunctionDeps

type ExecuteFunctionDeps struct {
	Engine  engine.Engine
	BaseURL string
}

ExecuteFunctionDeps holds dependencies for executing functions

type ExecutionWithLogs

type ExecutionWithLogs struct {
	store.Execution
	Logs []LogEntry `json:"logs"`
}

ExecutionWithLogs includes execution details and logs

type ListExecutionsResponse

type ListExecutionsResponse struct {
	Executions []store.Execution `json:"executions"`
}

ListExecutionsResponse is the response for listing executions

type ListFunctionsResponse

type ListFunctionsResponse struct {
	Functions []store.FunctionWithActiveVersion `json:"functions"`
}

ListFunctionsResponse is the response for listing functions

type ListVersionsResponse

type ListVersionsResponse struct {
	Versions []store.FunctionVersion `json:"versions"`
}

ListVersionsResponse is the response for listing versions

type LogEntry

type LogEntry struct {
	Level     LogLevel `json:"level"`
	Message   string   `json:"message"`
	CreatedAt int64    `json:"created_at"`
}

LogEntry represents a log entry from function execution

type LogLevel

type LogLevel string

LogLevel represents the severity level of a log entry

const (
	LogLevelDebug LogLevel = "debug"
	LogLevelInfo  LogLevel = "info"
	LogLevelWarn  LogLevel = "warn"
	LogLevelError LogLevel = "error"
)

type LoginRequest

type LoginRequest struct {
	APIKey string `json:"apiKey"`
}

type LoginResponse

type LoginResponse struct {
	Success bool   `json:"success"`
	Error   string `json:"error,omitempty"`
}

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware type

type NextRunResponse

type NextRunResponse struct {
	HasSchedule  bool    `json:"has_schedule"`
	CronSchedule *string `json:"cron_schedule,omitempty"`
	CronStatus   *string `json:"cron_status,omitempty"`
	IsPaused     bool    `json:"is_paused,omitempty"`
	NextRun      *int64  `json:"next_run,omitempty"`
	NextRunHuman *string `json:"next_run_human,omitempty"`
}

NextRunResponse is the response for getting the next scheduled run time

type PaginatedAIRequestsResponse

type PaginatedAIRequestsResponse struct {
	AIRequests []store.AIRequest    `json:"ai_requests"`
	Pagination store.PaginationInfo `json:"pagination"`
}

PaginatedAIRequestsResponse is the paginated response for AI requests

type PaginatedEmailRequestsResponse

type PaginatedEmailRequestsResponse struct {
	EmailRequests []store.EmailRequest `json:"email_requests"`
	Pagination    store.PaginationInfo `json:"pagination"`
}

PaginatedEmailRequestsResponse is the paginated response for email requests

type PaginatedExecutionWithLogs

type PaginatedExecutionWithLogs struct {
	store.Execution
	Logs       []LogEntry           `json:"logs"`
	Pagination store.PaginationInfo `json:"pagination"`
}

PaginatedExecutionWithLogs includes execution details with paginated logs

type PaginatedExecutionsResponse

type PaginatedExecutionsResponse struct {
	Executions []store.Execution    `json:"executions"`
	Pagination store.PaginationInfo `json:"pagination"`
}

PaginatedExecutionsResponse is the paginated response for listing executions

type PaginatedFunctionsResponse

type PaginatedFunctionsResponse struct {
	Functions  []store.FunctionWithActiveVersion `json:"functions"`
	Pagination store.PaginationInfo              `json:"pagination"`
}

PaginatedFunctionsResponse is the paginated response for listing functions

type PaginatedLogsResponse

type PaginatedLogsResponse struct {
	Logs       []LogEntry           `json:"logs"`
	Pagination store.PaginationInfo `json:"pagination"`
}

PaginatedLogsResponse is the paginated response for listing logs

type PaginatedVersionsResponse

type PaginatedVersionsResponse struct {
	Versions   []store.FunctionVersion `json:"versions"`
	Pagination store.PaginationInfo    `json:"pagination"`
}

PaginatedVersionsResponse is the paginated response for listing versions

type Server

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

Server represents the API server

func NewServer

func NewServer(config ServerConfig) *Server

NewServer creates a new API server with full configuration

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the http.Handler with all middleware applied

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(addr string) error

ListenAndServe starts the HTTP server on the specified address

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server without interrupting active connections

type ServerConfig

type ServerConfig struct {
	DB               store.DB
	Logger           logger.Logger
	KVStore          kv.Store
	EnvStore         env.Store
	HTTPClient       internalhttp.Client
	AITracker        ai.Tracker
	EmailTracker     email.Tracker
	Scheduler        *internalcron.FunctionScheduler
	ExecutionTimeout time.Duration
	FrontendHandler  http.Handler
	APIKey           string
	BaseURL          string
}

ServerConfig holds configuration for creating a Server

type UpdateEnvVarsRequest

type UpdateEnvVarsRequest struct {
	EnvVars map[string]string `json:"env_vars"`
}

UpdateEnvVarsRequest is the request body for updating environment variables

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError represents a validation error

func (*ValidationError) Error

func (e *ValidationError) Error() string

type VersionDiffResponse

type VersionDiffResponse struct {
	OldVersion int        `json:"old_version"`
	NewVersion int        `json:"new_version"`
	Diff       []DiffLine `json:"diff"`
}

VersionDiffResponse is the response for version diff

Jump to

Keyboard shortcuts

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