Help us shape the future of Go! Take the 2026 Go Developer Survey

hasty

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: MIT Imports: 5 Imported by: 0

README

hasty GoDoc Go Report Card Sourcegraph

Hasty is Simple and Fast Multiplexer for Go language! It supports:

  • URL Parameters
  • Router Prefix
  • Http method declaration
  • Support for http.Handler and http.HandlerFunc
  • Custom NotFound handler
Example
package main

import (
	"net/http"
	"encoding/json"
	"github.com/harshvladha/hasty"
)

func main() {
	mux := hasty.New()
	
	// mux.Get, Post, Put, etc... takes http.Handler 
	mux.Get("/test", http.HandlerFunc(getHandler))
	
	// :var1 is a path variable
	mux.Get("/test/:var1", http.HandlerFunc(getHandler))
	mux.ListenAndServe("8080")
}

func getHandler(rw http.ResponseWriter, req *http.Request) {
	var queryId string
	// Get the value of var1 path variable
	if query := hasty.GetValue(req, "var1"); query != "" {
		queryId = query
	}
	var myJson = struct {
		First  string
		Second string
		Third  string
	}{"Hello", "World!", queryId}

	json.NewEncoder(rw).Encode(&myJson)
}

Contributing
  • Fork it
  • Create issue on harshvladha/hasty
  • Create your feature/issue related branch (git checkout -b my-new-feature)
  • Write Unit Tests
  • Commit your changes (git commit -am 'Added some feature, Reference to issue.') e.g., Added xyz feature, resolves #1
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request
License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetValue

func GetValue(req *http.Request, variable string) string

GetValue returns the path variable of the requested URL

func SplitInTwo

func SplitInTwo(str string, sep string) (string, string)

SplitInTwo splits the given path in two parts using separator, it scans based on first encounter of the separator

Types

type ErrorStatusCode

type ErrorStatusCode struct {
	HttpStatus int
}

ErrorStatusCode is used to signal for HTTP Error Status Codes

type Mux

type Mux struct {
	Routes *RouteTrie

	Serve func(rw http.ResponseWriter, req *http.Request)
	// contains filtered or unexported fields
}

Mux have routes, notFound (404 status code) handler and Serve which serves incoming requests Routes: all the registered routes notFound: 404 status code handler, default: http.NotFound CaseSensitive: for routes to be case sensitive or not, default false

func New

func New() *Mux

New creates new instance of Mux and returns its pointer

func (*Mux) CaseSensitive

func (mux *Mux) CaseSensitive(cs bool) *Mux

CaseSensitive sets whether routes are case sensitive or not. Default being false

func (*Mux) Connect

func (mux *Mux) Connect(path string, handler http.Handler) *Route

Connect registers route with method CONNECT

func (*Mux) ConnectFunc

func (mux *Mux) ConnectFunc(path string, handler http.HandlerFunc) *Route

ConnectFunc registers route with method CONNECT

func (*Mux) DefaultServe

func (mux *Mux) DefaultServe(rw http.ResponseWriter, req *http.Request)

DefaultServe is the default HTTP request handler

func (*Mux) Delete

func (mux *Mux) Delete(path string, handler http.Handler) *Route

Delete registers route with method DELETE

func (*Mux) DeleteFunc

func (mux *Mux) DeleteFunc(path string, handler http.HandlerFunc) *Route

DeleteFunc registers route with method DELETE

func (*Mux) Get

func (mux *Mux) Get(path string, handler http.Handler) *Route

Get registers route with method GET

func (*Mux) GetFunc

func (mux *Mux) GetFunc(path string, handler http.HandlerFunc) *Route

GetFunc registers route with method GET

func (*Mux) Handle

func (mux *Mux) Handle(path string, handler http.Handler)

Handle registers route with all the methods

func (*Mux) HandleFunc

func (mux *Mux) HandleFunc(path string, handler http.HandlerFunc)

HandleFunc registers route with all the methods

func (*Mux) Head

func (mux *Mux) Head(path string, handler http.Handler) *Route

Head registers route with method HEAD

func (*Mux) HeadFunc

func (mux *Mux) HeadFunc(path string, handler http.HandlerFunc) *Route

HeadFunc registers route with method HEAD

func (*Mux) ListenAndServe

func (mux *Mux) ListenAndServe(port string) error

ListenAndServe appends ":" in port before calling http.ListenAndServe

func (*Mux) NotFound

func (mux *Mux) NotFound(handler http.Handler)

NotFound registers a custom 404 Handler and takes http.Handler as argument

func (*Mux) NotFoundFunc

func (mux *Mux) NotFoundFunc(handlerFunc http.HandlerFunc)

NotFoundFunc registers a custom 404 Handler and takes http.HandlerFunc as argument

func (*Mux) Options

func (mux *Mux) Options(path string, handler http.Handler) *Route

Options registers route with method OPTIONS

func (*Mux) OptionsFunc

func (mux *Mux) OptionsFunc(path string, handler http.HandlerFunc) *Route

OptionsFunc registers route with method OPTIONS

func (*Mux) Patch

func (mux *Mux) Patch(path string, handler http.Handler) *Route

Patch registers route with method PATCH

func (*Mux) PatchFunc

func (mux *Mux) PatchFunc(path string, handler http.HandlerFunc) *Route

PatchFunc registers route with method PATCH

func (*Mux) Post

func (mux *Mux) Post(path string, handler http.Handler) *Route

Post registers route with method POST

func (*Mux) PostFunc

func (mux *Mux) PostFunc(path string, handler http.HandlerFunc) *Route

PostFunc registers route with method POST

func (*Mux) Prefix

func (mux *Mux) Prefix(prefix string) *Mux

Prefix sets a default prefix to all the registered routes

func (*Mux) Put

func (mux *Mux) Put(path string, handler http.Handler) *Route

Put registers route with method PUT

func (*Mux) PutFunc

func (mux *Mux) PutFunc(path string, handler http.HandlerFunc) *Route

PutFunc registers route with method Put

func (*Mux) Register

func (mux *Mux) Register(method string, path string, handler http.Handler) *Route

Register takes method, path and its handler to register the route

func (*Mux) ServeHTTP

func (mux *Mux) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP calls the Serve method of Mux for processing

func (*Mux) Trace

func (mux *Mux) Trace(path string, handler http.Handler) *Route

Trace registers route with method TRACE

func (*Mux) TraceFunc

func (mux *Mux) TraceFunc(path string, handler http.HandlerFunc) *Route

TraceFunc registers route with method TRACE

type Route

type Route struct {
	Path string
	// contains filtered or unexported fields
}

Route contains Path and methods on it is enabled, with Handler to serve request Path: is full path with prefix methods: have value OR'ed for all the methods enabled Handler: has a serving handler for the request

func NewRoute

func NewRoute(url string) *Route

NewRoute creates new instance of Route and returns its pointer

type RouteTrie

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

RouteTrie is a Trie data structure which holds route mapping with path variables it also avoids creating new RouteTrie objects for same parent RouteTrie (repetition)

func (*RouteTrie) String

func (rt *RouteTrie) String(w io.Writer)

String prints the RouteTrie graph

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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