Skip to content
/ nchi Public

golang http router with elegance, speed, and flexibility

License

Notifications You must be signed in to change notification settings

muir/nchi

Repository files navigation

nchi - http router with speed, flexbility, and elegance

GoDoc unit tests report card codecov

nchi is a lightweight, elegant, and fast router for building Go HTTP services. It's especially good at helping you write large REST API services that are kept maintainable as your project grows and changes. nchi is built on top of the nject dependency injection framework and the fastest Go http router, httprouter. nchi is a straight-up rip-off of chi substituting nject for context and in the process making it easier to write middleware and and endpoints.

nchi can use standard middleware and it can use dependency-injection middleware. See:

  • nvelope for nject-based middleware
  • chi for chi's middleware collection

Note: if you're using nvelope.DeferredWriter, avoid other middleware that replaces the http.ResponseWriter.

"Standard" middlewhare has one of the following shapes:

  • func(http.HandlerFunc) http.HandlerFunc
  • func(http.Handler) http.Handler

nchi automatically detects standard middleware and translates it for use in an nject-based framework.

Install

go get github.com/muir/nchi

Examples

As easy as:

package main

import (
	"net/http"

	"github.com/muir/nchi"
	"github.com/go-chi/chi/v5/middleware"
)

func main() {
	r := nchi.NewRouter()
	r.Use(middleware.Logger)
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("welcome"))
	})
	http.ListenAndServe(":3000", r)
}

REST Preview:

Here is a little preview of how routing looks like with nchi.

import (
  //...
  "github.com/muir/nchi"
  "github.com/muir/nvelope"
  "github.com/muir/nject"
  "github.com/go-chi/chi/v5/middleware"
)

func main() {
  r := nchi.NewRouter()

  // A good base middleware stack
  r.Use(
     middleware.RequestID, 
     middleware.RealIP, 
     middleware.Logger,
     nchi.DecodeJSON,
  )

  r.Use(func(inner func() error, w http.ResponseWriter) {
     err := inner()
     if err == nil { 
       return
     }
     code := nvelope.GetReturnCode(err)
     w.WriteHeader(code)
     w.Write([]byte(err.Error()))
  })

  // Set a timeout value on the request context (ctx), that will signal
  // through ctx.Done() that the request has timed out and further
  // processing should be stopped.
  r.Use(middleware.Timeout(60 * time.Second))

  r.Get("/", func(w http.ResponseWriter) {
    w.Write([]byte("hi"))
  })

  // RESTy routes for "articles" resource
  r.Route("/articles", func(r nchi.Router) {
    r.With(paginate).Get("/", listArticles)                           // GET /articles
    r.With(paginate).Get("/:month/:day/:year", listArticlesByDate)    // GET /articles/01-16-2017

    r.Post("/", createArticle)                                        // POST /articles
    r.Get("/search", searchArticles)                                  // GET /articles/search

    r.Get("/:articleSlug", getArticleBySlug)                          // GET /articles/home-is-toronto

    // Subrouters:
    r.Route("/:articleID", func(r nchi.Router) {
      r.Use(LoadArticle)
      r.Get("/", getArticle)                                          // GET /articles/123
      r.Put("/", updateArticle)                                       // PUT /articles/123
      r.Delete("/", deleteArticle)                                    // DELETE /articles/123
    })
  })

  // Mount the admin sub-router
  r.Mount("/admin", adminRouter())

  http.ListenAndServe(":3333", r)
}

func LoadArticle(params nchi.Params) (*Article, nject.TerminalError) {
  articleID := params.ByName("articleID")
  article, err := dbGetArticle(articleID)
  if errors.Is(err, sql.NotFound) {
    return nil, nvelope.NotFound(err)
  }
  return article, err
}

func getArticle(article *Article, w http.ResponseWriter) {
  w.Write([]byte(fmt.Sprintf("title:%s", article.Title)))
}

Developement status

nchi seems to be working fine for the time being so not much is changing. Please file an issue if there is something you would like changed.