All Projects → arturovm → Min

arturovm / Min

Licence: mit
A minimalistic web framework with route grouping and middleware chaining

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Min

Gin
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Stars: ✭ 53,971 (+56711.58%)
Mutual labels:  middleware, framework, router
Pure Http
✨ The simple web framework for Node.js with zero dependencies.
Stars: ✭ 139 (+46.32%)
Mutual labels:  middleware, framework, router
Twig
Twig - less is more's web server for golang
Stars: ✭ 98 (+3.16%)
Mutual labels:  middleware, framework, router
Dragon
⚡A powerful HTTP router and URL matcher for building Deno web servers.
Stars: ✭ 56 (-41.05%)
Mutual labels:  middleware, framework, router
Diet
A tiny, fast and modular node.js web framework. Good for making fast & scalable apps and apis.
Stars: ✭ 394 (+314.74%)
Mutual labels:  rest, middleware, router
Golf
⛳️ The Golf web framework
Stars: ✭ 248 (+161.05%)
Mutual labels:  middleware, framework, router
Foxify
The fast, easy to use & typescript ready web framework for Node.js
Stars: ✭ 138 (+45.26%)
Mutual labels:  middleware, framework, router
Gearbox
Gearbox ⚙️ is a web framework written in Go with a focus on high performance
Stars: ✭ 455 (+378.95%)
Mutual labels:  rest, framework, router
Zen
zen is a elegant and lightweight web framework for Go
Stars: ✭ 257 (+170.53%)
Mutual labels:  middleware, framework, router
Goat
[DEPRECATED] 🐐 A minimalistic JSON API server in Go
Stars: ✭ 161 (+69.47%)
Mutual labels:  rest, middleware, framework
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+22623.16%)
Mutual labels:  middleware, framework, router
Gear
A lightweight, composable and high performance web service framework for Go.
Stars: ✭ 544 (+472.63%)
Mutual labels:  middleware, framework, router
Rest Control
Framework for testing and validation REST services
Stars: ✭ 51 (-46.32%)
Mutual labels:  rest, framework
Rainbow
An Express router middleware for RESTful API base on file path.
Stars: ✭ 53 (-44.21%)
Mutual labels:  middleware, router
Yarf
Yet Another REST Framework
Stars: ✭ 62 (-34.74%)
Mutual labels:  rest, framework
Looli
a tiny web framework
Stars: ✭ 45 (-52.63%)
Mutual labels:  middleware, framework
Apprun
AppRun is a JavaScript library for developing high-performance and reliable web applications using the elm inspired architecture, events and components.
Stars: ✭ 1,087 (+1044.21%)
Mutual labels:  framework, router
Falcon
The no-nonsense REST API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
Stars: ✭ 8,654 (+9009.47%)
Mutual labels:  rest, framework
Koatty
Koa2 + Typescript = Koatty. Use Typescript's decorator implement IOC and AOP.
Stars: ✭ 67 (-29.47%)
Mutual labels:  middleware, router
Chubbyphp Framework
A based PSR-15 microframework that also sets maximum flexibility with minimum complexity and easy replaceability of the individual components, but also of the framework.
Stars: ✭ 69 (-27.37%)
Mutual labels:  middleware, framework

min

v1.0.0

GoDoc Go Report Card Build Status Codecov GitHub

min is a BYO*, minimalistic web framework that builds on top of your router of choice and adds some additional functionality—namely, middleware chaining and route grouping. It's meant to be used on projects large and small that require flexibility, and varying degrees of custom code and architecture.

This version of min integrates some of the lessons I've learned recently. For this release, I decided to focus on orthogonality and composability, and took a "pure" TDD approach to the API rewrite. The result is a much smaller library with the same functionality, minus some unnecessary abstractions.

This package takes some inspiration from design decisions in chi and gin.

Usage

Hello World

You can initialize a new instance of the Min type with whichever type that implements min.Handler. An adapter for httprouter is included.

