ezserver

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

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

Go to latest
Published: Aug 4, 2022 License: BSD-2-Clause Imports: 10 Imported by: 1

README

ezserver

This is an easy-to-use wrapper around Go's HTTP and HTTPS servers.

Demonstrations

Checkout the demo folder for some samples.

License

ezserver is licensed under the BSD 2-clause license. See LICENSE.

Copyright (c) 2014-2015, Alex Nichol.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyListening = errors.New("Already listening.")
	ErrInvalidPort      = errors.New("Invalid port.")
	ErrNotListening     = errors.New("Not listening.")
	ErrInvalidRootCA    = errors.New("Invalid root CA.")
)

Errors relating to HTTP or HTTPS servers.

Functions

This section is empty.

Types

type AutocertHandler

type AutocertHandler func(w http.ResponseWriter, r *http.Request) bool

An AutocertHandler is a function which handles certain HTTP requests for ACME verification.

It returns false if the request was not handled.

type HTTP

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

HTTP is an HTTP server instance which can listen on one port at a time.

func NewHTTP

func NewHTTP(handler http.Handler) *HTTP

NewHTTP creates a new HTTP server with a given handler. The server will not be started.

func (*HTTP) AutocertHandler

func (h *HTTP) AutocertHandler() AutocertHandler

AutocertHandler returns the current AutocertHandler.

See SetAutocertHandler for more.

func (*HTTP) IsRunning

func (h *HTTP) IsRunning() bool

IsRunning returns whether or not the server is accepting connections.

func (*HTTP) SecurityRedirects

func (h *HTTP) SecurityRedirects() []string

SecurityRedirects returns the list of hosts that are redirected to use a different amount of security. For HTTP servers, redirects go to HTTPS. For HTTPS servers, redirects go to HTTP.

The returned slice is a copy of the original, so the caller may modify it without changing the redirects used by the server.

func (*HTTP) SetAutocertHandler

func (h *HTTP) SetAutocertHandler(handler AutocertHandler)

SetAutocertHandler sets the current AutocertHandler.

This is set on HTTP handlers to allow them to pass requests to an HTTPS handler.

func (*HTTP) SetSecurityRedirects

func (h *HTTP) SetSecurityRedirects(r []string)

SetSecurityRedirects sets the list of hosts that are redirected to use a different amount of security.

The slice is copied before being adopted by the server, so the caller may modify it without changing the redirects used by the server.

See SecurityRedirects for more.

func (*HTTP) Start

func (h *HTTP) Start(port int) error

Start runs the HTTP server on a given port.

func (*HTTP) Status

func (h *HTTP) Status() (bool, int)

Status returns whether or not the server is running and the port on which it is listening (if applicable).

func (*HTTP) Stop

func (h *HTTP) Stop() error

Stop stops the HTTP server. This method will only return once the running HTTP server has stopped accepting connections.

func (*HTTP) Wait

func (h *HTTP) Wait() error

Wait waits for the HTTP server to stop and then returns.

type HTTPS

type HTTPS struct {
	*HTTP
	// contains filtered or unexported fields
}

HTTPS is an HTTPS server instance which can listen on one port at a time.

func NewHTTPS

func NewHTTPS(handler http.Handler, config *TLSConfig) *HTTPS

NewHTTPS creates a new HTTPS server with a given handler. The server will not be started.

func (*HTTPS) HandleAutocertRequest

func (h *HTTPS) HandleAutocertRequest(w http.ResponseWriter, r *http.Request) bool

HandleAutocertRequest handles HTTP requests to verify with an ACME authority that we own a domain. If the request is not a verification request, false is returned and a downstream handler should be used.

func (*HTTPS) SetTLSConfig

func (h *HTTPS) SetTLSConfig(c *TLSConfig) error

SetTLSConfig sets the TLSConfig on the server. This may stop and restart the server.

func (*HTTPS) Start

func (h *HTTPS) Start(port int) error

Start runs the HTTP server on a given port.

func (*HTTPS) TLSConfig

func (h *HTTPS) TLSConfig() *TLSConfig

TLSConfig returns the TLSConfig for the server.

type KeyCert

type KeyCert struct {
	Key         string `json:"key"`
	Certificate string `json:"certificate"`
}

KeyCert represents a TLS key/centificate pair.

type Server

type Server interface {
	// IsRunning returns true if the server is running.
	IsRunning() bool

	// Start starts the server if it is not running.
	Start(port int) error

	// Status returns the status of the server.
	// The first return value is the result of IsRunning().
	// The second return value is the port on which the server is listenening if
	// it is running.
	Status() (bool, int)

	// Stop stops the server.
	Stop() error

	// Wait waits for the server to be stopped.
	Wait() error
}

Server serves some sort of web content on a specified port.

type TLSConfig

type TLSConfig struct {
	Named   map[string]KeyCert `json:"named"`
	RootCAs []string           `json:"root_ca"`
	Default KeyCert            `json:"default"`

	ACMEDirectoryURL string   `json:"acme_dir_url"`
	ACMEHosts        []string `json:"acme_hosts"`
	ACMECacheDir     string   `json:"acme_cache_dir"`
}

TLSConfig represents the TLS configuration for an HTTPS server.

func (*TLSConfig) Clone

func (c *TLSConfig) Clone() *TLSConfig

Clone produces a deep copy of a TLSConfig object.

func (*TLSConfig) ToConfig

func (c *TLSConfig) ToConfig() (*tls.Config, *autocert.Manager, error)

ToConfig turns a TLSConfig into a tls.Config.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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