otiai10 / marmoset

The very minimum web toolkit, less than framework
https://github.com/otiai10/marmoset-example
MIT License
11 stars 4 forks source link
go http-router http-server

marmoset

Go codecov Go Report Card Maintainability GoDoc

less than "web framework", just make your code a bit DRY.

func main() {
    router := marmoset.NewRouter()
    router.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("hello!"))
    })
    http.ListenAndServe(":8080", router)
}

Features

func main() {

    router := marmoset.NewRouter()

    router.GET("/foo", your.FooHttpHandlerFunc)
    router.POST("/bar", your.BarHttpHandlerFunc)

    // Path parameters available with regex like declaration
    router.GET("/users/(?P<name>[a-zA-Z0-9]+)/hello", func(w http.ResponseWriter, r *http.Request) {
        marmoset.Render(w).HTML("hello", map[string]string{
            // Path parameters can be accessed by req.FromValue()
            "message": fmt.Printf("Hello, %s!", r.FormValue("name")),
        })
    })

    // Set static file path
    router.Static("/public", "/your/assets/path")

    // Last added filter will be called first.
    server := marmoset.NewFilter(router).
        Add(&your.Filter{}).
        Add(&your.AnotherFilter{}).
        Add(&marmoset.ContextFilter{}). // if you want
        Server()

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