grrt

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 4 Imported by: 10

README

GRRT (Go Request RouTer)

GRRT (Go Request RouTer) is a direct replacement for the archived gorilla/mux. It has built-in CORS and Method based routing.

Replaces gorilla/mux with one line of code

Quality Gate Status CircleCI Go Report Card

Features

  • Request Routing
  • Method Based Routing
  • CORS
Full REST Service Example Project
Full Web Example Project

Package GolangToolKits/grrt implements a request router and dispatcher for handling incoming requests to their associated handler.

The name mux stands for "HTTP request multiplexer". Like the standard http.ServeMux, grrt.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL. The main features are:

  • Replaces gorilla/mux with one line of code
  • It implements the http.Handler interface so it is compatible with the standard http.ServeMux.
  • URL hosts, paths and query values can be handled.
  • Path variable can be used instead of query parameters with ease.
  • Method base routing is easy
  • CORS is built in with no need for additional modules.


Install

go get -u github.com/GolangToolKits/grrt

RestExample

REST Service Full Example
import(

    "fmt"
    "net/http"
    "os"
    "strconv"
    ph "github.com/GolangToolKits/grrtRouterRestExample/handlers"
    mux "github.com/GolangToolKits/grrt"
)


func main() {

	var sh ph.StoreHandler //see the example project for the full code

	h := sh.New()

	router := mux.NewRouter()
	router.EnableCORS()
	router.CORSAllowCredentials()
	router.SetCorsAllowedHeaders("X-Requested-With, Content-Type, api-key, customer-key, Origin")
	router.SetCorsAllowedOrigins("*")
	router.SetCorsAllowedMethods("GET, DELETE, POST, PUT")

	port := "3000"
	envPort := os.Getenv("PORT")
	if envPort != "" {
		portInt, _ := strconv.Atoi(envPort)
		if portInt != 0 {
			port = envPort
		}
	}

	router.HandleFunc("/rs/product/get/{id}", h.GetProduct).Methods("GET")
	router.HandleFunc("/rs/product/get/{id}/{sku}", h.GetProductWithIDAndSku).Methods("GET")
	router.HandleFunc("/rs/products", h.GetProducts).Methods("GET")
	router.HandleFunc("/rs/product/add", h.AddProduct).Methods("POST")
	router.HandleFunc("/rs/product/update", h.UpdateProduct).Methods("PUT")
	fmt.Println("running on Port:", port)
	http.ListenAndServe(":"+port, (router))

}

WebExample

Web Full Example

import (
	"fmt"
	"html/template"
	"net/http"
	"os"
	"strconv"

	mux "github.com/GolangToolKits/grrt"
	hd "github.com/GolangToolKits/grrtRouterWebSiteExample/handlers"
)

func main() {

	var sh hd.SiteHandler

	sh.Templates = template.Must(template.ParseFiles("./static/index.html",
		"./static/product.html", "./static/addProduct.html"))

	router := mux.NewRouter()

	h := sh.New()

	router.HandleFunc("/", h.Index).Methods("GET")
	router.HandleFunc("/product/{id}/{sku}", h.ViewProduct).Methods("GET")
	router.HandleFunc("/addProduct", h.AddProduct).Methods("POST")

	port := "8080"
	envPort := os.Getenv("PORT")
	if envPort != "" {
		portInt, _ := strconv.Atoi(envPort)
		if portInt != 0 {
			port = envPort
		}
	}

	router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))

	fmt.Println("Web UI is running on port 8080!")

	http.ListenAndServe(":"+port, (router))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetURLVars added in v0.0.7

func SetURLVars(r *http.Request, vars map[string]string) *http.Request

SetURLVars SetURLVars

func Vars

func Vars(r *http.Request) map[string]string

Vars Vars returns the path variables for the current request

Types

type ReqRoute

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

ReqRoute ReqRoute

func (*ReqRoute) GetHandler

func (t *ReqRoute) GetHandler() http.Handler

GetHandler GetHandler

func (*ReqRoute) GetMethods

func (t *ReqRoute) GetMethods() *[]string

GetMethods GetMethods

func (*ReqRoute) GetPath

func (t *ReqRoute) GetPath() string

GetPath GetPath

func (*ReqRoute) GetPathVarsCount

