Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/valstack-dev/go-trie-url-route

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/valstack-dev/go-trie-url-route

  • v0.0.1
  • Source
  • Go
  • Socket score

Version published
Created
Source

Route

A very efficient and minimal HTTP Go router, using a Trie data structure for efficiency.

Based on https://github.com/ant0ine/go-json-rest with the kitchen-sink philosophy stuff removed (so much that I wouldn't particularly consider it a fork).

It aims to be the innermost handler in a composition of handlers. It doesn't require methods to have http.HandlerFunc signature, as route paths are interface{}.

Install

go get github.com/mantasmatelis/go-route

Basic Usage

import(
    "net/http"
    "github.com/mantasmatelis/go-trie-url-route")

var router route.Router

func main() {
    router.SetRoutes(
	route.Route{"POST", "/count/:Count",    SetCount},
        route.Route{"GET",  "/count"            GetCount},
        route.Route{"POST", "/count",           IncrementCount},
        route.Route{"POST", "/reset",           ResetCount},
    )
    
    http.ListenAndServe(":3000", http.HandlerFunc(handler))
}

func handler(w http.ResponseWriter, r *http.Request) {
    route, params, pathMatched := router.FindRouteFromURL(r.Method, r.URL)
    if route != nil {
        route.Func.(func(http.ResponseWriter, *http.Request, map[string]string))(w, r, params)
    }
}

...

Differences

How is this different from ant0ine/go-json-rest? This doesn't:

  • Log
  • Handle errors
  • Route automatically
  • Have any settings
  • Override http.Request or http.ResponseWriter (which makes interfacing with other libraries easier)
  • Force specific handler signatures on you (in the example you can see the route.Func is typecast before being called. Use this feature to pass a "Context" struct containing request information, like the authenticated user object, or pass the "params" in the url string.)

It exposes the basic router so that your own stack of handlers can be easily written, customized to specific needs.

Enjoy! Pull requests maintaining the basic above philosophy are welcomed.

FAQs

Package last updated on 13 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc