static

package module
v0.0.0-...-a58aa74 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: MIT Imports: 16 Imported by: 2

README

static - a simple static web server

Build Status Go Report Card Go Reference

static is a simple static web server which does nothing more than just serve up static files with a configured root path. It also supports Single-Page-App (SPA) mode and CORS as well as configurable index files.

Installation

Source

With a valid Go environment:

go install go.mills.io/static/cmd/static@latest

Or build from source:

git clone https://git.mills.io/prologic/static.git
cd static
make install

Usage

Run static:

static

Then visit: http://localhost:8000/

Configuration

By default static services up files from the current directory. This can be configured via the -r/--root option. For a full list of options see --help:

$ ./static -h
Usage: ./static [options] [file]
  -b, --bind string     [int]:<port> to bind to (default "0.0.0.0:8000")
  -d, --debug           enable debug logging
  -i, --index strings   default index file to serve (default [index.html])
  -n, --no-dir          disable directory listing
  -r, --root string     path to serve (default "/Users/prologic/Projects/static")
  -s, --spa             enable single-page (SPA) mode
  -v, --version         display version information
pflag: help requested

License

static is licensed under the terms of the MIC License

Documentation

Overview

Package static is a simple static web server with some simple useful features such as - Directory listing - Default index files - Single page mode (SPA) - Default CORS headers

Index

Constants

View Source
const (
	// DefaultBind is the default [<address>]:<port> to bind to
	DefaultBind = ":8000"

	// DefaultRoot is the default root directory to serve
	DefaultRoot = "."

	// DefaultCGI is the default for whether or not to enable execution of cgi scripts
	DefaultCGI = false

	// DefaultDir is the default for whether or not to enable directory listing
	DefaultDir = true

	// DefaultSPA is the default for whether or not to enable Single-Page-App (SPA) mode
	DefaultSPA = false
)

Variables

View Source
var (
	// Version is the tagged release version in the form <major>.<minor>.<patch>
	// following semantic versioning and is overwritten by the build system.
	Version = defaultVersion

	// Commit is the commit sha of the build (normally from Git) and is overwritten
	// by the build system.
	Commit = defaultCommit

	// Build is the date and time of the build as an RFC3339 formatted string
	// and is overwritten by the build system.
	Build = defaultBuild
)
View Source
var DefaultIndex = []string{"index.html", "index.html"}

DefaultIndex is the default set of index files to serve

Functions

func FullVersion

func FullVersion() string

FullVersion display the full version and build

func IsExecutable

func IsExecutable(mode os.FileMode) bool

IsExecutable returns true if the execution bits are set for the file mode

Types

type Middleware

type Middleware func(httprouter.Handle) httprouter.Handle

Middleware ...

type Option

type Option func(svr *Server) error

Option is a function type that configures the server

func WithBind

func WithBind(bind string) Option

WithBind configures the server with a bind interface and port in the form of [<address>]:<port> For example: WithBind(":8000")

func WithCGI

func WithCGI(cgi bool) Option

WithCGI configures whether or not the server permits execution of cgi scripts

func WithDir

func WithDir(dir bool) Option

WithDir configures whether or not the server permits directory listing

func WithIndex

func WithIndex(index []string) Option

WithIndex sets the server's default set of index pages to look for when serving a directory

func WithRoot

func WithRoot(root string) Option

WithRoot sets the root directory for the server

func WithSPA

func WithSPA(spa bool) Option

WithSPA configures whether or not the server should operate in SPA (single-page-app) mode where requests to non-existent pages are sent to the index page

type Router

type Router struct {
	httprouter.Router
	// contains filtered or unexported fields
}

Router ...

func NewRouter

func NewRouter() *Router

NewRouter ...

func (*Router) DELETE

func (r *Router) DELETE(path string, handle httprouter.Handle)

DELETE is a shortcut for Router.Handle("DELETE", path, handle)

func (*Router) File

func (r *Router) File(path, name string)

File serves the named file.

func (*Router) GET

func (r *Router) GET(path string, handle httprouter.Handle)

GET is a shortcut for Router.Handle("GET", path, handle)

func (*Router) Group

func (r *Router) Group(path string, m ...Middleware) *Router

Group returns new *Router with given path and middlewares. It should be used for handles which have same path prefix or common middlewares.

func (*Router) HEAD

func (r *Router) HEAD(path string, handle httprouter.Handle)

HEAD is a shortcut for Router.Handle("HEAD", path, handle)

func (*Router) Handle

func (r *Router) Handle(method, path string, handle httprouter.Handle)

Handle registers a new request handle combined with middlewares.

func (*Router) Handler

func (r *Router) Handler(method, path string, handler http.Handler)

Handler is an adapter for http.Handler.

func (*Router) HandlerFunc

func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)

HandlerFunc is an adapter for http.HandlerFunc.

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handle httprouter.Handle)

OPTIONS is a shortcut for Router.Handle("OPTIONS", path, handle)

func (*Router) PATCH

func (r *Router) PATCH(path string, handle httprouter.Handle)

PATCH is a shortcut for Router.Handle("PATCH", path, handle)

func (*Router) POST

func (r *Router) POST(path string, handle httprouter.Handle)

POST is a shortcut for Router.Handle("POST", path, handle)

func (*Router) PUT

func (r *Router) PUT(path string, handle httprouter.Handle)

PUT is a shortcut for Router.Handle("PUT", path, handle)

func (*Router) ServeFiles

func (r *Router) ServeFiles(path string, root http.FileSystem)

ServeFiles ...

func (*Router) ServeFilesWithCacheControl

func (r *Router) ServeFilesWithCacheControl(path string, root fs.FS)

ServeFilesWithCacheControl ...

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Router) Static

func (r *Router) Static(path, root string)

Static serves files from given root directory.

func (*Router) Use

func (r *Router) Use(m ...Middleware) *Router

Use appends new middleware to current Router.

type Server

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

Server constructs the static server, configures it and sets up initial routes

func NewServer

func NewServer(opts ...Option) (*Server, error)

NewServer constructs a new server with the provided configuration options

func (*Server) IndexHandler

func (s *Server) IndexHandler() httprouter.Handle

IndexHandler handlers serving up resources and optionally performing directory listening (if enabled)

func (*Server) Run

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

Run runs the server with the provided context and shuts down when the context is cancelled

Directories

Path Synopsis
cmd
static
Package main is a simple static web server
Package main is a simple static web server

Jump to

Keyboard shortcuts

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