server

package
v0.0.0-...-0d1c8f5 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package server implements a simple server to forward dockerhub deploy webhooks to jenkins

Index

Constants

View Source
const (
	ApplicationName = "dockerhub-webhook" // Application name.

	DefaultHostname       = "0.0.0.0"                          // The hostname or address of the server.
	DefaultPort           = 8080                               // Port to receive requests: see IANA Port Numbers.
	DefaultProfPort       = 6060                               // Profiler port to receive requests.*
	DefaultMaxConnections = 0                                  // Maximum number of connections allowed.*
	DefaultMaxProcs       = 0                                  // Maximum number of computer processors to utilize.*
	DefaultNamespace      = "development"                      // Default namespace to process notification into.
	DefaultAlivePath      = "/v1.0/alive"                      // Default path to check health of server.
	DefaultNotifyPath     = "/v1.0/notify"                     // Default path to handle notify events.
	DefaultStatusPath     = "/v1.0/status"                     // Default path to check server status information.
	DefaultTargetHost     = "jenkins"                          // Server hostname or IP to forward to.
	DefaultTargetPort     = 8080                               // Server port to forward to.
	DefaultTargetPath     = "/generic-webhook-trigger/invoke/" // Path to generic jenkins webhook
	DefaultTargetToken    = ""                                 // Security token to send to target

	// Listener and connections.
	TCPKeepAliveTimeout = 3 * time.Minute
	TCPReadTimeout      = 10 * time.Second
	TCPWriteTimeout     = 10 * time.Second

	// Error messages.
	InvalidMediaType     = "Invalid Content-Type or Accept header value."
	InvalidMethod        = "Invalid Method for this route."
	InvalidBody          = "Invalid body of text in request."
	InvalidJSONText      = "Invalid JSON format in text of body in request."
	CouldNotProcess      = "Could not process this request."
	InvalidAuthorization = "Invalid authorization."
)

Variables

View Source
var (
	StoppedError = errors.New("Server stop requested.")
)

Functions

func Version

func Version() string

Get version of the server

Types

type Info

type Info struct {
	Version    string `json:"version"`        // Version of the server.
	Name       string `json:"name"`           // The name of the server.
	Hostname   string `json:"hostname"`       // The hostname of the server.
	UUID       string `json:"UUID"`           // Unique ID of the server.
	Port       int    `json:"port"`           // Port the server is listening on.
	ProfPort   int    `json:"profPort"`       // Profiler port the server is listening on.
	MaxConn    int    `json:"maxConnections"` // The maximum concurrent connections accepted.
	Debug      bool   `json:"debugEnabled"`   // Is debugging enabled on the server.
	Namespace  string `json:"namespace"`      // Namespace to process image.
	AlivePath  string `json:"alivePath"`      // Path to set within server for handling health checks.
	NotifyPath string `json:"notifyPath"`     // Path to set within server for handling duckerhub requests.
	StatusPath string `json:"statusPath"`     // Path to set within server for handling status requests
	TargetHost string `json:"targetHost"`     // Host redirects are sent forward.
	TargetPort int    `json:"targetPort"`     // Port of host redirects.
	TargetPath string `json:"targetPath"`     // Path of action for redirects.
}

Info provides basic information about the running server.

func InfoNew

func InfoNew(options ...func(*Info)) *Info

InfoNew is a factory function that returns a new instance of Info. options is an optional list of functions that initialize the structure

func (*Info) String

func (i *Info) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type Middleware

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

Middleware is used to perform filtering work on the request before the main controllers are called.

func (*Middleware) ServeHTTP

func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the interface to accept requests so they can be filtered before handling by the server.

type Options

