Socket
Book a DemoInstallSign in
Socket

github.com/husobee/backdrop

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/husobee/backdrop

v0.0.0-20151224171753-a62f904332a5
Source
Go
Version published
Created
Source

Backdrop - Go Web Framework Agnostic Context Management

Abstract

Context is critical for web applications. Many frameworks solve the context issue by locking you into their framework. This project attempts to provide a clean interface to use net.context within your framework agnostic application.

Design

  • Global Context Repository protected by goroutine synchronization
  • Contexts are per request and cancelable
  • Underlying contexts are net.context.Context

Examples


package main

import (
	"fmt"
	"net/http"
	"time"

	"golang.org/x/net/context"

	"github.com/codegangsta/negroni"
	"github.com/husobee/backdrop"
	"github.com/husobee/vestigo"
	"github.com/satori/go.uuid"
	"github.com/tylerb/graceful"
)

func main() {
	// using negroni for this example to show middlewares can access context
	n := negroni.Classic()
	// start backdrop - here you can provide a backdrop.Options with a base
	// context to inherit from if you choose.
	backdrop.Start(nil)

	// set up awesome router ;)
	router := vestigo.NewRouter()
	router.Get("/:name", f)

	n.Use(&ridMiddleware{})
	// add router to middleware
	n.UseHandler(router)

	// graceful start/stop server
	srv := &graceful.Server{
		Timeout: 5 * time.Second,
		Server: &http.Server{
			Addr: ":1234",
			// top level handler needs to clear the context
			// per each request, use this wrapper handler
			Handler: backdrop.NewClearContextHandler(n),
		},
	}
	srv.ListenAndServe()
}

func f(w http.ResponseWriter, r *http.Request) {
	// get the id from the context
	id, err := backdrop.Get(r, "id")
	if err != nil {
		fmt.Println("err: ", err.Error())
	}
	fmt.Printf("request id is: %v\n", id)
	// you can also get the entire context if you are more comfortable with that
	ctx := backdrop.GetContext(r)
	ctx = context.WithValue(ctx, "key", "value")
	// and setting the newly created context in backdrop
	backdrop.SetContext(r, ctx)
}

type ridMiddleware struct{}

func (rid *ridMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	// example of setting a value to the context
	backdrop.Set(r, "id", uuid.NewV4())
	next(w, r)
}


Licensing

  • The MIT License covered under this License.

Contributing

If you wish to contribute, please fork this repository, submit an issue, or pull request with your suggestions.
Please use gofmt and golint before trying to contribute.

FAQs

Package last updated on 24 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.