web

package module
v0.0.0-...-08af748 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2017 License: MIT Imports: 6 Imported by: 0

README

web

Minimal web toolkit build upon the Go 1.7 immutable context.

Build Status GoDoc

Example
app := web.New()
app.Use(assert.Middleware())
app.Use(web.Mount("/assets", serve.Dir("public")))
app.Use(logger.Middleware())
app.Use(timeout.Timeout("15s"))
app.Use(sessions.Middleware(
    cookieName,
    sessions.NewCookieStore([]byte("secret")),
))
app.Use(routes.Public())
if err := app.Run("0.0.0.0:3000"); err != nil {
    log.Fatal(err)
}
Request-scoped data

The following shows some examples where request-scoped data is used.

Router

The following example shows how the context can be utilized for middlewares to add own data to the request context. In this example, a router middleware adds the path parameters to the context.

r := router.New()
// Use http.HandlerFunc footprint, but ...
r.GET("/user/:id", func(rw http.ResponseWriter, r *http.Request) {
  // ... still be able to retrieve router specific values like params
  id := router.Param(r, "id")
  // id := router.ParamFromContext(r.Context(), "id")
})
Authentication / Current User

The following example authenticates the currently logged in user. On successfull authentication the user is added to the context. This allows accessing the current user on succeeding middlewares and routes.

type User struct {}

type key int
var userKey key = 1

func (user *User) NewContext(ctx context.Context) context.Context {
  return context.WithValue(ctx, userKey, user)
}

func UserFromContext(ctx context.Context) *User {
  user, ok := ctx.Value(userKey).(*User)
  if !ok {
    return nil
  }
  return user
}

// middleware to ensure authentication
app.Use(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
  // ...
  if user.IsAuthenticated() {
    // continue in the middleware stack with the user being added to the context
    next(rw, r.WithContext(user.NewContext(r.Context())))
  } else {
    rw.WriteHeader(http.StatusUnauthorized)
  }
})

// route that uses the previuously added user
r.POST("/articles", func(rw http.ResponseWriter, r *http.Request) {
  user := UserFromContext(r.Context())
  // ...
})
Works well with

License

MIT

Documentation

Overview

Package web is a minimal web middleware toolkit build upon build upon the Go 1.7 immutable context(https://golang.org/pkg/context).

app := web.New()
app.Use(assert.Middleware())
app.Use(web.Mount("/assets", serve.Dir("public")))
app.Use(logger.Middleware())
app.Use(timeout.Timeout("15s"))
app.Use(sessions.Middleware(
    cookieName,
    sessions.NewCookieStore([]byte("secret")),
))
app.Use(routes.Public())
if err := app.Run("0.0.0.0:3000"); err != nil {
  log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App interface {
	// Add a middleware to the middleware stack.
	Use(Middleware)

	// UseHandler can be used to use a http.Handler as a middleware.
	UseHandler(http.Handler)

	// UseFunc can be used to use a http.HandlerFunc as a middleware.
	UseFunc(http.HandlerFunc)

	// Execute the middleware stack using the provided context.
	Execute(http.ResponseWriter, *http.Request, http.HandlerFunc)

	// Serve the middleware stack through HTTP.
	ServeHTTP(http.ResponseWriter, *http.Request)

	// Run a http server on the given address.
	Run(addr string) error

	// Run a https server on the given address using the provided certificate.
	RunTLS(addr, certFile, keyFile string) error
}

The App is used to register middlewares and serve them through HTTP.

func New

func New() App

New creates a new App.

type Middleware

type Middleware func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

A middleware.

func Combine

func Combine(mw ...Middleware) Middleware

Combine can be used to combine multiple middlewares into one stack.

func Func

func Func(fn http.HandlerFunc) Middleware

UseHandler wraps a http.HandlerFunc into a Middleware.

func Handler

func Handler(handler http.Handler) Middleware

UseHandler wraps a http.Handler into a Middleware.

func Mount

func Mount(path string, mw Middleware) Middleware

Mount can be used to mount a middleware to the specified path / path-prefix. If the the path matches, the matched part ist trimmed and the middleware is called. Otherwise, the middleware is skipped.

Jump to

Keyboard shortcuts

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