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
- func RunFileServer(targetDir string, port int, log logrus.FieldLogger) error
- type AroundHandler
- type Context
- type ContextHandler
- type GenerateRequester
- type GenerateRouter
- func (router *GenerateRouter) Around(handler AroundHandler)
- func (router *GenerateRouter) Get(url string, handler ContextHandler)
- func (router *GenerateRouter) GetHTML(url string, handler ContextHandler)
- func (router *GenerateRouter) GetRootHTML(handler ContextHandler)
- func (router *GenerateRouter) Requester() Requester
- func (router *GenerateRouter) URLs() []string
- type Requester
- type Response
- type Router
- type WebRequester
- type WebRouter
- func (router *WebRouter) Around(handler AroundHandler)
- func (router *WebRouter) FileServe(pattern, dirPath string)
- func (router *WebRouter) Get(url string, handler ContextHandler)
- func (router *WebRouter) GetHTML(url string, handler ContextHandler)
- func (router *WebRouter) GetRootHTML(handler ContextHandler)
- func (router *WebRouter) Requester() Requester
- func (router *WebRouter) Run() error
- func (router *WebRouter) URLs() []string
Constants ¶
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 ¶
ContextHandler is the handler for Router routes
type GenerateRequester ¶
type GenerateRequester struct {
// contains filtered or unexported fields
}
GenerateRequester makes requests on the GenerateRouter
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 ¶
Response given by all routers
func NewResponse ¶
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
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 ¶
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 ¶
Requester returns a requester for the given router, to make requests and return the response