fileserver

package
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: BSD-3-Clause Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-http/v4/fileserver

There are many Go HTTP file server tools. This one is mine.

Tools

$> make cli
go build -mod vendor -ldflags="-s -w" -o bin/fileserver cmd/fileserver/main.go
fileserver
$> ./bin/fileserver -h
Usage of ./bin/fileserver:
  -cors-origins string
    	A comma-separated of origins to allow CORS requests from. Required if -enable-cors is set.
  -enable-cors
    	Enable CORS headers on responses.
  -enable-gzip
    	Enable gzip-ed responses.
  -mimetype value
    	One or more key=value pairs mapping a file extension to a specific content (or mime) type to assign for that request
  -prefix string
    	A prefix to append to URL to serve requests from.
  -root string
    	A valid path to serve files from.
  -server-uri string
    	A valid aaronland/go-http-server URI. Registered schemes are: HTTP,HTTPS,LAMBDA,MKCERT,TLS. (default "http://localhost:8080")
Example
$> make debug
fileserver -root ./www \
	-mimetype js=text/javascript \
	-mimetype wasm=application/wasm \
	-enable-cors \
	-cors-origins http://localhost:8080
	
2025/02/21 11:30:54 Serving ./www and listening for requests on http://localhost:8080
Safe defaults

Files are served from -root only. Paths that escape the root (including through symlinks), dotfiles and dot-directories (such as .git and .env), and directories without an index.html (no directory listings) all return a 404. -enable-cors requires -cors-origins; there is no wildcard default.

Documentation

Overview

Package fileserver provides HTTP handlers and middleware for serving files from a local directory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContentTypeHandler

func NewContentTypeHandler(opts *ContentTypeOptions, next http.Handler) (http.Handler, error)

NewContentTypeHandler returns a middleware handler that sets the "Content-Type" header for requests whose URI file extension is a key in 'opts.Matches', before invoking 'next'.

func NewFileServerHandler

func NewFileServerHandler(opts *FileServerOptions) (http.Handler, error)

NewFileServerHandler returns a handler that serves files from the directory 'opts.Root', optionally wrapped with gzip compression and CORS support. It returns an error if 'opts.Root' is not a directory.

func NewWithHeadersHandler

func NewWithHeadersHandler(headers map[string]string, next http.Handler) (http.Handler, error)

NewWithHeadersHandler returns a middleware handler that sets each of the key and value pairs in 'headers' as response headers before invoking 'next'.

Types

type ContentTypeOptions

type ContentTypeOptions struct {
	// Matches maps file extensions (including the leading ".") to the Content-Type header values to set.
	Matches map[string]string
}

ContentTypeOptions defines configuration options for the handler returned by NewContentTypeHandler.

type FileServerOptions

type FileServerOptions struct {
	// Root is the path of the directory to serve files from.
	Root string
	// EnableCORS is a boolean flag to enable CORS support.
	EnableCORS bool
	// CORSOrigins is the list of origins allowed by CORS requests.
	CORSOrigins []string
	// EnableGzip is a boolean flag to enable gzip compression of responses.
	EnableGzip bool
}

FileServerOptions defines configuration options for the handler returned by NewFileServerHandler.

Jump to

Keyboard shortcuts

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