import (
    "fmt"
    "net/http"

    "github.com/julienschmidt/httprouter"

    "github.com/arturovm/min"
    "github.com/arturovm/min/adapter"
)

func main() {
    a := &adapter.Httprouter{Router: httprouter.New()}
    m := min.New(a)

    m.Get("/", helloWorld)

    http.ListenAndServe(":8080", m)
}

func helloWorld(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "hello world!")
}

Route Parameters

min supports all the syntax variations for defining route parameters that the underlying router does. For instance, in the case of httprouter:

import (
    "fmt"
    "net/http"

    "github.com/julienschmidt/httprouter"

    "github.com/arturovm/min"
    "github.com/arturovm/min/adapter"
)

func main() {
    a := &adapter.Httprouter{Router: httprouter.New()}
    m := min.New(a)

    m.Get("/:name", greet)

    http.ListenAndServe(":8080", m)
}

func greet(w http.ResponseWriter, r *http.Request) {
    name := httprouter.ParamsFromContext(r.Context()).ByName("name")
    fmt.Fprintf(w, "hello %s!", name)
}

Route Grouping

import (
    "fmt"
    "net/http"

    "github.com/julienschmidt/httprouter"

    "github.com/arturovm/min"
    "github.com/arturovm/min/adapter"
)

func main() {
    a := &adapter.Httprouter{Router: httprouter.New()}
    m := min.New(a)

    apiRouter := m.NewGroup("/api")
    {
        // GET /api
        apiRouter.Get("/", apiRoot)
        // GET /api/ignacio
        apiRouter.Get("/:name", greet)
    }

    http.ListenAndServe(":8080", m)
}

func apiRoot(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "api root")
}

func greet(w http.ResponseWriter, r *http.Request) {
    name := httprouter.ParamsFromContext(r.Context()).ByName("name")
    fmt.Fprintf(w, "hello %s!", name)
}

Middleware

Middleware in min are simply functions that take an http.Handler (the one next in the chain) and return another one. They are resolved in the order that they are chained. You can chain them together with the Middleware.Then method.

min users are meant to take advantage of context to make better use of middleware.

import (
    "context"
    "fmt"
    "log"
    "net/http"

    "github.com/julienschmidt/httprouter"

    "github.com/arturovm/min"
    "github.com/arturovm/min/adapter"
)

func main() {
    a := &adapter.Httprouter{Router: httprouter.New()}
    m := min.New(a)

    chain := min.Middleware(logger).Then(printer)
    m.Use(chain)

    apiRouter := m.NewGroup("/api")
    {
        apiRouter.Get("/", apiRoot)
        nameRouter := apiRouter.NewGroup("/:name")
        {
            // Every request sent to routes defined on this sub-router will now
            // have a reference to a name in its context.
            // Useful for RESTful design.
            nameRouter.Use(nameExtractor)

            // GET /api/ignacio
            nameRouter.Get("/", greet)
            // GET /api/ignacio/goodbye
            nameRouter.Get("/goodbye", goodbye)
        }
    }

    http.ListenAndServe(":8080", m)
}

// -- Middleware --

// a simple logger
func logger(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Printf("| %s %s", r.Method, r.URL)
        next.ServeHTTP(w, r)
    })
}

// a useless middleware that prints text
func printer(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Println("this prints some text")
        next.ServeHTTP(w, r)
    })
}

// extracts a name from the URL and injects it into the request's context
func nameExtractor(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        name := httprouter.ParamsFromContext(r.Context()).ByName("name")
        ctx := context.WithValue(r.Context(), "name", name)
        next.ServeHTTP(w, r.WithContext(ctx))
    })
}

// -- Handlers --

func apiRoot(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "api root")
}

// greets the user with :name
func greet(w http.ResponseWriter, r *http.Request) {
    name := r.Context().Value("name").(string)
    fmt.Fprintf(w, "hello %s!", name)
}

// says "bye" to the user with :name
func goodbye(w http.ResponseWriter, r *http.Request) {
    name := r.Context().Value("name").(string)
    fmt.Fprintf(w, "bye %s!", name)
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].