Simple HTTP router for Go

ngamux

Simple HTTP router for Go



Installation

Run this command with correctly configured Go toolchain.

go get github.com/ngamux/ngamux

Examples

package main

import(
  "net/http"
  "github.com/ngamux/ngamux"
)

func main() {
  mux := ngamux.NewNgamux()
  mux.Get("/", func(rw http.ResponseWriter, r *http.Request) {
    ngamux.JSON(rw, map[string]string{
      "message": "welcome!",
    })
  })
  
  http.ListenAndServe(":8080", mux)
}

Todo

  • Multiple handler (middleware for each route)
  • Route group
  • Route params (in URL parameters)
Comments
  • [PROPOSAL] Refactor Group Code

    [PROPOSAL] Refactor Group Code

    Usage Example

    package main
    
    import (
      "fmt"
      "log"
      "net/http"
      "time"
    
      "github.com/ngamux/ngamux"
    )
    
    func handler(rw http.ResponseWriter, r *http.Request) error {
      fmt.Fprintln(rw, r.URL.Path)
      return nil
    }
    
    func mw(next ngamux.HandlerFunc) ngamux.HandlerFunc {
      return func(rw http.ResponseWriter, r *http.Request) error {
        now := time.Now()
        defer func() {
          log.Println(r.URL.Path, time.Since(now))
        }()
    
        return next(rw, r)
      }
    }
    
    func main() {
      mux := ngamux.NewNgamux()
    
      mux.GroupPrefix("/api", func(api *ngamux.Ngamux) {
        api.Get("/foo", mw(handler))
        api.Get("/bar", mw(handler))
      })
    
      http.ListenAndServe(":8080", mux)
    }
    
    
  • is it ready to be version 1, now?

    is it ready to be version 1, now?

    I need to communicate with all public users, testers, and contributors about version tagging of this HTTP router. is it ready to be version 1, now?

    let me mention all contributors: @born2ngopi @ClavinJune @milhamsuryapratama

  • [PROPOSAL] Wrap http.ResponseWriter and *http.Request in Context

    [PROPOSAL] Wrap http.ResponseWriter and *http.Request in Context

    Some reasons we need to wrap http.ResponseWriter and *http.Request is:

    • cleaner route handler
    • manage request and response in one place
    • like most http router (Fiber, Echo)

    Then we need create one place that contains http.ResponseWriter and *http.Request inside that can manage both easily. It could be like this.

    func IndexHandler(c *ngamux.Ctx) error {
    }
    

    If we see deeper, the Ctx type will be like this.

    type Ctx struct {
      rw http.ResponseWriter
      r *http.Request
    }
    
    // request
    func (c Ctx) GetParam(key string) string
    func (c Ctx) GetJSON(store interface{}) error
    
    // response
    func (c Ctx) String(key string) error
    func (c Ctx) JSON(data interface{}) error
    
    // other method implementation here
    
  • Little revamp for ngamux to moving into interface base style code

    Little revamp for ngamux to moving into interface base style code

    Hi I've made some changes, to moving ngamux to use interface for clearance purpose rather than directly returns struct of definition of ngamux, for detail please check the commit messages.

  • [BUG] Handle Error Produced by Handler

    [BUG] Handle Error Produced by Handler

    Describe the bug Handler has error as return value. It can be nil if no errors. Otherwise, error object if an error happen.

    To Reproduce Steps to reproduce the behavior:

    1. Create route and bind with handler
    mux.Get("/", func(rw http.ResponseWriter, r *http.Request) error {
      return errors.New("something bad")
    })
    
    1. The error is not handled.

    Expected behavior Should send HTTP response with error message and status.

  • [FEATURE] implementing global middleware

    [FEATURE] implementing global middleware

    for implement global middleware, we can use method for assign middleware to route/handler

    example:

    mux.Use(func(next ngamux.HandlerFunc) ngamux.HandlerFunc {
    	return func(rw http.ResponseWriter, r *http.Request) error {
    		fmt.Println("hello from middleware")
    		return next(rw, r)
    	}
    })
    
  • [BUG] fixing panic when route not found

    [BUG] fixing panic when route not found

    Describe The Bug

    foundRoute handler is replaced with nil whenever a route is not found

    To Reproduce

    1. Run example/basic/basic.go
    2. Access unregistered endpoint (e.g: https://localhost:8080/foobar)
    3. Golang panics

    Expected Behavior

    r.config.NotFoundHandler is called

    Additional Context

    https://github.com/ngamux/ngamux/blob/5f35c09b6819d402a387089d3d578b282499e2a1/ngamux.go#L138-L142 the foundroute is always replaced

  • [FEATURE] add panic handler to avoid panic on serving HTTP requests

    [FEATURE] add panic handler to avoid panic on serving HTTP requests

    Description

    In Order to avoid panicking when serving HTTP Requests, I implement the basic panicHandler, read the example/panic/main.go for full usage example

  • add middleware and change logic copy routesMap

    add middleware and change logic copy routesMap

    add recovery and cors in middleware with example how to use

    change logic copy routesMap, why we must change previous logic. because the previous logic only copy first map. and not copy second map. for performance

    before:

    Concurrency Level:      1
    Time taken for tests:   56.737 seconds
    Complete requests:      100000
    Failed requests:        0
    Total transferred:      13500000 bytes
    HTML transferred:       2700000 bytes
    Requests per second:    1762.52 [#/sec] (mean)
    Time per request:       0.567 [ms] (mean)
    Time per request:       0.567 [ms] (mean, across all concurrent requests)
    Transfer rate:          232.36 [Kbytes/sec] received
    

    after:

    Concurrency Level:      1
    Time taken for tests:   32.535 seconds
    Complete requests:      100000
    Failed requests:        0
    Total transferred:      13600000 bytes
    HTML transferred:       2800000 bytes
    Requests per second:    3073.65 [#/sec] (mean)
    Time per request:       0.325 [ms] (mean)
    Time per request:       0.325 [ms] (mean, across all concurrent requests)
    Transfer rate:          408.22 [Kbytes/sec] received
    
    
  • improve more performa in matching route

    improve more performa in matching route

    I've done performance testing using apache banchmark with the following results

    cpu : Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz

    before :

    Document Path:          /users/123/slug-name
    Document Length:        0 bytes
    
    Concurrency Level:      1
    Time taken for tests:   18.295 seconds
    Complete requests:      100000
    Failed requests:        0
    Total transferred:      7500000 bytes
    HTML transferred:       0 bytes
    Requests per second:    5466.11 [#/sec] (mean)
    Time per request:       0.183 [ms] (mean)
    Time per request:       0.183 [ms] (mean, across all concurrent requests)
    Transfer rate:          400.35 [Kbytes/sec] received
    
    

    after:

    Document Path:          /users/123/slug-name
    Document Length:        0 bytes
    
    Concurrency Level:      1
    Time taken for tests:   16.679 seconds
    Complete requests:      100000
    Failed requests:        0
    Total transferred:      7500000 bytes
    HTML transferred:       0 bytes
    Requests per second:    5995.52 [#/sec] (mean)
    Time per request:       0.167 [ms] (mean)
    Time per request:       0.167 [ms] (mean, across all concurrent requests)
    Transfer rate:          439.13 [Kbytes/sec] received
    
    

    kan lumayan XD

  • [PROPOSAL] Return Header Only on Head Request

    [PROPOSAL] Return Header Only on Head Request

    Is your feature request related to a problem? Please describe. Some conditions, we need to get response header only. It can do by send request using HEAD method. But, I can't do it using this library.

    Describe the solution you'd like Check HEAD request method for all request. Remove the body and send header only.

    References:

    • https://reqbin.com/Article/HttpHead
Related tags
xujiajun/gorouter is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework.

gorouter xujiajun/gorouter is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework. Motivation I wanted a sim

Dec 8, 2022
Fast, simple, and lightweight HTTP router for Golang

Sariaf Fast, simple and lightweight HTTP router for golang Install go get -u github.com/majidsajadi/sariaf Features Lightweight compatible with net/ht

Aug 19, 2022
Simple router build on `net/http` supports custom middleWare.

XMUS-ROUTER Fast lightweight router build on net/http supports delegate and in url params. usage : Create new router using NewRouter() which need rout

Dec 27, 2021
Simple HTTP router for Go

ngamux Simple HTTP router for Go Installation Examples Todo Installation Run this command with correctly configured Go toolchain. go get github.com/ng

Dec 13, 2022
Simple http router.

Simple router based on tree structure. import ( "fmt" "log" "net/http" router "github.com/dmitruk-v/router/v1" ) func main() { ro := router.New

Dec 29, 2021
FastRouter is a fast, flexible HTTP router written in Go.

FastRouter FastRouter is a fast, flexible HTTP router written in Go. FastRouter contains some customizable options, such as TrailingSlashesPolicy, Pan

Sep 27, 2022
Go Server/API micro framework, HTTP request router, multiplexer, mux
Go Server/API micro framework, HTTP request router, multiplexer, mux

?? gorouter Go Server/API micro framework, HTTP request router, multiplexer, mux. ?? ABOUT Contributors: Rafał Lorenz Want to contribute ? Feel free t

Dec 16, 2022
A high performance HTTP request router that scales well

HttpRouter HttpRouter is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go. In contrast to the

Dec 28, 2022
High-speed, flexible tree-based HTTP router for Go.

httptreemux High-speed, flexible, tree-based HTTP router for Go. This is inspired by Julien Schmidt's httprouter, in that it uses a patricia tree, but

Dec 28, 2022
:rotating_light: Is a lightweight, fast and extensible zero allocation HTTP router for Go used to create customizable frameworks.
:rotating_light: Is a lightweight, fast and extensible zero allocation HTTP router for Go used to create customizable frameworks.

LARS LARS is a fast radix-tree based, zero allocation, HTTP router for Go. view examples. If looking for a more pure Go solution, be sure to check out

Dec 27, 2022
A powerful HTTP router and URL matcher for building Go web servers with 🦍

gorilla/mux https://www.gorillatoolkit.org/pkg/mux Package gorilla/mux implements a request router and dispatcher for matching incoming requests to th

Jan 9, 2023
An extremely fast Go (golang) HTTP router that supports regular expression route matching. Comes with full support for building RESTful APIs.

ozzo-routing You may consider using go-rest-api to jumpstart your new RESTful applications with ozzo-routing. Description ozzo-routing is a Go package

Dec 31, 2022
Pure is a fast radix-tree based HTTP router
Pure is a fast radix-tree based HTTP router

package pure Pure is a fast radix-tree based HTTP router that sticks to the native implementations of Go's "net/http" package; in essence, keeping the

Dec 1, 2022
Go HTTP router

violetear Go HTTP router http://violetear.org Design Goals Keep it simple and small, avoiding extra complexity at all cost. KISS Support for static an

Dec 10, 2022
lightweight, idiomatic and composable router for building Go HTTP services

chi is a lightweight, idiomatic and composable router for building Go HTTP services. It's especially good at helping you write large REST API services

Jan 8, 2023
:tongue: CleverGo is a lightweight, feature rich and high performance HTTP router for Go.

CleverGo CleverGo is a lightweight, feature rich and trie based high performance HTTP request router. go get -u clevergo.tech/clevergo English 简体中文 Fe

Nov 17, 2022
Fast and flexible HTTP router
Fast and flexible HTTP router

treemux - fast and flexible HTTP router Basic example Debug logging CORS example Error handling Rate limiting using Redis Gzip compression OpenTelemet

Dec 27, 2022
Lightweight Router for Golang using net/http standard library with custom route parsing, handler and context.

Go-Lightweight-Router Lightweight Router for Golang using net/http standard library with custom route parsing, handler and context. Further developmen

Nov 3, 2021
A high performance HTTP request router that scales well

HttpRouter HttpRouter is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go. In contrast to the

Dec 9, 2021