func (t *ReqRoute) GetPathVarsCount() int

GetPathVarsCount GetPathVarsCount

func (*ReqRoute) GetPrefix

func (t *ReqRoute) GetPrefix() string

GetPrefix GetPrefix

func (*ReqRoute) GetVarNames

func (t *ReqRoute) GetVarNames() *[]string

GetVarNames GetVarNames

func (*ReqRoute) Handler

func (t *ReqRoute) Handler(handler http.Handler) Route

Handler Handler

func (*ReqRoute) HandlerFunc

func (t *ReqRoute) HandlerFunc(f func(http.ResponseWriter, *http.Request)) Route

HandlerFunc HandlerFunc

func (*ReqRoute) IsActive

func (t *ReqRoute) IsActive() bool

IsActive IsActive

func (*ReqRoute) IsMethodAllowed

func (t *ReqRoute) IsMethodAllowed(m string) bool

IsMethodAllowed IsMethodAllowed

func (*ReqRoute) IsPathVarsUsed

func (t *ReqRoute) IsPathVarsUsed() bool

IsPathVarsUsed IsPathVarsUsed

func (*ReqRoute) Methods

func (t *ReqRoute) Methods(ms ...string) Route

Methods Methods

func (*ReqRoute) New

func (t *ReqRoute) New() Route

New New creates new Route

func (*ReqRoute) Path

func (t *ReqRoute) Path(p string) Route

Path Path

func (*ReqRoute) PathPrefix

func (t *ReqRoute) PathPrefix(px string) Route

PathPrefix PathPrefix

type ReqRouter

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

ReqRouter RequestRouter

func (*ReqRouter) CORSAllowCredentials added in v0.0.6

func (t *ReqRouter) CORSAllowCredentials()

CORSAllowCredentials CORSAllowCredentials

func (*ReqRouter) EnableCORS

func (t *ReqRouter) EnableCORS()

EnableCORS EnableCORS

func (*ReqRouter) Handle

func (t *ReqRouter) Handle(path string, handler http.Handler) Route

Handle Handle

func (*ReqRouter) HandleFunc

func (t *ReqRouter) HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) Route

HandleFunc HandleFunc

func (*ReqRouter) NewRoute

func (t *ReqRouter) NewRoute() Route

NewRoute NewRoute

func (*ReqRouter) PathPrefix

func (t *ReqRouter) PathPrefix(px string) Route

PathPrefix PathPrefix

func (*ReqRouter) ServeHTTP

func (t *ReqRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP ServeHTTP dispatches the handler registered in the matched route.

func (*ReqRouter) SetCorsAllowedHeaders added in v0.0.2

func (t *ReqRouter) SetCorsAllowedHeaders(hdr string)

SetCorsAllowedHeaders SetAllowedHeaders

func (*ReqRouter) SetCorsAllowedMethods added in v0.0.2

func (t *ReqRouter) SetCorsAllowedMethods(mths string)

SetCorsAllowedMethods AllowedMethods

func (*ReqRouter) SetCorsAllowedOrigins added in v0.0.2

func (t *ReqRouter) SetCorsAllowedOrigins(org string)

SetCorsAllowedOrigins AllowedOrigins

type Route

type Route interface {
	Handler(handler http.Handler) Route
	HandlerFunc(f func(http.ResponseWriter, *http.Request)) Route
	Path(p string) Route
	PathPrefix(p string) Route
	GetHandler() http.Handler
	GetPath() string
	GetPrefix() string
	GetVarNames() *[]string
	IsActive() bool
	Methods(ms ...string) Route
	GetMethods() *[]string
	IsMethodAllowed(m string) bool
	IsPathVarsUsed() bool
	GetPathVarsCount() int
}

Route Route

type Router

type Router interface {
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	Handle(path string, handler http.Handler) Route
	HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) Route
	PathPrefix(path string) Route
	NewRoute() Route

	//CORS methods
	EnableCORS()
	CORSAllowCredentials()
	SetCorsAllowedHeaders(headers string)
	SetCorsAllowedOrigins(origins string)
	SetCorsAllowedMethods(methods string)
}

Router Router

func NewRouter

func NewRouter() Router

NewRouter NewRouter creates new Router

Jump to

Keyboard shortcuts

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