GithubHelp home page GithubHelp logo

golangtoolkits / grrt Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 83 KB

GRRT (Go Request RouTer) is a direct replacement for gorilla/mux. It has built-in CORS, path variables and method based routing.

License: MIT License

Go 99.95% Shell 0.05%
cors gorilla gorilla-mux gorilla-sessions mux mux-router go golang

grrt's Introduction

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

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

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

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))
}

grrt's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.