server

package module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2017 License: MIT Imports: 13 Imported by: 53

README

server

A simple http server using http.ListenAndServe

There are also packages for:

Config Logging Scheduling

Documentation

Overview

Package server is a wrapper around the stdlib http server and x/autocert pkg.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Redirect added in v1.5.2

func Redirect(w http.ResponseWriter, r *http.Request, path string) error

Redirect uses status 302 StatusFound by default - this is not a permanent redirect We don't accept external or relative paths for security reasons

func RedirectExternal added in v1.5.2

func RedirectExternal(w http.ResponseWriter, r *http.Request, path string) error

RedirectExternal redirects setting the status code (for example unauthorized), but does no checks on the path Use with caution and only on paths *fixed at compile time*.

func RedirectStatus added in v1.5.2

func RedirectStatus(w http.ResponseWriter, r *http.Request, path string, status int) error

RedirectStatus redirects setting the status code (for example unauthorized) We don't accept external or relative paths for security reasons

Types

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

Logger interface for a logger - deprecated for 2.0

type Server

type Server struct {

	// Deprecated Logging - due to be removed in 2.0
	// Instead use the structured logging with server/log
	Logger Logger
	// contains filtered or unexported fields
}

Server wraps the stdlib http server and x/autocert pkg with some setup.

func New

func New() (*Server, error)

New creates a new server instance

func (*Server) Config

func (s *Server) Config(key string) string

Config returns a specific configuration value or "" if no value

func (*Server) ConfigBool

func (s *Server) ConfigBool(key string) bool

ConfigBool returns the current configuration value as bool (yes=true, no=false), or false if no value

func (*Server) ConfigInt

func (s *Server) ConfigInt(key string) int64

ConfigInt returns the current configuration value as int64, or 0 if no value

func (*Server) Configuration

func (s *Server) Configuration() map[string]string

Configuration returns the map of configuration keys to values

func (*Server) ConfiguredTLSServer added in v1.5.1

func (s *Server) ConfiguredTLSServer(certManager *autocert.Manager) *http.Server

ConfiguredTLSServer returns a TLS server instance with a secure config this server has read/write timeouts set to 20 seconds, prefers server cipher suites and only uses certain accelerated curves see - https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/

func (*Server) Fatal

func (s *Server) Fatal(format string)

Fatal logs the message, and then exits with status 1

func (*Server) Fatalf

func (s *Server) Fatalf(format string, v ...interface{})

Fatalf the message with the given arguments to our internal logger, and then exits with status 1

func (*Server) Log

func (s *Server) Log(message string)

Log logs the message to our internal logger

func (*Server) Logf

func (s *Server) Logf(format string, v ...interface{})

Logf logs the message with the given arguments to our internal logger

func (*Server) Mode

func (s *Server) Mode() string

Mode returns the mode (production or development)

func (*Server) Port

func (s *Server) Port() int

Port returns the port of the server

func (*Server) PortString added in v1.5.1

func (s *Server) PortString() string

PortString returns a string port suitable for passing to http.Server

func (*Server) Production

func (s *Server) Production() bool

Production tells the caller if this server is in production mode or not?

func (*Server) Start

func (s *Server) Start() error

Start starts an http server on the given port

func (*Server) StartRedirectAll added in v1.5.1

func (s *Server) StartRedirectAll(p int, url string)

StartRedirectAll starts redirecting from port given to the given url this should be called before StartTLS if redirecting to https

func (*Server) StartTLS added in v1.5.1

func (s *Server) StartTLS(cert, key string) error

StartTLS starts an https server on the given port with tls cert/key from config keys.

func (*Server) StartTLSAutocert added in v1.5.1

func (s *Server) StartTLSAutocert(email string, domains string) error

StartTLSAutocert starts an https server on the given port by requesting certs from an ACME provider. The server must be on a public IP which matches the DNS for the domains.

func (*Server) Timef

func (s *Server) Timef(format string, start time.Time, v ...interface{})

Timef logs a time since starting, when used with defer at the start of a function to time Usage: defer s.Timef("Completed %s in %s",time.Now(),args...)

type StatusError added in v1.5.2

type StatusError struct {
	Err     error
	Status  int
	Title   string
	Message string
	File    string
	Line    int
}

StatusError wraps a std error and stores more information (status code, display title/msg and caller info)

func BadRequestError added in v1.5.2

func BadRequestError(e error, args ...string) *StatusError

BadRequestError returns a new StatusError with Status StatusBadRequest and optional Title and Message

func Error added in v1.5.2

func Error(e error, s int, t string, m string) *StatusError

Error returns a new StatusError with code StatusInternalServerError and a generic message

func InternalError added in v1.5.2

func InternalError(e error, args ...string) *StatusError

InternalError returns a new StatusError with Status StatusInternalServerError and optional Title and Message Usage: return router.InternalError(err)

func NotAuthorizedError added in v1.5.2

func NotAuthorizedError(e error, args ...string) *StatusError

NotAuthorizedError returns a new StatusError with Status StatusUnauthorized and optional Title and Message

func NotFoundError added in v1.5.2

func NotFoundError(e error, args ...string) *StatusError

NotFoundError returns a new StatusError with Status StatusNotFound and optional Title and Message Usage return router.NotFoundError(err,"Optional Title", "Optional user-friendly Message")

func ToStatusError added in v1.5.2

func ToStatusError(e error) *StatusError

ToStatusError returns a *StatusError or wraps a standard error in a 500 StatusError

func (*StatusError) Error added in v1.5.2

func (e *StatusError) Error() string

Error returns the underling error string - it should not be shown in production

func (*StatusError) FileLine added in v1.5.2

func (e *StatusError) FileLine() string

FileLine returns file name and line of error

func (*StatusError) String added in v1.5.2

func (e *StatusError) String() string

String returns a string represenation of this error, useful for debugging

Directories

Path Synopsis
Package config offers utilities for parsing a json config file.
Package config offers utilities for parsing a json config file.
Package log provides a structured, levelled logger interface for use in server handlers, which handles multiple output streams.
Package log provides a structured, levelled logger interface for use in server handlers, which handles multiple output streams.
Package schedule provides a simple way to schedule functions at a time or interval Package schedule provides a simple way to schedule functions at a time or interval
Package schedule provides a simple way to schedule functions at a time or interval Package schedule provides a simple way to schedule functions at a time or interval

Jump to

Keyboard shortcuts

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