httping

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2020 License: MIT Imports: 6 Imported by: 1

README

Httping go

Release Coverage License GoDoc

A library for creating API's on golang that comes with a JSend responses helper

Checkout some example.

Getting started

Download

go get github.com/ednailson/httping-go
Creating a server
server := httping.NewHttpServer("", 3000)

Here a http server was created on the port 3000 and from the server is possible to create http routes.

The server has support to CORS. To enable it, you just need to do

server.EnableCORS()
Creating a route
routeExample := server.NewRoute(nil, "/example")

Now there a route. It is possible to add http methods to this route with a treatment function.

Creating a route from a route

It is possible to create a route from a route. This allows to create routes from a unique route.

routeCreateExample := server.NewRoute(routeExample, "/create")
routeUpdateExample := server.NewRoute(routeExample, "/update")

So now there are two new routes: http://localhost:3000/example/create and http://localhost:3000/example/update.

Adding a method on the route
routeExample.AddMethod("POST", func(request httping.HttpRequest) httping.IResponse {
    if len(request.body) == 0 {
        return httping.NewResponse(404)
    }
    return httping.NewResponse(200)
})

A method POST is now available on the route http://localhost:3000/example.

p.s.: only http methods and http codes are allowed

And it is possible to add different methods on the same route.

routeExample.AddMethod("GET", func(request httping.HttpRequest) httping.IResponse {
    if len(request.body) == 0 {
        return httping.NewResponse(404)
    }
    return httping.NewResponse(200)
})

Now the route http://localhost:3000/example has the methods GET and POST.

If you will not use the route two or more times you can directly create a route and add a method

server.NewRoute(nil, "/create").AddMethod("POST", func(request httping.HttpRequest) httping.IResponse {
		return httping.NewResponse(200)
	})
Responding

Responses for the handleFunc()

For creating a ResponseMessage

response := httping.NewResponse(200)

This will build a Response message with the status correct according with the http status code and jsend pattern.

Example

server.NewRoute(nil, "/create").POST(func(request httping.HttpRequest) httping.IResponse {
		return httping.NewResponse(200).AddData("success")
	})

It respects the jsend's pattern.

On responses it also possible to add Headers, Code and Message.

There are a few helpers for the most commons http status codes.

Example

server.NewRoute(nil, "/create").POST(func(request httping.HttpRequest) httping.IResponse {
		return httping.OK("data example")
	})

It will return a status code ok (200) with the data data example You can check all the helpers here.

Middleware on the server or route

It is possible to add a middleware handler function to the server or a route

server := httping.NewHttpServer(3000).AddMiddleware(
    func(request httping.HttpRequest) httping.IResponse {
        if request.Headers["Authorization"][0] != "token"{
            return httping.Unauthorized("not authorized")
        }
        return nil
    }
)

If you return IResponse: The server will not let the request proceed and it will return the response returned.

If you return nil, the server will let the request proceed to the route's handleFunc

Middleware can also be applied only on a route. If the server has a middleware, the function will be added by the route's middleware function.

It is possible to set middleware. It will replaced all the middleware functions on that route or server.

Developer

Júnior Vilas Boas

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFunc

type HandlerFunc func(request HttpRequest) (response IResponse)

type HttpRequest

type HttpRequest struct {
	Body    []byte
	Query   url.Values
	Params  map[string]string
	Headers map[string][]string
	Cookies []*http.Cookie
}

type IResponse added in v0.13.0

type IResponse interface {
	Headers() map[string][]string
	Cookies() []*http.Cookie
	Response() interface{}
	StatusCode() int
}

type IRoute

type IRoute interface {
	AddMethod(method string, handler HandlerFunc)
	POST(handler HandlerFunc)
	GET(handler HandlerFunc)
	DELETE(handler HandlerFunc)
	PUT(handler HandlerFunc)
	PATCH(handler HandlerFunc)
	HEAD(handler HandlerFunc)
	OPTIONS(handler HandlerFunc)
	SetMiddleware(middleware []HandlerFunc) IRoute
	AddMiddleware(middleware HandlerFunc) IRoute
	// contains filtered or unexported methods
}

type IServer

type IServer interface {
	NewRoute(group IRoute, path string) IRoute
	RunServer() (ServerCloseFunc, chan error)
	SetMiddleware(middleware []HandlerFunc) IServer
	AddMiddleware(middleware HandlerFunc) IServer
	EnableCORS() IServer
}

func NewHttpServer

func NewHttpServer(host string, port int) IServer

Creates a HTTP Server