type Options struct {
	Name        string   `json:"name"`           // The name of the server.
	Hostname    string   `json:"hostname"`       // The hostname of the server.
	Port        int      `json:"port"`           // The default port of the server.
	ProfPort    int      `json:"profPort"`       // The profiler port of the server.
	MaxConn     int      `json:"maxConnections"` // The maximum concurrent connections accepted.
	MaxProcs    int      `json:"maxProcs"`       // The maximum number of processor cores available.
	Debug       bool     `json:"debugEnabled"`   // Is debugging enabled in the application or server.
	ValidTokens []string `json:"-"`              // Valid tokens to access this server.
	Namespace   string   `json:"namespace"`      // Namespace to process request image into.
	AlivePath   string   `json:"alivePath"`      // Path to set within server for handling health checks.
	NotifyPath  string   `json:"notifyPath"`     // Path to set within server for handling duckerhub requests.
	StatusPath  string   `json:"statusPath"`     // Path to set within server for handling status requests
	TargetHost  string   `json:"targetHost"`     // Host redirects are sent forward.
	TargetPort  int      `json:"targetPort"`     // Port of host redirects.
	TargetPath  string   `json:"targetPath"`     // Path of action for redirects.
	TargetToken string   `json:"-"`              // Token for redirects server.
}

Options represents parameters that are passed to the application to be used in constructing the run and the server (if server mode is indicated).

func OptionsNew

func OptionsNew(options ...func(*Options)) *Options

func (*Options) String

func (o *Options) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type Server

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

Server is the main structure that represents a server instance.

func New

func New(opts *Options, addedOptions ...func(*Server)) *Server

New is a factory function that returns a new server instance.

func (*Server) LogRequest

func (s *Server) LogRequest(r *http.Request)

LogRequest logs the http request information into the logger.

func (*Server) Shutdown

func (s *Server) Shutdown() bool

Shutdown takes down the server gracefully back to an initialize state.

func (*Server) Start

func (s *Server) Start()

Start spins up the server to accept incoming connections.

func (*Server) StartProfiler

func (s *Server) StartProfiler()

StartProfiler is called to enable dynamic profiling.

type Status

type Status struct {
	Start        time.Time                   `json:"startTime"`    // The start time of the server.
	RequestCount int64                       `json:"requestCount"` // How many requests came in to the server.
	RequestBytes int64                       `json:"requestBytes"` // Size of the requests in bytes.
	ConnNumAvail int                         `json:"connNumAvail"` // Number of live connections available.
	RouteStats   map[string]map[string]int64 `json:"routeStats"`   // How many requests/bytes came into each route.
}

Status contains runtime statistics.

func StatusNew

func StatusNew(options ...func(*Status)) *Status

StatusNew is a factory function that returns a new instance of Status. options is an optional list of functions that initialize the structure

func (*Status) IncrRequestStats

func (s *Status) IncrRequestStats(rb int64)

IncrRequestStats increments the stats totals for the server.

func (*Status) IncrRouteStats

func (s *Status) IncrRouteStats(path string, rb int64)

IncrRouteStats increments the stats totals for the route.

func (*Status) String

func (s *Status) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type ThrottledConn

type ThrottledConn struct {
	*net.TCPConn
	// contains filtered or unexported fields
}

ThrottledConn is a wrapper over net.conn that allows us to throttle connections via the listener.

func (*ThrottledConn) Close

func (c *ThrottledConn) Close() error

Close overloads the type function of the connection so that the listener throttle can be serviced. TODO If file streaming is needed in the future, also add CloseRead() and CloseWrite() coverage.

func (*ThrottledConn) Done

func (c *ThrottledConn) Done()

Done puts back a token so it can be serviced again by the throttle listener.

type ThrottledListener

type ThrottledListener struct {
	*net.TCPListener
	// contains filtered or unexported fields
}

ThrottledListener is a wrapper on a listener that limits connections.

func ThrottledListenerNew

func ThrottledListenerNew(addr string, mxConn int) (*ThrottledListener, error)

ThrottledListenerNew is a factory function that returns an instatiated ThrottledListener.

func (*ThrottledListener) Accept

func (t *ThrottledListener) Accept() (net.Conn, error)

Accept overrides the accept function of the listener so that waits can occur on tokens in the queue.

func (*ThrottledListener) GetConnNumAvail

func (t *ThrottledListener) GetConnNumAvail() int

GetConnNumAvail returns the total number of connections available.

func (*ThrottledListener) Stop

func (t *ThrottledListener) Stop()

Stops the listener

Jump to

Keyboard shortcuts

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