server

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvS2ServerConfig         = "S2_SERVER_CONFIG"
	EnvS2ServerListen         = "S2_SERVER_LISTEN"
	EnvS2ServerType           = "S2_SERVER_TYPE"
	EnvS2ServerRoot           = "S2_SERVER_ROOT"
	EnvS2ServerMaxUploadSize  = "S2_SERVER_MAX_UPLOAD_SIZE"
	EnvS2ServerMaxPreviewSize = "S2_SERVER_MAX_PREVIEW_SIZE"
	EnvS2ServerUser           = "S2_SERVER_USER"
	EnvS2ServerPassword       = "S2_SERVER_PASSWORD" // #nosec G101 -- env var name, not a credential
	EnvS2ServerBuckets        = "S2_SERVER_BUCKETS"
)
View Source
const (
	DefaultMaxUploadSize      = 5 << 30  // 5 GiB — default for osfs/s3 backends.
	DefaultMemfsMaxUploadSize = 16 << 20 // 16 MiB — conservative default for the in-memory backend.
	DefaultMaxPreviewSize     = 10 << 20 // 10 MiB
)

Variables

View Source
var DefaultRoot = "data"

DefaultRoot is the default storage root path used by DefaultConfig when the user does not supply one via -root, -f, or S2_SERVER_ROOT.

It is intentionally a var (not a const) so that binaries can be built with a different default via linker flags, matching the same idiom used for version injection:

go build -ldflags "-X github.com/mojatter/s2/server.DefaultRoot=/var/lib/s2" ./cmd/s2-server

This lets the stock "go install" binary default to a relative "data" directory (so a new user can run s2-server in any directory and immediately see where data lands) while the Docker image is built with /var/lib/s2 baked in.

View Source
var StaticFS embed.FS

Functions

func FilterKeep

func FilterKeep(objs []s2.Object) []s2.Object

FilterKeep removes .keep marker files from a list of objects.

func RegisterHandleFunc

func RegisterHandleFunc(pattern string, handler HandlerFunc)

func RegisterTemplate

func RegisterTemplate(name string)

func Run

func Run(args []string) error

Types

type Buckets

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

func (*Buckets) Create

func (bs *Buckets) Create(ctx context.Context, name string) error

func (*Buckets) CreateFolder

func (bs *Buckets) CreateFolder(ctx context.Context, bucket, key string) error

func (*Buckets) CreatedAt

func (bs *Buckets) CreatedAt(ctx context.Context, name string) time.Time

CreatedAt returns the creation time of a bucket by reading the .keep marker file. If the marker is missing, the current time is returned as a fallback.

func (*Buckets) Delete

func (bs *Buckets) Delete(ctx context.Context, name string) error

func (*Buckets) Exists

func (bs *Buckets) Exists(name string) (bool, error)

func (*Buckets) Get

func (bs *Buckets) Get(ctx context.Context, name string) (s2.Storage, error)

func (*Buckets) Names

func (bs *Buckets) Names() ([]string, error)

type Config

type Config struct {
	s2.Config
	// Listen is the address to listen on.
	Listen string `json:"listen"`
	// MaxUploadSize is the maximum upload size in bytes. When 0, a backend-specific
	// default is used (see EffectiveMaxUploadSize): 5 GiB for osfs/s3, 16 MiB for
	// memfs. The conservative memfs default protects the host from accidental
	// OOM when a large upload targets the in-memory backend; set an explicit
	// value here to override.
	MaxUploadSize int64 `json:"max_upload_size"`
	// MaxPreviewSize is the maximum file size for text preview in bytes (0 = default 10MB).
	MaxPreviewSize int64 `json:"max_preview_size"`
	// User is the username for authentication (Basic Auth for Web Console, Access Key ID for S3 API).
	// When empty, authentication is disabled.
	User string `json:"user"`
	// Password is the password for authentication (Basic Auth password for Web Console, Secret Access Key for S3 API).
	Password string `json:"password"`
	// Buckets is a list of bucket names to create on startup if they don't already exist.
	Buckets []string `json:"buckets"`
}

Config is a configuration for the server.

func DefaultConfig

func DefaultConfig() *Config

func (*Config) EffectiveMaxUploadSize added in v0.2.0

func (cfg *Config) EffectiveMaxUploadSize() int64

EffectiveMaxUploadSize returns the upload size limit to enforce for this configuration. When MaxUploadSize is explicitly set (> 0) it is returned as-is; otherwise a backend-specific default is chosen so that switching Type to memfs does not silently inherit the 5 GiB default.

func (*Config) LoadEnv

func (cfg *Config) LoadEnv() error

func (*Config) LoadFile

func (cfg *Config) LoadFile(filename string) error

type ErrBucketNotFound

type ErrBucketNotFound struct {
	Name string
}

ErrBucketNotFound is returned when a bucket does not exist.

func (*ErrBucketNotFound) Error

func (e *ErrBucketNotFound) Error() string

type Flags

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

Flags holds the parsed command-line arguments for s2-server. Pointer-typed fields distinguish "explicitly set" from "left at default"; only explicitly set flags override values loaded from file/env.

type HandlerFunc

type HandlerFunc func(srv *Server, w http.ResponseWriter, r *http.Request)

type Server

type Server struct {
	Config    *Config
	Template  *template.Template
	Buckets   *Buckets
	StartedAt time.Time // server start time, used as epoch for upload ID generation
}

Server is a web server that provides a Web Console and S3-compatible API for S2.

func NewServer

func NewServer(ctx context.Context, cfg *Config) (*Server, error)

NewServer creates a new server with the specified configuration.

func (*Server) Handler added in v0.0.3

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

Handler builds and returns the HTTP handler without starting a listener.

func (*Server) Start

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

Start starts the server and shuts it down gracefully when ctx is cancelled.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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