handler

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2017 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package handler contains bootstrapping logic for the application, and all handlers for serving POST requests. The payload should have at least one key: message. And it contains the log entry for writing. If a "type" for the entry is not provided, it falls back to "info". If the "timestamp" is not provided, it uses the current time it receives the payload.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoWriter is returned when no write is provided.
	ErrNoWriter = errors.New("no writers specified")

	// ErrNilLogger is returned when logger is nil.
	ErrNilLogger = errors.New("logger cannot be nil")

	// ErrDuplicateWriter is returned on duplicated writers.
	ErrDuplicateWriter = errors.New("duplicated writer")

	// ErrWritingEntry is for when there is a problem with writing the entry.
	ErrWritingEntry = errors.New("writing the entry")

	// ErrGettingReader is returned when the reader factory cannot return an
	// appropriate reader for the entry.
	ErrGettingReader = errors.New("getting reader")

	// ErrNoOptions is returned when no option is provided.
	ErrNoOptions = errors.New("no option provided")

	// ErrTimeout is returned when the timeout is zero.
	ErrTimeout = errors.New("timeout cannot be zero")
)
View Source
var ServeFunc func(s ServiceInt, logger internal.FieldLogger, stop chan os.Signal, port int) error

ServeFunc is a function that is run for setting up the handlers.

Functions

func Bootstrap

func Bootstrap(logger internal.FieldLogger, configFile string, port int)

Bootstrap reads the command options and starts the server.

func Serve

func Serve(s ServiceInt, logger internal.FieldLogger, stop chan os.Signal, port int) error

Serve starts a http.Server in a goroutine. It listens to the Interrupt signal and shuts down the server. It returns any errors occurred during the service. In case of graceful shut down, it will nil.

func WithConfWriters

func WithConfWriters(logger internal.FieldLogger, c *config.Setting) func(*Service) error

WithConfWriters uses a config.Setting object to set up the writers. If any errors occurred during writer instantiation, it stops and returns that error.

func WithTimeout

func WithTimeout(timeout time.Duration) func(*Service) error

WithTimeout sets the timeout on Service. It returns an error if the timeout is zero.

func WithWriters

func WithWriters(ws ...io.Writer) func(*Service) error

WithWriters will return an error if two identical writers are injected.

Types

type Service

type Service struct {
	// Writers is a slice of all writers.
	Writers []io.Writer

	// Logger is used for logging service's behaviours.
	Logger internal.FieldLogger
	// contains filtered or unexported fields
}

Service listens to the incoming http requests and decides how to route the payload to be written.

func New

func New(logger internal.FieldLogger, opts ...func(*Service) error) (*Service, error)

New returns an error if there is no logger or no writer specified.

func (*Service) Handler

func (l *Service) Handler() http.HandlerFunc

func (*Service) RecieveHandler

func (l *Service) RecieveHandler(w http.ResponseWriter, r *http.Request)

RecieveHandler handles the logs coming from the endpoint. It handles the writes in a goroutine in order to avoid write loss. It will log any errors that might occur during writes. It returns a http.StatusBadRequest if the payload is not a valid JSON object or does not contain the required fields.

func (*Service) Timeout

func (l *Service) Timeout() time.Duration

Timeout returns the timeout associated with this service.

type ServiceInt

type ServiceInt interface {
	Handler() http.HandlerFunc
	Timeout() time.Duration
}

ServiceInt is an interface for the handler.Service

Jump to

Keyboard shortcuts

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