gorouter

package module
v0.0.0-...-bf62598 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

gorouter

A simple HTTP router for Go.

Reimplemented Custom HTTP Routing in Go to write human-readable routes and to easily parse values from URI.

Install
$ go get -u github.com/OliverPohlak/gorouter
Example
package main

import (
	"log"
	"net/http"

	"github.com/OliverPohlak/gorouter"
)

type response struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func userHandler(ctx *gorouter.Context) {
	name, err := ctx.GetStringParam("name")
	if err != nil {
		ctx.WriteError(http.StatusBadRequest, err.Error())
		return
	}

	age, err := ctx.GetIntParam("age")
	if err != nil {
		ctx.WriteError(http.StatusBadRequest, err.Error())
		return
	}

	ctx.WriteJSON(http.StatusOK, response{Name: name, Age: age})
}

func main() {
	router := gorouter.NewRouter()
	router.GET(`/user/:name/:age`, userHandler)

	log.Fatalln(http.ListenAndServe(":9000", router))
}

$ curl -i "http://localhost:9000/user/John/23" 
HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 03 Mar 2021 09:56:53 GMT
Content-Length: 24

{"name":"John","age":23}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	http.ResponseWriter
	*http.Request
	Params map[string]string
}

func (*Context) GetIntParam

func (ctx *Context) GetIntParam(key string) (int, error)

func (*Context) GetStringParam

func (ctx *Context) GetStringParam(key string) (string, error)

func (*Context) ReadBody

func (ctx *Context) ReadBody(data interface{}) error

func (*Context) SetParam

func (ctx *Context) SetParam(key string, value string)

func (*Context) WriteError

func (ctx *Context) WriteError(code int, err string)

func (*Context) WriteJSON

func (ctx *Context) WriteJSON(code int, data interface{}) error

func (*Context) WriteString

func (ctx *Context) WriteString(code int, body string)

type Error

type Error struct {
	Error string `json:"error"`
}

type Handler

type Handler func(*Context)

func WrapHandler

func WrapHandler(h http.Handler) Handler

type Route

type Route struct {
	Pattern *regexp.Regexp
	Handler Handler
	Method  string
	Keys    []string
}

type Router

type Router struct {
	Routes       []Route
	DefaultRoute Handler
}

func NewRouter

func NewRouter() *Router

func (*Router) DELETE

func (r *Router) DELETE(pattern string, handler Handler)

func (*Router) GET

func (r *Router) GET(pattern string, handler Handler)

func (*Router) PATCH

func (r *Router) PATCH(pattern string, handler Handler)

func (*Router) POST

func (r *Router) POST(pattern string, handler Handler)

func (*Router) PUT

func (r *Router) PUT(pattern string, handler Handler)

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)

Jump to

Keyboard shortcuts

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