gomiddleware

package module
v0.0.0-...-90b602a Latest Latest
Warning

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

Go to latest
Published: May 16, 2022 License: MIT Imports: 12 Imported by: 0

README

gomiddleware

Negroni Middleware for golang

  • Logger - log writer
  • Static - file server

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogDefaultDateFormat = time.RFC3339

LogDefaultDateFormat is the format used for date by the default Log instance.

View Source
var LogDefaultFormat = "{{.StartTime}} | {{.Status}} | \t {{.Duration}} | {{.Hostname}} | {{.Method}} {{.Path}} \n"

LogDefaultFormat is the format logged used by the default Log instance.

Functions

This section is empty.

Types

type ALogger

type ALogger interface {
	Println(v ...interface{})
	Printf(format string, v ...interface{})
}

ALogger interface

type LogEntry

type LogEntry struct {
	StartTime string
	Status    int
	Duration  time.Duration
	Hostname  string
	Method    string
	Path      string
}

LogEntry is the structure passed to the template.

type Logger

type Logger struct {
	// LoggerInterface implements more log.Logger interface to be compatible with other implementations
	ALogger
	// contains filtered or unexported fields
}

Log is a middleware handler that logs the request as it goes in and the response as it goes out.

func NewLogger

func NewLogger() *Logger

NewLogger returns a new Logger instance

func NewLoggerWithStream

func NewLoggerWithStream(w io.Writer) *Logger

NewLogger with io.writer returns a new Logger instance

func (*Logger) ServeHTTP

func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

func (*Logger) SetDateFormat

func (l *Logger) SetDateFormat(format string)

func (*Logger) SetFormat

func (l *Logger) SetFormat(format string)

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// Status returns the status code of the response or 200 if the response has
	// not been written (as this is the default response code in net/http)
	Status() int
	// Written returns whether or not the ResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
	// Before allows for a function to be called before the ResponseWriter has been written to. This is
	// useful for setting headers or any other operations that must happen before a response has been written.
	Before(func(ResponseWriter))
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) ResponseWriter

NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter

type Static

type Static struct {
	// Dir is the directory to serve static files from
	Dir http.FileSystem
	// Prefix is the optional prefix used to serve the static directory content
	Prefix string
	// IndexFile defines which file to serve as index if it exists.
	IndexFile string
}

Static is a middleware handler that serves static files in the given directory/filesystem. If the file does not exist on the filesystem, it passes along to the next middleware in the chain. If you desire "fileserver" type behavior where it returns a 404 for unfound files, you should consider using http.FileServer from the Go stdlib.

func NewStatic

func NewStatic(directory http.FileSystem) *Static

NewStatic returns a new instance of Static

func (*Static) ServeHTTP

func (s *Static) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Jump to

Keyboard shortcuts

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