router

package
v1.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package router is a router for static websites. Provides a GenerateRouter to generate files and a WebRouter, a simplified web router, which have the same interface.

Index

Constants

View Source
const RootURL = "/"

RootURL is the URL of the Root of the router

Variables

This section is empty.

Functions

func RunFileServer

func RunFileServer(targetDir string, port int, log logrus.FieldLogger) error

RunFileServer hosts the files of targetDir into given port with the log

Types

type AroundHandler

type AroundHandler func(ctx Context, handler ContextHandler) error

AroundHandler is the handler for Router callbacks

type Context

type Context interface {
	// Log returns the log of the context
	Log() logrus.FieldLogger
	// SetLog sets the log of the Context, so that you can set the context of the log
	SetLog(log logrus.FieldLogger)

	// ContentType returns the Content-Type to be sent to the response
	ContentType() string
	// SetContentType sets the Content-Type of the response
	SetContentType(contentType string)

	// URL returns the URL of the request
	URL() string

	// Respond sets the response data of the request
	Respond(bytes []byte)
}

Context is an interface of the context provided for every route, it's an interface to make testing easier

type ContextHandler

type ContextHandler func(ctx Context) error

ContextHandler is the handler for Router routes

type GenerateRequester

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

GenerateRequester makes requests on the GenerateRouter

func (*GenerateRequester) Get

func (requester *GenerateRequester) Get(url string) (*Response, error)

Get gets the response of the route's handler given the url

type GenerateRouter

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

GenerateRouter generates static files, note that ContentType is respected by the router's Response struct, by default via calling mime.TypeByExtension (Go std lib) on the route pattern or setting it via Context. However, generated files DO NOT have a ContentType, as ContentType is a http thing and will be set when files are uploaded to S3.

See the Router interface.

func NewGenerateRouter

func NewGenerateRouter(log logrus.FieldLogger) *GenerateRouter

NewGenerateRouter returns a new instance of GenerateRouter

func (*GenerateRouter) Around

func (router *GenerateRouter) Around(handler AroundHandler)

Around is a callback/handler that is called around all routes

func (*GenerateRouter) Get

func (router *GenerateRouter) Get(url string, handler ContextHandler)

Get define a handler for any file type given a URL

func (*GenerateRouter) GetHTML

func (router *GenerateRouter) GetHTML(url string, handler ContextHandler)

GetHTML defines a HTML handler given a URL (shorthand for Get with Content-Type set for .html files)

func (*GenerateRouter) GetRootHTML

func (router *GenerateRouter) GetRootHTML(handler ContextHandler)

GetRootHTML defines a HTML handler for the root URL `/`

func (*GenerateRouter) Requester

func (router *GenerateRouter) Requester() Requester

Requester returns a requester for the given router, to make requests and return the response

func (*GenerateRouter) URLs

func (router *GenerateRouter) URLs() []string

URLs returns a list the URLs defined on the router

type Requester

type Requester interface {
	// Calls the route and returns the response given the url
	Get(url string) (*Response, error)
}

Requester is an abstraction for making router requests

type Response

type Response struct {
	Body     []byte
	MimeType string
}

Response given by all routers

func NewResponse

func NewResponse(body []byte, mimeType string) *Response

NewResponse returns a new instance of Response

type Router

type Router interface {
	// Around is a callback/handler that is called around all routes
	Around(handler AroundHandler)

	// GetRootHTML defines a HTML handler for the root URL `/`
	GetRootHTML(handler ContextHandler)
	// GetHTML defines a HTML handler given a url (shorthand for Get with Content-Type set for .html files)
	GetHTML(url string, handler ContextHandler)
	// Get define a handler for any file type given a url
	Get(url string, handler ContextHandler)

	// URLs returns a list the URLs defined on the router
	URLs() []string
	// Requester returns a requester for the given router, to make requests and return the response
	Requester() Requester
}

Router is the interface for all routers.

type WebRequester

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

WebRequester makes requests on the WebRouter

func (*WebRequester) Get

func (requester *WebRequester) Get(url string) (resp *Response, err error)

Get gets the response of the route's handler given the url

type WebRouter

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

WebRouter is the router to host a web application server. It's simplified such that all errors give http.StatusBadRequest and print out the errors. It's also can't handle two routes like this: `/folder` returning HTML and `/folder/something.png` because gostatic is made to generate static websites so `/folder` would be a folder and can't return HTML.

Content-Type is respected by default via calling mime.TypeByExtension (Go std lib) on the route pattern or setting it via Context.

See the Router interface.

func NewWebRouter

func NewWebRouter(port int, log logrus.FieldLogger) *WebRouter

NewWebRouter returns a new instance of WebRouter

func (*WebRouter) Around

func (router *WebRouter) Around(handler AroundHandler)

Around is a callback/handler that is called around all routes

func (*WebRouter) FileServe

func (router *WebRouter) FileServe(pattern, dirPath string)

FileServe sets the router to redirect requests with a pattern to a file directory. Content-Type is respected via calling mime.TypeByExtension (Go std lib).

func (*WebRouter) Get

func (router *WebRouter) Get(url string, handler ContextHandler)

Get define a handler for any file type given a URL

func (*WebRouter) GetHTML

func (router *WebRouter) GetHTML(url string, handler ContextHandler)

GetHTML defines a HTML handler given a URL (shorthand for Get with Content-Type set for .html files)

func (*WebRouter) GetRootHTML

func (router *WebRouter) GetRootHTML(handler ContextHandler)

GetRootHTML defines a HTML handler for the root URL `/`

func (*WebRouter) Requester

func (router *WebRouter) Requester() Requester

Requester returns a requester for the given router, to make requests and return the response

func (*WebRouter) Run

func (router *WebRouter) Run() error

Run starts the web server for this router

func (*WebRouter) URLs

func (router *WebRouter) URLs() []string

URLs returns a list the URLs defined on the router

Jump to

Keyboard shortcuts

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