server: github.com/fragmenta/server Index | Files | Directories

package server

import "github.com/fragmenta/server"

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

Index

Package Files

deprecated.go errors.go headers.go redirects.go server.go

func AddCacheHeaders Uses

func AddCacheHeaders(w http.ResponseWriter, days int, hash string)

AddCacheHeaders adds Cache-Control, Expires and Etag headers using the age in days and content hash provided

func Redirect Uses

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 Uses

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 Uses

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

type Logger Uses

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

Logger interface for a logger - deprecated for 2.0

type Server Uses

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 Uses

func New() (*Server, error)

New creates a new server instance

func (*Server) Config Uses

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

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

func (*Server) ConfigBool Uses

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 Uses

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

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

func (*Server) Configuration Uses

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

Configuration returns the map of configuration keys to values

func (*Server) ConfiguredTLSServer Uses

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 Uses

func (s *Server) Fatal(format string)

Fatal logs the message, and then exits with status 1

func (*Server) Fatalf Uses

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 Uses

func (s *Server) Log(message string)

Log logs the message to our internal logger

func (*Server) Logf Uses

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

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

func (*Server) Mode Uses

func (s *Server) Mode() string

Mode returns the mode (production or development)

func (*Server) Port Uses

func (s *Server) Port() int

Port returns the port of the server

func (*Server) PortString Uses

func (s *Server) PortString() string

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

func (*Server) Production Uses

func (s *Server) Production() bool

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

func (*Server) SetProduction Uses

func (s *Server) SetProduction(value bool)

SetProduction sets the mode manually to SetProduction

func (*Server) Start Uses

func (s *Server) Start() error

Start starts an http server on the given port

func (*Server) StartRedirectAll Uses

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

StartRedirectAll starts redirecting all requests on the given port to the given host this should be called before StartTLS if redirecting http on port 80 to https

func (*Server) StartTLS Uses

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

StartTLS starts an https server on the given port with tls cert/key from config keys. Settings based on an article by Filippo Valsorda. https://blog.cloudflare.com/exposing-go-on-the-internet/

func (*Server) StartTLSAutocert Uses

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 Uses

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 Uses

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 Uses

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

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

func Error Uses

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 Uses

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 Uses

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

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

func NotFoundError Uses

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 Uses

func ToStatusError(e error) *StatusError

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

func (*StatusError) Error Uses

func (e *StatusError) Error() string

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

func (*StatusError) FileLine Uses

func (e *StatusError) FileLine() string

FileLine returns file name and line of error

func (*StatusError) String Uses

func (e *StatusError) String() string

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

Directories

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

Package server imports 13 packages (graph) and is imported by 18 packages. Updated 2018-10-10. Refresh now. Tools for package owners.