gohttprouter

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

goHTTPRouter

GoDoc pipeline status coverage report codecov Go Report Card

goHTTPRouter is a framework used for HTTP request routing.

Examples

Simple Routing

Just replace the standard router of golang with this one:

import httprouter "gitlab.com/martinr92/gohttprouter/v2"
router := httprouter.New()
router.HandleFunc(http.MethodGet, "/home", func(response http.ResponseWriter, request *http.Request, info httprouter.RoutingInfo) {
    response.Write([]byte("Home"))
})
err := http.ListenAndServe("localhost:8080", router)

Routing with placeholder

A path can also contain placeholder (like :id). If then a request is sent to the url "/user/123" the method gets executed and the URL part (in this case "123") is passed as parameter into your handler function.

import httprouter "gitlab.com/martinr92/gohttprouter/v2"
router := httprouter.New()
router.HandleFunc(http.MethodGet, "/user/:id", handleUserPages)
router.HandleFunc(http.MethodGet, "/user/:id/settings", handleUserPages)
func handleUserPages(response http.ResponseWriter, request *http.Request, info httprouter.RoutingInfo) {
    id := info.Parameters["id"]
    response.Write([]byte(id))
}
err := http.ListenAndServe("localhost:8080", router)

Serve Static Content

Static files (like JavaScript or CSS) can be served automatically (including caching header and MIME type). It uses the embed.FS (since go 1.16) to serve static content.

import httprouter "gitlab.com/martinr92/gohttprouter/v2"

//go:embed files/statc/*
var staticFiles embed.FS
staticFS := httprouter.NewFS(&staticFiles)
router := httprouter.New()
router.Handle(http.MethodGet, "/static/*", staticFS)

For development purpose you can enable the local file serving additionally. The framework checks first, if the file exists locally and serves it directly. If not, the file is served from the embed.FS. This helps you during local development so you can modify a CSS file without recompiling everything.

import httprouter "gitlab.com/martinr92/gohttprouter/v2"
staticFS := httprouter.NewFS(&staticFiles)
staticFS.UseLocalFolder = true
staticFS.LocalFolderPrefix = "some/folder" // optional

License

Copyright 2018-2021 Martin Riedl

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheControlHeaderHandler

func CacheControlHeaderHandler(w http.ResponseWriter, r *http.Request, info RoutingInfo, file fs.File)

CacheControlHeaderHandler send cache control header of 7 days for the browser and 30 days for the CDN

func ContentLengthHeaderHandler

func ContentLengthHeaderHandler(w http.ResponseWriter, r *http.Request, info RoutingInfo, file fs.File)

ContentLengthHeaderHandler sends the file size

func ContentTypeHeaderHandler

func ContentTypeHeaderHandler(w http.ResponseWriter, r *http.Request, info RoutingInfo, file fs.File)

ContentTypeHeaderHandler sends content type based on the file name extension

func ContentTypeOptionHeaderHandler

func ContentTypeOptionHeaderHandler(w http.ResponseWriter, r *http.Request, info RoutingInfo, file fs.File)

ContentTypeOptionHeaderHandler for nosniff content type option

func DefaultHeaderHandler

func DefaultHeaderHandler(w http.ResponseWriter, r *http.Request, info RoutingInfo, file fs.File)

DefaultHeaderHandler is the default implementation for file headers

func Must

func Must(err error)

Must is a wrapper that panics if an error was returned.

Types

type FS

type FS struct {
	StaticFiles       *embed.FS
	FolderPrefix      string
	UseLocalFolder    bool
	LocalFolderPrefix string
	HeaderHeandler    HeaderHandler
}

FS file system for serving static content.

func NewFS

func NewFS(staticFiles *embed.FS) *FS

NewFS creates a new instance of the http file system used for serving static files.

func (*FS) ServeHTTP

func (fs *FS) ServeHTTP(w http.ResponseWriter, r *http.Request, info RoutingInfo)

type Handler

type Handler interface {
	ServeHTTP(http.ResponseWriter, *http.Request, RoutingInfo)
}

Handler interface that must be implemented by custom structs.

type HeaderHandler

type HeaderHandler func(http.ResponseWriter, *http.Request, RoutingInfo, fs.File)

HeaderHandler for custom http header handling

type RouteHandler

type RouteHandler func(http.ResponseWriter, *http.Request, RoutingInfo)

RouteHandler is the function construct called by this framework.

type Router

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

Router is the main struct for the whole routing logic.

func New

func New() *Router

New creates a new router instance.

func (*Router) Handle

func (router *Router) Handle(method string, path string, handler Handler) error

Handle registers a new handler using method and path for execution.

func (*Router) HandleFunc

func (router *Router) HandleFunc(method string, path string, handler RouteHandler) error

HandleFunc registers a new handler function using method and path for execution.

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(response http.ResponseWriter, request *http.Request)

type RoutingInfo

type RoutingInfo struct {
	Parameters map[string]string
}

RoutingInfo is returned for each web server method call and includes details abount the execution.

Jump to

Keyboard shortcuts

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