type Response added in v0.15.0

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

func Accepted added in v0.5.0

func Accepted(data interface{}) *Response

func AlreadyReported added in v0.14.1

func AlreadyReported(data interface{}) *Response

func BadRequest added in v0.5.0

func BadRequest(data interface{}) *Response

func Conflict added in v0.14.1

func Conflict(data interface{}) *Response

func Created added in v0.5.0

func Created(data interface{}) *Response

func ExpectationFailed added in v0.14.1

func ExpectationFailed(data interface{}) *Response

func FailedDependency added in v0.14.1

func FailedDependency(data interface{}) *Response

func Forbidden added in v0.5.0

func Forbidden(data interface{}) *Response

func Gone added in v0.14.1

func Gone(data interface{}) *Response

func IMUsed added in v0.14.1

func IMUsed(data interface{}) *Response

func InternalServerError added in v0.5.0

func InternalServerError(data interface{}) *Response

func LengthRequired added in v0.14.1

func LengthRequired(data interface{}) *Response

func Locked added in v0.14.1

func Locked(data interface{}) *Response

func MethodNotAllowed added in v0.5.0

func MethodNotAllowed(data interface{}) *Response

func MisdirectedRequest added in v0.14.1

func MisdirectedRequest(data interface{}) *Response

func MultiStatus added in v0.14.1

func MultiStatus(data interface{}) *Response

func NewResponse added in v0.3.0

func NewResponse(statusCode int) *Response

func NoContent added in v0.5.0

func NoContent() *Response

func NonAuthoritativeInfo added in v0.14.1

func NonAuthoritativeInfo(data interface{}) *Response

func NotAcceptable added in v0.5.0

func NotAcceptable(data interface{}) *Response

func NotFound added in v0.5.0

func NotFound(data interface{}) *Response

func OK added in v0.5.0

func OK(data interface{}) *Response

func PartialContent added in v0.14.1

func PartialContent(data interface{}) *Response

func PreconditionFailed added in v0.14.1

func PreconditionFailed(data interface{}) *Response

func PreconditionRequired added in v0.14.1

func PreconditionRequired(data interface{}) *Response

func ProxyAuthRequired added in v0.14.1

func ProxyAuthRequired(data interface{}) *Response

func RequestEntityTooLarge added in v0.14.1

func RequestEntityTooLarge(data interface{}) *Response

func RequestHeaderFieldsTooLarge added in v0.14.1

func RequestHeaderFieldsTooLarge(data interface{}) *Response

func RequestTimeout added in v0.14.1

func RequestTimeout(data interface{}) *Response

func RequestURITooLong added in v0.14.1

func RequestURITooLong(data interface{}) *Response

func RequestedRangeNotSatisfiable added in v0.14.1

func RequestedRangeNotSatisfiable(data interface{}) *Response

func ResetContent added in v0.14.1

func ResetContent(data interface{}) *Response

func Teapot added in v0.14.1

func Teapot(data interface{}) *Response

func TooEarly added in v0.14.1

func TooEarly(data interface{}) *Response

func TooManyRequests added in v0.14.1

func TooManyRequests(data interface{}) *Response

func Unauthorized added in v0.5.0

func Unauthorized(data interface{}) *Response

func UnavailableForLegalReasons added in v0.14.1

func UnavailableForLegalReasons(data interface{}) *Response

func UnprocessableEntity added in v0.14.1

func UnprocessableEntity(data interface{}) *Response

func UnsupportedMediaType added in v0.14.1

func UnsupportedMediaType(data interface{}) *Response

func UpgradeRequired added in v0.14.1

func UpgradeRequired(data interface{}) *Response

func (*Response) AddCookie added in v0.15.0

func (r *Response) AddCookie(cookie *http.Cookie) *Response

func (*Response) AddHeader added in v0.15.0

func (r *Response) AddHeader(key, value string) *Response

func (*Response) Cookies added in v0.15.0

func (r *Response) Cookies() []*http.Cookie

func (*Response) Headers added in v0.15.0

func (r *Response) Headers() map[string][]string

func (*Response) Response added in v0.15.0

func (r *Response) Response() interface{}

func (*Response) SetCookies added in v0.15.0

func (r *Response) SetCookies(cookies []*http.Cookie) *Response

func (*Response) SetData added in v0.15.0

func (r *Response) SetData(data interface{}) *Response

func (*Response) StatusCode added in v0.15.0

func (r *Response) StatusCode() int

type ServerCloseFunc

type ServerCloseFunc func() error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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