handler

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserNotFound      = errors.New("user not found")
	ErrUserAlreadyExists = errors.New("user already exists")
)

Functions

This section is empty.

Types

type AIHandler

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

AIHandler manages HTTP requests for AI capabilities.

func NewAIHandler

func NewAIHandler(service AIService, logger Logger) *AIHandler

NewAIHandler initializes a new AIHandler with necessary dependencies.

func (*AIHandler) HandleChatCompletion

func (h *AIHandler) HandleChatCompletion(w http.ResponseWriter, r *http.Request)

HandleChatCompletion processes a standard request-response chat interaction.

func (*AIHandler) HandleGenerateImage

func (h *AIHandler) HandleGenerateImage(w http.ResponseWriter, r *http.Request)

HandleGenerateImage processes image generation requests.

func (*AIHandler) HandleStreamChatCompletion

func (h *AIHandler) HandleStreamChatCompletion(w http.ResponseWriter, r *http.Request)

HandleStreamChatCompletion handles Server-Sent Events (SSE) for real-time chat streaming.

func (*AIHandler) RegisterRoutes

func (h *AIHandler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes attaches the handlers to a standard http.ServeMux. This follows Go 1.22+ routing patterns.

type AIService

type AIService interface {
	GenerateChatCompletion(ctx context.Context, req ChatRequest) (*ChatResponse, error)
	StreamChatCompletion(ctx context.Context, req ChatRequest) (<-chan ChatStreamChunk, error)
	GenerateImage(ctx context.Context, req ImageRequest) (*ImageResponse, error)
}

AIService defines the business logic for AI operations. In a full project, this would likely be imported from internal/service or internal/core/ports.

type ChatMessage

type ChatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type ChatRequest

type ChatRequest struct {
	Model       string        `json:"model"`
	Messages    []ChatMessage `json:"messages"`
	Temperature float64       `json:"temperature,omitempty"`
	MaxTokens   int           `json:"max_tokens,omitempty"`
}

type ChatResponse

type ChatResponse struct {
	ID      string      `json:"id"`
	Object  string      `json:"object"`
	Created int64       `json:"created"`
	Message ChatMessage `json:"message"`
	Usage   Usage       `json:"usage"`
}

type ChatStreamChunk

type ChatStreamChunk struct {
	ID      string `json:"id"`
	Content string `json:"content"` // Delta content
	Done    bool   `json:"done"`
	Error   error  `json:"-"` // Internal error passing
}

type CreateUserRequest

type CreateUserRequest struct {
	Email    string `json:"email" validate:"required,email"`
	Name     string `json:"name" validate:"required,min=2,max=100"`
	Password string `json:"password" validate:"required,min=8"`
}

CreateUserRequest defines the payload for creating a user.

type ErrorResponse

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

type ImageItem

type ImageItem struct {
	URL     string `json:"url,omitempty"`
	B64JSON string `json:"b64_json,omitempty"`
}

type ImageRequest

type ImageRequest struct {
	Prompt string `json:"prompt"`
	Size   string `json:"size,omitempty"` // e.g., "1024x1024"
	N      int    `json:"n,omitempty"`
}

type ImageResponse

type ImageResponse struct {
	Created int64       `json:"created"`
	Data    []ImageItem `json:"data"`
}

type ListUsersResponse

type ListUsersResponse struct {
	Data []UserResponse `json:"data"`
	Meta PaginationMeta `json:"meta"`
}

ListUsersResponse defines the paginated response.

type Logger

type Logger interface {
	Info(msg string, args ...any)
	Error(msg string, args ...any)
	Debug(msg string, args ...any)
}

Logger defines a structured logger interface.

type PaginationMeta

type PaginationMeta struct {
	CurrentPage int   `json:"current_page"`
	PageSize    int   `json:"page_size"`
	TotalItems  int64 `json:"total_items"`
	TotalPages  int   `json:"total_pages"`
}

type UpdateUserRequest

type UpdateUserRequest struct {
	Email string `json:"email" validate:"omitempty,email"`
	Name  string `json:"name" validate:"omitempty,min=2,max=100"`
}

UpdateUserRequest defines the payload for updating a user.

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

type User

type User struct {
	ID        string    `json:"id"`
	Email     string    `json:"email"`
	Name      string    `json:"name"`
	Password  string    `json:"-"` // Never return password
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

User represents the domain model for a user.

type UserHandler

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

UserHandler handles HTTP requests for user resources.

func NewUserHandler

func NewUserHandler(service UserService, logger *slog.Logger) *UserHandler

NewUserHandler creates a new instance of UserHandler.

func (*UserHandler) Create

func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request)

Create handles the creation of a new user.

func (*UserHandler) Delete

func (h *UserHandler) Delete(w http.ResponseWriter, r *http.Request)

Delete removes a user.

func (*UserHandler) Get

func (h *UserHandler) Get(w http.ResponseWriter, r *http.Request)

Get retrieves a user by ID.

func (*UserHandler) List

func (h *UserHandler) List(w http.ResponseWriter, r *http.Request)

List retrieves a paginated list of users.

func (*UserHandler) RegisterRoutes

func (h *UserHandler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers the user endpoints on the provided mux. Uses Go 1.22+ routing patterns.

func (*UserHandler) Update

func (h *UserHandler) Update(w http.ResponseWriter, r *http.Request)

Update modifies an existing user.

type UserResponse

type UserResponse struct {
	ID        string    `json:"id"`
	Email     string    `json:"email"`
	Name      string    `json:"name"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

UserResponse defines the public view of a user.

type UserService

type UserService interface {
	Create(ctx context.Context, user *User) error
	GetByID(ctx context.Context, id string) (*User, error)
	Update(ctx context.Context, user *User) error
	Delete(ctx context.Context, id string) error
	List(ctx context.Context, page, limit int) ([]User, int64, error)
}

UserService defines the business logic for user operations. This interface allows for dependency injection and easy mocking.

Jump to

Keyboard shortcuts

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