Giter Site home page Giter Site logo

xhttp's Introduction

xhttp

GoDoc

Description

This package defines a wrapper around the *http.ServeMux that can be found in Go's Standard library. The goal is to provide convenience methods to register request handlers and facilitate the usage of an execution context per goroutine spawned (which should be the norm).

What are the main additions compared to net/http?

The ServeMux wrapper allows to define a deadline for the completion of subtasks that would be launched on behalf of the request handling goroutine.

Convenience methods for request handler registration are also provided.

Lastly, two new interfaces are defined:

  • xhttp.Handler
  • xhttp.HandlerLinker

An xhttp.Handler differs from the traditional http.Handler by the signature of its ServeHTTP method: it demands to be provided with one more argument which is an execution.Context.

An xhttp.HandlerLinker enables the linking of two Handler objects so that one can call the other. By recursion, it allows to create a linked list (chain) of handlers.

 // Handler ServeHTTP signature from the standard library.
ServeHTTP(w http.ResponseWriter, r *http.Request)

// Handler ServeHTTP signature from the xhttp package.
ServeHTTP(ctx execution.Context, w http.ResponseWriter, r *http.Request)

// HandlerLinker ServeHTTP and Link signatures from the xhttp package.
ServeHTTP(ctx execution.Context, w http.ResponseWriter, r *http.Request)
Link(h Handler) HandlerLinker

Convenience methods

As a wrapper around *http.ServeMux, we have defined several methods that should render the task of registering request handlers per route and verb easier.

Registration consists in defining a handler and using it as argument.

s := xhttp.NewServeMux()

s.GET("/foo",someHandler)
s.PUT("/bar/", someOtherHandler)

where someHandler and someOtherHandler implement the Handler interface.

To register handlers that apply regardless of the request verb, the USE variadic method, which accepts linkable handlers as arguments, exists :

s.USE(handlerlinkerA, handlerlinkerB, handlerlinkerC)
// Calling it again will queue another handler to the previous list
// For instance here, handlerlinkerD will be called right after
// handlerlinkerC
s.USE(handlerlinkerD)

More about chaining/linking Handler objects

If a given route & request.Method requires a response to be processed by a chain of handlers, the xhttp.Chain function can be used to create such a chain.

postHandler := xhttp.Chain(hlinkerA, hlinkerB, hlinkerC).Link(Handler)
s.POST("/foobar", postHandler)

Basic Handlers

The /handler/ subfolder contains some general use request handlers for response compression, CSRF protection, session management,...

License

BSD 3-clause

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.