server

package
v0.0.0-...-6aaa5b3 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2025 License: GPL-3.0 Imports: 46 Imported by: 0

Documentation

Overview

Package server contains the REST API server, routers, and handlers.

Index

Constants

View Source
const (
	CtxKeyGameNoCount         = "game_no_count"
	CtxKeyGameMinData         = "game_min_data"
	CtxKeyGameAllowPreviousID = "game_allow_previous_id"
	CtxKeyGameAllowTags       = "game_allow_tags"
)

Context keys.

Variables

View Source
var Version = ""

The server version.

Functions

This section is empty.

Types

type Account

type Account struct {
	ID               request.FieldString `bson:"id"                 json:"id"                 yaml:"id"`
	Name             request.FieldString `bson:"name"               json:"name"               yaml:"name"`
	Status           request.FieldString `bson:"status"             json:"status"             yaml:"status"`
	StatusData       request.FieldJSON   `bson:"status_data"        json:"status_data"        yaml:"status_data"`
	Repo             request.FieldString `bson:"repo"               json:"repo"               yaml:"repo"`
	RepoStatus       request.FieldString `bson:"repo_status"        json:"repo_status"        yaml:"repo_status"`
	RepoStatusData   request.FieldJSON   `bson:"repo_status_data"   json:"repo_status_data"   yaml:"repo_status_data"`
	GameCommitHash   request.FieldString `bson:"game_commit_hash"   json:"game_commit_hash"   yaml:"game_commit_hash"`
	GameLimit        request.FieldInt64  `bson:"game_limit"         json:"game_limit"         yaml:"game_limit"`
	Secret           request.FieldString `bson:"secret"             json:"secret"             yaml:"secret"`
	AIAPIKey         request.FieldString `bson:"ai_api_key"         json:"ai_api_key"         yaml:"ai_api_key"`
	AIMaxTokens      request.FieldInt64  `bson:"ai_max_tokens"      json:"ai_max_tokens"      yaml:"ai_max_tokens"`
	AIThinkingBudget request.FieldInt64  `bson:"ai_thinking_budget" json:"ai_thinking_budget" yaml:"ai_thinking_budget"`
	Data             request.FieldJSON   `bson:"data"               json:"data"               yaml:"data"`
	CreatedAt        request.FieldTime   `bson:"created_at"         json:"created_at"         yaml:"created_at"`
	UpdatedAt        request.FieldTime   `bson:"updated_at"         json:"updated_at"         yaml:"updated_at"`
}

Account values represent account data.

func (*Account) Validate

func (a *Account) Validate() error

Validate checks that the value contains valid data.

func (*Account) ValidateCreate

func (a *Account) ValidateCreate() error

ValidateCreate checks that the value contains valid data for creation.

type Claims

type Claims struct {
	AccountID   string `json:"account_id"`
	AccountName string `json:"account_name"`
	UserID      string `json:"user_id"`
	Scopes      string `json:"scopes"`
}

Claims values contain token claims information.

type Game

