hyper_mux

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2021 License: AGPL-3.0 Imports: 11 Imported by: 7

README

hyper-mux

A very simplified Mux focused for ease of use.

go get github.com/hyperjumptech/hyper-mux

How to use the Mux

The following is how you going to use Hyper-Mux with vanilla http.Server

import (
    mux "github.com/hyperjumptech/hyper-mux"
    "net/http"
)

var (
    hmux := mux.NewHyperMux()	
)

...
theServer := &http.Server{
    Addr:     "0.0.0.0:8080",
    Handler:  hmux,
}
err := theServer.ListenAndServe()
if err != nil {
    panic(err.Error())
}
...

Adding Routes

First we create the route's handler function

func HandleHelloHyperMux(w http.ResponseWriter, r *http.Request) {
    hmux.WriteString(w, http.StatusOK, "Hello Hyper-Mux")
}

Then we map the HandleFunc function to the route

hmux.AddRoute("/", hmux.MethodGet, HandleHelloHyperMux)

Using Middleware

First we create the middleware

func ContextSetter(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        next.ServeHTTP(w,r)
        fmt.Println("POST CALL")
    })
}

Then we add it to our middleware chain

hmux.UseMiddleware(ContextSetter)

Documentation

Index

Constants

View Source
const (
	MethodGet     = "GET"
	MethodPost    = "POST"
	MethodPut     = "PUT"
	MethodDelete  = "DELETE"
	MethodHead    = "HEAD"
	MethodOptions = "OPTIONS"
	MethodPatch   = "PATCH"
)
View Source
const (
	CharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)

Variables

View Source
var (
	DefaultCORSOption = cors.Options{
		AllowedOrigins:     []string{"*"},
		AllowedMethods:     []string{MethodPost, MethodGet, MethodDelete, MethodPut},
		AllowedHeaders:     []string{"Authorization", "Content-Type", "Content-Length", "Content-Encoding", "Accept", "Accept-Encoding"},
		ExposedHeaders:     []string{"*", "Authorization"},
		MaxAge:             300,
		AllowCredentials:   true,
		OptionsPassthrough: false,
		Debug:              true,
	}
	Cors *cors.Cors
)

Functions

func ContextSetterMiddleware

func ContextSetterMiddleware(next http.Handler) http.Handler

ContextSetterMiddleware is a middleware function that will ensure the request context existance.

func GZIPCompressMiddleware added in v1.1.0

func GZIPCompressMiddleware(next http.Handler) http.Handler

GZIPCompressMiddleware is a wrapper for easy GZIP Content Handling.

func GetRequestID

func GetRequestID(r *http.Request) string

GetRequestID will retrieve the RequestID value from context if ContextSetterMiddleware middleware is used. If ContextSetterMiddleware is not used, it will return empty string

func InternalServerError

func InternalServerError(w http.ResponseWriter, err error)

InternalServerError writes error message to the http.ResponseWriter

func MakeRequestID

func MakeRequestID() string

func NewCORSMiddleware

func NewCORSMiddleware(options cors.Options) func(next http.Handler) http.Handler

NewCORSMiddleware will create CORS middleware to be used in your web app this middleware will handle the OPTIONS method automatically.

func WriteJson

func WriteJson(w http.ResponseWriter, code int, data interface{})

WriteJson simply writes a JSON to the http.ResponseWriter of type application/json if the marshaled data is not marshallable to json, it write an internal server error.

func WriteString

func WriteString(w http.ResponseWriter, code int, text string)

WriteString simply write to the http.ResponseWriter a text of type text/plain

Types

type ContextKey

type ContextKey string
const (
	RequestID ContextKey = "REQUEST-ID"
)

type HyperMux

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

HyperMux holds all the end-point routings and middlewares.

func NewHyperMux

func NewHyperMux() *HyperMux

NewHyperMux creates new instance of HyperMux

func (*HyperMux) AddRoute

func (m *HyperMux) AddRoute(pattern, method string, hFunc http.HandlerFunc)

AddRoute add a routing to a HTTP resource

func (*HyperMux) ServeHTTP

func (m *HyperMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serve the HTTP request, its an implementation of http.Handler.ServeHTTP

func (*HyperMux) UseMiddleware

func (m *HyperMux) UseMiddleware(mw func(next http.Handler) http.Handler)

UseMiddleware append a middleware to the end of middleware chain in this Mux

Jump to

Keyboard shortcuts

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