server

package
v0.0.0-...-528a4d7 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: EUPL-1.2 Imports: 24 Imported by: 0

Documentation

Overview

Package server provides an HTTP server for serving static files.

Index

Constants

View Source
const AssetsPrefix = "/.h2static-assets/"

AssetsPrefix defines the URL prefix for static assets.

View Source
const CSSAsset = AssetsPrefix + "style.css"

CSSAsset defines the path of the CSS file.

View Source
const LogoAsset = AssetsPrefix + "logo.svg"

LogoAsset defines the path of the logo.

Variables

This section is empty.

Functions

func AddHeadersHandler

func AddHeadersHandler(headers map[string]string, h http.Handler) http.Handler

AddHeadersHandler wraps an http.Handler adding headers.

func AssetsHandler

func AssetsHandler() http.Handler

AssetsHandler serves static assets for the server.

Types

type BasicAuthHandler

type BasicAuthHandler struct {
	http.Handler

	// User/password pairs
	Credentials map[string]string
	// The authentication realm
	Realm string
}

BasicAuthHandler provides Basic Authorization.

func (BasicAuthHandler) ServeHTTP

func (h BasicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP logs server startup and serves via the configured handler.

type DirEntryInfo

type DirEntryInfo struct {
	Name      string
	IsDir     bool
	Size      int64
	HumanSize humanSizeInfo `json:"-"`
}

DirEntryInfo holds details for a directory entry.

type DirInfo

type DirInfo struct {
	Name    string
	IsRoot  bool
	Entries []DirEntryInfo
}

DirInfo holds details about the directory being listed.

type DirectoryListingTemplate

type DirectoryListingTemplate struct {
	Config DirectoryListingTemplateConfig
	// contains filtered or unexported fields
}

DirectoryListingTemplate is a template rendered for a directory.

func NewDirectoryListingTemplate

func NewDirectoryListingTemplate(config DirectoryListingTemplateConfig) *DirectoryListingTemplate

NewDirectoryListingTemplate returns a DirectoryListingTemplate for the specified directory.

func (*DirectoryListingTemplate) RenderHTML

func (t *DirectoryListingTemplate) RenderHTML(w http.ResponseWriter, path string, dir *File, sortColumn string, sortAsc bool) error

RenderHTML renders the HTML template for a directory.

func (*DirectoryListingTemplate) RenderJSON

func (t *DirectoryListingTemplate) RenderJSON(w http.ResponseWriter, path string, dir *File, sortColumn string, sortAsc bool) error

RenderJSON returns JSON listing for a directory.

type DirectoryListingTemplateConfig

type DirectoryListingTemplateConfig struct {
	PathPrefix string
}

DirectoryListingTemplateConfig holds configuration for a DirectoryListingTemplate

type File

type File struct {
	Info os.FileInfo
	// contains filtered or unexported fields
}

File is an entry of a FileSystem entry.

If the entry is a directory and the filesystem is configured to hide dotfiles, the directory will also not list dotfiles under it.

func NewFile

func NewFile(absPath string, hideDotFiles bool) (*File, error)

NewFile returns a File for an absolute path.

func (File) AbsPath

func (f File) AbsPath() string

AbsPath returns the absolute path of the File.

func (File) Readdir

func (f File) Readdir() ([]*File, error)

Readdir files in the directory, excluding special files and optionally hidden files (that start with a dot).

type FileHandler

type FileHandler struct {
	FileSystem     FileSystem
	DirectoryIndex bool
	// contains filtered or unexported fields
}

FileHandler is an http.Handler which serves static files under the specified filesystem.

func NewFileHandler

func NewFileHandler(fileSystem FileSystem, directoryIndex bool, pathPrefix string) *FileHandler

NewFileHandler returns a FileHandler for the specified filesystem.

func (FileHandler) ServeHTTP

func (f FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles a request for the static file serve.

type FileSize

type FileSize float64

FileSize represent a file size as a float number.

func (FileSize) String

func (f FileSize) String() string

String returns a string representation of the FileSize.

type FileSystem

type FileSystem struct {
	ResolveHTML          bool
	HideDotFiles         bool
	AllowOutsideSymlinks bool
	Root                 string
}

FileSystem provides acess to files and directories under a certain root. It can optionally optionally:

  • serve .htm(l) files for the corresponding path without suffix, if the original path is not found
  • hide dotfiles
  • allow access to file/directories outside the filesystem root via symlinks

func (FileSystem) Open

func (fs FileSystem) Open(name string) (*File, error)

Open returns a File object for the specified path under the FileSystem directory.

func (FileSystem) OpenFile

func (fs FileSystem) OpenFile(name string) (*File, error)

OpenFile returns a File object for the specified path under the FileSystem directory if it esists and it's not a directory.

type LoggingHandler

type LoggingHandler struct {
	http.Handler
}

LoggingHandler wraps an http.Handler providing logging at startup.

func (LoggingHandler) ServeHTTP

func (h LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP logs server startup and serves via the configured handler.

type StaticServer

type StaticServer struct {
	Config StaticServerConfig
}

StaticServer is a static HTTP server.

func NewStaticServer

func NewStaticServer(config StaticServerConfig) (*StaticServer, error)

NewStaticServer returns a StaticServer.

func (*StaticServer) Run

func (s *StaticServer) Run() error

Run starts the server.

func (*StaticServer) Scheme

func (s *StaticServer) Scheme() string

Scheme returns the server scheme (http or https)

type StaticServerConfig

type StaticServerConfig struct {
	Addr                    string
	AllowOutsideSymlinks    bool
	CSS                     string
	DebugAddr               string
	Dir                     string
	DisableH2               bool
	DisableIndex            bool
	DisableLookupWithSuffix bool
	Log                     bool
	PasswordFile            string
	RequestPathPrefix       string
	ShowDotFiles            bool
	TLSCert                 string
	TLSKey                  string
}

StaticServerConfig holds configuration options for a StaticServer.

func (StaticServerConfig) IsHTTPS

func (c StaticServerConfig) IsHTTPS() bool

IsHTTPS returns whether HTTPS is enabled in the config.

func (StaticServerConfig) Port

func (c StaticServerConfig) Port() uint16

Port returns the port from the config.

func (StaticServerConfig) Validate

func (c StaticServerConfig) Validate() error

Validate raises an error if StaticServerConfig is invalid.

Jump to

Keyboard shortcuts

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