type Game struct {
	AccountID   request.FieldString      `bson:"account_id"  json:"account_id"  yaml:"account_id"`
	Debug       request.FieldBool        `bson:"debug"       json:"debug"       yaml:"debug"`
	Pause       request.FieldBool        `bson:"pause"       json:"pause"       yaml:"pause"`
	Public      request.FieldBool        `bson:"public"      json:"public"      yaml:"public"`
	W           request.FieldInt64       `bson:"w"           json:"w"           yaml:"w"`
	H           request.FieldInt64       `bson:"h"           json:"h"           yaml:"h"`
	ID          request.FieldString      `bson:"id"          json:"id"          yaml:"id"`
	PreviousID  request.FieldString      `bson:"previous_id" json:"previous_id" yaml:"previous_id"`
	Name        request.FieldString      `bson:"name"        json:"name"        yaml:"name"`
	Version     request.FieldString      `bson:"version"     json:"version"     yaml:"version"`
	Description request.FieldString      `bson:"description" json:"description" yaml:"description"`
	Icon        request.FieldString      `bson:"icon"        json:"icon"        yaml:"icon"`
	Status      request.FieldString      `bson:"status"      json:"status"      yaml:"status"`
	StatusData  request.FieldJSON        `bson:"status_data" json:"status_data" yaml:"status_data"`
	Subject     request.FieldJSON        `bson:"subject"     json:"subject"     yaml:"subject"`
	Objects     request.FieldJSON        `bson:"objects"     json:"objects"     yaml:"objects"`
	Images      request.FieldJSON        `bson:"images"      json:"images"      yaml:"images"`
	Script      request.FieldString      `bson:"script"      json:"script"      yaml:"script"`
	Source      request.FieldString      `bson:"source"      json:"source"      yaml:"source"`
	CommitHash  request.FieldString      `bson:"commit_hash" json:"commit_hash" yaml:"commit_hash"`
	Tags        request.FieldStringArray `bson:"tags"        json:"tags"        yaml:"tags"`
	Prompts     request.FieldJSON        `bson:"prompts"     json:"prompts"     yaml:"prompts"`
	CreatedAt   request.FieldTime        `bson:"created_at"  json:"created_at"  yaml:"created_at"`
	CreatedBy   request.FieldString      `bson:"created_by"  json:"created_by"  yaml:"created_by"`
	UpdatedAt   request.FieldTime        `bson:"updated_at"  json:"updated_at"  yaml:"updated_at"`
	UpdatedBy   request.FieldString      `bson:"updated_by"  json:"updated_by"  yaml:"updated_by"`
}

Game values represent game state data.

func (*Game) Validate

func (g *Game) Validate() error

Validate checks that the value contains valid data.

func (*Game) ValidateCreate

func (g *Game) ValidateCreate() error

ValidateCreate checks that the value contains valid data for creation.

type HealthCheck

type HealthCheck struct {
	Service   string `json:"service,omitempty"`
	Version   string `json:"version,omitempty"`
	CommitID  string `json:"commit_id,omitempty"`
	BuildTime string `json:"build_time,omitempty"`
	Health    uint32 `json:"health,omitempty"`
}

HealthCheck values represent return information from health checks.

type Prompt

type Prompt struct {
	Prompt   request.FieldString `bson:"prompt"   json:"prompt"   yaml:"prompt"`
	Response request.FieldString `bson:"response" json:"response" yaml:"response"`
	Thinking request.FieldString `bson:"thinking" json:"thinking" yaml:"thinking"`
}

Prompt values represent a single AI prompt and response.

type Prompter

type Prompter interface {
	Prompt(ctx context.Context,
		prompts *Prompts,
		state *Game,
	) error
}

Prompter values are able to send prompts to AI services.

func NewAnthropicPrompter

func NewAnthropicPrompter(s *Server,
	key string,
	maxTokens, budgetTokens int64,
) Prompter

NewMockPrompter creates a new mock prompter with the given response, state and delay.

func NewMockPrompter

func NewMockPrompter(s *Server,
	res string,
	delay time.Duration,
) Prompter

NewMockPrompter creates a new mock prompter with the given response, state and delay.

type Prompts

type Prompts struct {
	Current Prompt              `bson:"current" json:"current" yaml:"current"`
	History []Prompt            `bson:"history" json:"history" yaml:"history"`
	Error   request.FieldString `bson:"error"   json:"error"   yaml:"error"`
	GameID  request.FieldString `bson:"game_id" json:"game_id" yaml:"game_id"`
}

Prompts values contain the AI prompt data for a game.

func (*Prompts) Copy

func (p *Prompts) Copy() *Prompts

Copy creates a copy of the Prompts struct.

type Server

type Server struct {
	http.Server
	sync.RWMutex
	// contains filtered or unexported fields
}

Server values implement HTTP server functionality.

func NewServer

