middleware

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2016 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFavicon added in v0.8.0

func NewFavicon(iconpath string) gear.Middleware

NewFavicon returns a middleware to serve favicon from the provided directory.

package main

import (
	"github.com/teambition/gear"
	"github.com/teambition/gear/middleware"
)

func main() {
	app := gear.New()
	app.Use(gear.NewDefaultLogger())
	app.Use(middleware.NewFavicon("./testdata/favicon.ico"))
	app.Use(func(ctx *gear.Context) error {
		return ctx.HTML(200, "<h1>Hello, Gear!</h1>")
	})
	app.Error(app.Listen(":3000"))
}

func NewStatic

func NewStatic(opts StaticOptions) gear.Middleware

NewStatic returns a Static middleware to serves static content from the provided root directory.

package main

import (
	"github.com/teambition/gear"
	"github.com/teambition/gear/middleware"
)

func main() {
	app := gear.New()
	app.Use(gear.NewDefaultLogger())
	app.Use(middleware.NewFavicon("./testdata/favicon.ico"))
	app.Use(middleware.NewStatic(middleware.StaticOptions{
		Root:        "./testdata",
		Prefix:      "/",
		StripPrefix: false,
	}))
	app.Use(func(ctx *gear.Context) error {
		return ctx.HTML(200, "<h1>Hello, Gear!</h1>")
	})
	app.Error(app.Listen(":3000"))
}

func NewTimeout added in v0.7.0

func NewTimeout(du time.Duration, h gear.Hook) gear.Middleware

NewTimeout returns a timeout middleware with time.Duration and timeout hook. A timeout middleware example:

app := gear.New()
app.Use(NewTimeout(time.Second, func(ctx *gear.Context) {
	// timeout hook
	ctx.Status(504)
	ctx.String("Service timeout")
}))
app.Use(func(ctx *gear.Context) error {
	// some process maybe timeout...
	c, _ := ctx.WithTimeout(time.Second * 2)
	select {
	case <-ctx.Done(): // this case will always reached
	case <-c.Done(): // this case maybe reached... but elapsed time should be 1 sec.
	}
	return nil
})
app.Use(func(ctx *gear.Context) error {
	// if timeout, the rest of middleware will not run.
	panic("this middleware unreachable")
})

Types

type StaticOptions

type StaticOptions struct {
	Root        string // The directory you wish to serve
	Prefix      string // The url prefix you wish to serve as static request, default to `'/'`.
	StripPrefix bool   // Strip the prefix from URL path, default to `false`.
}

StaticOptions is static middleware options

Jump to

Keyboard shortcuts

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