server

package
v0.0.0-...-2d58911 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package server provides the HTTP server for the web UI and REST API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Addr               string
	Port               int
	Token              string // HuggingFace token
	ModelsDir          string // Output directory for models (not configurable via API)
	DatasetsDir        string // Output directory for datasets (not configurable via API)
	Concurrency        int
	MaxActive          int
	MultipartThreshold string   // Minimum size for multipart download
	Verify             string   // Verification mode: none, size, sha256
	Retries            int      // Number of retry attempts
	AllowedOrigins     []string // CORS origins
	Endpoint           string   // Custom HuggingFace endpoint (e.g., for mirrors)
}

Config holds server configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

type DownloadRequest

type DownloadRequest struct {
	Repo               string   `json:"repo"`
	Revision           string   `json:"revision,omitempty"`
	Dataset            bool     `json:"dataset,omitempty"`
	Filters            []string `json:"filters,omitempty"`
	Excludes           []string `json:"excludes,omitempty"`
	AppendFilterSubdir bool     `json:"appendFilterSubdir,omitempty"`
	DryRun             bool     `json:"dryRun,omitempty"`
}

DownloadRequest is the request body for starting a download. Note: Output path is NOT configurable via API for security reasons. The server uses its configured OutputDir (Models/ for models, Datasets/ for datasets).

type ErrorResponse

type ErrorResponse struct {
	Error   string `json:"error"`
	Details string `json:"details,omitempty"`
}

ErrorResponse represents an API error.

type Job

type Job struct {
	ID        string            `json:"id"`
	Repo      string            `json:"repo"`
	Revision  string            `json:"revision"`
	IsDataset bool              `json:"isDataset,omitempty"`
	Filters   []string          `json:"filters,omitempty"`
	Excludes  []string          `json:"excludes,omitempty"`
	OutputDir string            `json:"outputDir"`
	Status    JobStatus         `json:"status"`
	Progress  JobProgress       `json:"progress"`
	Error     string            `json:"error,omitempty"`
	CreatedAt time.Time         `json:"createdAt"`
	StartedAt *time.Time        `json:"startedAt,omitempty"`
	EndedAt   *time.Time        `json:"endedAt,omitempty"`
	Files     []JobFileProgress `json:"files,omitempty"`
	// contains filtered or unexported fields
}

Job represents a download job.

type JobFileProgress

type JobFileProgress struct {
	Path       string `json:"path"`
	TotalBytes int64  `json:"totalBytes"`
	Downloaded int64  `json:"downloaded"`
	Status     string `json:"status"` // pending, active, complete, skipped, error
}

JobFileProgress holds per-file progress.

type JobManager

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

JobManager manages download jobs.

func NewJobManager

func NewJobManager(cfg Config, wsHub *WSHub) *JobManager

NewJobManager creates a new job manager.

func (*JobManager) CancelJob

func (m *JobManager) CancelJob(id string) bool

CancelJob cancels a running or queued job.

func (*JobManager) CreateJob

func (m *JobManager) CreateJob(req DownloadRequest) (*Job, bool, error)

CreateJob creates a new download job. Returns existing job if same repo+revision+dataset is already in progress.

func (*JobManager) DeleteJob

func (m *JobManager) DeleteJob(id string) bool

DeleteJob removes a job from the list.

func (*JobManager) GetJob

func (m *JobManager) GetJob(id string) (*Job, bool)

GetJob retrieves a job by ID.

func (*JobManager) ListJobs

func (m *JobManager) ListJobs() []*Job

ListJobs returns all jobs.

func (*JobManager) Subscribe

func (m *JobManager) Subscribe() chan *Job

Subscribe adds a listener for job updates.

func (*JobManager) Unsubscribe

func (m *JobManager) Unsubscribe(ch chan *Job)

Unsubscribe removes a listener.

type JobProgress

type JobProgress struct {
	TotalFiles      int   `json:"totalFiles"`
	CompletedFiles  int   `json:"completedFiles"`
	TotalBytes      int64 `json:"totalBytes"`
	DownloadedBytes int64 `json:"downloadedBytes"`
	BytesPerSecond  int64 `json:"bytesPerSecond"`
}

JobProgress holds aggregate progress info.

type JobStatus

type JobStatus string

JobStatus represents the state of a download job.

const (
	JobStatusQueued    JobStatus = "queued"
	JobStatusRunning   JobStatus = "running"
	JobStatusCompleted JobStatus = "completed"
	JobStatusFailed    JobStatus = "failed"
	JobStatusCancelled JobStatus = "cancelled"
)

type PlanFile

type PlanFile struct {
	Path string `json:"path"`
	Size int64  `json:"size"`
	LFS  bool   `json:"lfs"`
}

PlanFile represents a file in the plan.

type PlanResponse

type PlanResponse struct {
	Repo       string     `json:"repo"`
	Revision   string     `json:"revision"`
	Files      []PlanFile `json:"files"`
	TotalSize  int64      `json:"totalSize"`
	TotalFiles int        `json:"totalFiles"`
}

PlanResponse is the response for a dry-run/plan request.

type Server

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

Server is the HTTP server for hfdownloader.

func New

func New(cfg Config) *Server

New creates a new server with the given configuration.

func (*Server) ListenAndServe

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

ListenAndServe starts the HTTP server.

type SettingsResponse

type SettingsResponse struct {
	Token              string `json:"token,omitempty"`
	ModelsDir          string `json:"modelsDir"`
	DatasetsDir        string `json:"datasetsDir"`
	Concurrency        int    `json:"connections"`
	MaxActive          int    `json:"maxActive"`
	MultipartThreshold string `json:"multipartThreshold"`
	Verify             string `json:"verify"`
	Retries            int    `json:"retries"`
	Endpoint           string `json:"endpoint,omitempty"`
}

SettingsResponse represents current settings.

type SuccessResponse

type SuccessResponse struct {
	Success bool   `json:"success"`
	Message string `json:"message,omitempty"`
}

SuccessResponse represents a simple success message.

type WSClient

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

WSClient represents a connected WebSocket client.

type WSHub

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

WSHub manages WebSocket clients and broadcasts.

func NewWSHub

func NewWSHub() *WSHub

NewWSHub creates a new WebSocket hub.

func (*WSHub) Broadcast

func (h *WSHub) Broadcast(msgType string, data any)

Broadcast sends a message to all connected clients.

func (*WSHub) BroadcastEvent

func (h *WSHub) BroadcastEvent(event any)

BroadcastEvent sends a progress event to all clients.

func (*WSHub) BroadcastJob

func (h *WSHub) BroadcastJob(job *Job)

BroadcastJob sends a job update to all clients.

func (*WSHub) ClientCount

func (h *WSHub) ClientCount() int

ClientCount returns the number of connected clients.

func (*WSHub) Run

func (h *WSHub) Run()

Run starts the hub's main loop.

type WSMessage

type WSMessage struct {
	Type string `json:"type"`
	Data any    `json:"data"`
}

WSMessage represents a message sent over WebSocket.

Jump to

Keyboard shortcuts

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