webserver

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: BSD-2-Clause Imports: 14 Imported by: 0

README

Tiny Web Server

An extremely simple web server module, for efficiently serving static files over HTTP. TLS and GZip encoding is supported, and configurable through the command line or config files.

Standalone Server

A standalone server application is provided, allowing you to serve files from a given directory. The default config file path is /etc/tiny-web-server.conf, and serves files from /srv/www

Config Files

The ini format is used, all settings are under the server section, allowing you to add sections for your own application, each setting is listed below.

Name Type Description
address string Address the server is bound to.
port int Port number used.
static string Directory path of file served.
name string Name to identify the server.
cert string File path of TLS certificate.
key string File path of TLS private key.
http-to-https boolean Redirect http requests to https.
gzip boolean Enable GZip encoding.

If a certificate and key is provided, then TLS will automatically be enabled.

Go Module

This server is mainly intended to be embedded. For example, to provide a web interface, or host a WebSocket server.

API
  • Listen(Config, http.HandlerFunc) error
    • Start listening for incoming requests, responding with the handler provided.
  • Handler(Config) http.HandlerFunc
    • Create a handler for serving your static files.
  • DefaultConfig() Config
    • Provides a Config with default settings.
  • FileConfig(*ini.File, Config) Config
    • Read a config file, using the settings in the given Config as defaults.
  • CommandLineConfig(Config) Config
    • Read command line options, using the settings in the given Config as defaults.

Documentation

Index

Constants

View Source
const (
	NotFoundRedirect = NotFoundResponseKind(iota)
	NotFoundPage
)
View Source
const DefaultPort = 80
View Source
const DefaultTLSPort = 443

Variables

This section is empty.

Functions

func Handler

func Handler(config Config) http.HandlerFunc

func HandlerWithOnNotFound added in v0.0.6

func HandlerWithOnNotFound(config Config, onNotFound func(string) NotFoundResponse) http.HandlerFunc

func Listen

func Listen(config Config, handler http.HandlerFunc) error

Types

type Config

type Config struct {
	Address         string
	Port            uint
	StaticFilesPath string
	ServerName      string

	CertFilePath string
	KeyFilePath  string

	EnableHttpToHttps bool
	EnableGzip        bool
}

func CommandLineConfig

func CommandLineConfig(config Config) Config

func DefaultConfig

func DefaultConfig() Config

func FileConfig

func FileConfig(configFile *ini.File, config Config) Config

type NotFoundResponse added in v0.0.6

type NotFoundResponse struct {
	Kind        NotFoundResponseKind
	RedirectURL string
}

type NotFoundResponseKind added in v0.0.6

type NotFoundResponseKind int

Jump to

Keyboard shortcuts

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