engine

package module
v0.0.0-...-35bea84 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2017 License: MIT Imports: 18 Imported by: 1

README

engine

Based off gin, engine is a collection of helpers to kick start for web projects in go. Not sure how fast it is - just works for me.

Installation

  1. Get it.
$ go get github.com/mnbbrown/engine
  1. Import and use.
package main

import (
    "fmt"
    "github.com/mnbbrown/engine"
    "net/http"
)

func main() {
    e := engine.NewRouter()
    e.Get("/", func(rw http.ResponseWriter, req *http.Request) {
        fmt.Fprint(rw, "Hello World!")
    })
    http.ListenAndServe(":8080", e)
}

Licence

The MIT License (MIT)

Copyright (c) 2015- Matthew Brown me@matthewbrown.io

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllowAllConfig = &CORSConfig{
	AllowedMethods: []string{"*"},
	AllowedOrigins: []string{"*"},
	AllowedHeaders: []string{"Authorization", "X-Requested-With"},
}
View Source
var (
	ResetColour = string([]byte{27, 91, 48, 109})
)

Functions

func CORSAcceptAll

func CORSAcceptAll(next http.Handler) http.Handler

func ColourForStatus

func ColourForStatus(code int) string

func HumanSize

func HumanSize(size int) string

HumanSize returns a human representation of a bytes size as a string

func HumanSizeSuffix

func HumanSizeSuffix(size int) string

func JSON

func JSON(rw http.ResponseWriter, v interface{}, code int)

func JSONError

func JSONError(rw http.ResponseWriter, err error, code int)

func MetadataMiddleware

func MetadataMiddleware(next http.Handler) http.Handler

M is middleware that adds metadata to each requests and logs

Types

type CORSConfig

type CORSConfig struct {
	AllowedMethods []string
	AllowedOrigins []string
	AllowedHeaders []string
}

type Context

type Context struct {
	context.Context
	io.ReadCloser

	Params httprouter.Params
	// contains filtered or unexported fields
}

func GetContext

func GetContext(req *http.Request) *Context

func (*Context) Set

func (c *Context) Set(key interface{}, value interface{})

func (*Context) Value

func (c *Context) Value(key interface{}) interface{}

func (*Context) Wrap

func (c *Context) Wrap(ctx context.Context)

type J

type J map[string]interface{}

func ParseJSON

func ParseJSON(req *http.Request) (J, error)

func (J) GetString

func (j J) GetString(key string) string

type MiddlewareFunc

type MiddlewareFunc func(http.Handler) http.Handler

func CORSMiddleware

func CORSMiddleware(config *CORSConfig) MiddlewareFunc

type RequestMetadata

type RequestMetadata struct {
	RequestID string
	Method    string
	Path      string
	Status    int
	IP        string
	Size      int
	Latency   time.Duration
	StartTime time.Time
}

RequestMetadata generates metadata info for each request

func GetMetadata

func GetMetadata(ctx *Context) (*RequestMetadata, bool)

FromContext extracts the metadata from the request

func (*RequestMetadata) Logger

func (r *RequestMetadata) Logger() *log.Entry

Logger returns a logger that logs with the request fields

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) Header

func (s *ResponseWriter) Header() http.Header

func (*ResponseWriter) Hijack

func (w *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*ResponseWriter) Length

func (s *ResponseWriter) Length() int

func (*ResponseWriter) Status

func (s *ResponseWriter) Status() int

func (*ResponseWriter) Write

func (s *ResponseWriter) Write(data []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (s *ResponseWriter) WriteHeader(statusCode int)

type Router

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

func NewRouter

func NewRouter() *Router

func (*Router) Delete

func (r *Router) Delete(path string, handler http.HandlerFunc, middleware ...MiddlewareFunc)

Delete registers a DELETE handler for the given path.

func (*Router) Get

func (r *Router) Get(path string, handler http.HandlerFunc, middleware ...MiddlewareFunc)

Get registers a GET handler for the given path.

func (*Router) Handle

func (r *Router) Handle(method, path string, handler http.Handler, middleware ...MiddlewareFunc)

func (*Router) HandleFunc

func (r *Router) HandleFunc(method, path string, handler func(http.ResponseWriter, *http.Request), middleware ...MiddlewareFunc)

func (*Router) Head

func (r *Router) Head(path string, handler http.HandlerFunc, middleware ...MiddlewareFunc)

func (*Router) ListMiddleware

func (r *Router) ListMiddleware() (mi []string)

func (*Router) Options

func (r *Router) Options(path string, handler http.HandlerFunc, middleware ...MiddlewareFunc)

Options registers a OPTIONS handler for the given path.

func (*Router) Patch

func (r *Router) Patch(path string, handler http.HandlerFunc, middleware ...MiddlewareFunc)

Patch registers a PATCH handler for the given path.

func (*Router) Post

func (r *Router) Post(path string, handler http.HandlerFunc, middleware ...MiddlewareFunc)

Post registers a POST handler for the given path.

func (*Router) Put

func (r *Router) Put(path string, handler http.HandlerFunc, middleware ...MiddlewareFunc)

Put registers a PUT handler for the given path.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*Router) SetMethodNotAllowed

func (r *Router) SetMethodNotAllowed(h http.Handler)

func (*Router) SetNotFound

func (r *Router) SetNotFound(h http.Handler)

func (*Router) Static

func (r *Router) Static(relativePath, root string)

func (*Router) SubRouter

func (r *Router) SubRouter(relativePath string, middleware ...MiddlewareFunc) *Router

func (*Router) Use

func (r *Router) Use(middleware ...MiddlewareFunc)

func (*Router) UseHandler

func (r *Router) UseHandler(handler http.Handler)

Jump to

Keyboard shortcuts

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