func NewServer(cfg *config.Config,
	log logger.Logger,
	metric metric.Recorder,
	tracer trace.Tracer,
) (*Server, error)

NewServer creates a new HTTP server.

func (*Server) Cache

func (s *Server) Cache(ctx context.Context) cache.Accessor

Cache gets the server cache for a specific request.

func (*Server) Close

func (s *Server) Close()

Close releases all server games immediately.

func (*Server) ConnectDB

func (s *Server) ConnectDB()

ConnectDB connects to the NoSQL database.

func (*Server) DB

func (s *Server) DB() *mongo.Database

DB gets the database used by for the server.

func (*Server) Health

func (s *Server) Health() uint32

Health gets the status code for the current server health.

func (*Server) HealthHandler

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

HealthHandler returns a route handler for /health requests.

func (*Server) Mux

func (s *Server) Mux(w http.ResponseWriter, r *http.Request)

Mux routes and serves a request.

func (*Server) Serve

func (s *Server) Serve() error

Serve listens for and processes HTTP requests.

func (*Server) SetCache

func (s *Server) SetCache(cache cache.Accessor)

SetCache sets the database client for the server.

func (*Server) SetDB

func (s *Server) SetDB(db *mongo.Client)

SetDB sets the database client for the server.

func (*Server) SetHealth

func (s *Server) SetHealth(code uint32)

SetHealth sets the status code for the current server health.

func (*Server) SetPrompter

func (s *Server) SetPrompter(p Prompter)

SetPrompter sets the AI prompt sending interface client.

func (*Server) SetRepoClient

func (s *Server) SetRepoClient(cli repo.Client)

SetRepoClient sets the git repository client to be used for imports.

func (*Server) Shutdown

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

Shutdown releases all server games gracefully.

func (*Server) UpdateAuthConfig

func (s *Server) UpdateAuthConfig()

UpdateAuthConfig retrieves and begins periodic update of authentication configuration data, if configured to do so.

func (*Server) UpdateGameImports

func (s *Server) UpdateGameImports()

UpdateGameImports periodically checks the import repository for game updates.

func (*Server) UpdateGamePrompts

func (s *Server) UpdateGamePrompts()

UpdateGamePrompts periodically updates pending game prompts.

func (*Server) UpdateMetrics

func (s *Server) UpdateMetrics(ctx context.Context,
) error

UpdateMetrics is used to periodically update the service metrics.

type User

type User struct {
	AccountID request.FieldString `bson:"account_id"         json:"account_id"         yaml:"account_id"`
	ID        request.FieldString `bson:"id"                 json:"id"                 yaml:"id"`
	Email     request.FieldString `bson:"email"              json:"email"              yaml:"email"`
	LastName  request.FieldString `bson:"last_name"          json:"last_name"          yaml:"last_name"`
	FirstName request.FieldString `bson:"first_name"         json:"first_name"         yaml:"first_name"`
	Status    request.FieldString `bson:"status"             json:"status"             yaml:"status"`
	Scopes    request.FieldString `bson:"scopes"             json:"scopes"             yaml:"scopes"`
	Data      request.FieldJSON   `bson:"data"               json:"data"               yaml:"data"`
	Password  *string             `bson:"password,omitempty" json:"password,omitempty" yaml:"password,omitempty"`
	CreatedAt request.FieldTime   `bson:"created_at"         json:"created_at"         yaml:"created_at"`
	CreatedBy request.FieldString `bson:"created_by"         json:"created_by"         yaml:"created_by"`
	UpdatedAt request.FieldTime   `bson:"updated_at"         json:"updated_at"         yaml:"updated_at"`
	UpdatedBy request.FieldString `bson:"updated_by"         json:"updated_by"         yaml:"updated_by"`
}

User values represent user data.

func (*User) Validate

func (u *User) Validate() error

Validate checks that the value contains valid data.

func (*User) ValidateCreate

func (u *User) ValidateCreate() error

ValidateCreate checks that the value contains valid data for creation.

Jump to

Keyboard shortcuts

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