Report a Bug | Request a Feature | Ask a Question
🍰 Let’s bake
Getting started with Cake is fast, and it gives your all the tools you need to create a sweet API:
package main
import (
"github.com/arivictor/cake"
"net/http"
)
func main() {
app := cake.New()
app.Route(http.MethodGet, "/:name", Handler)
app.Run("localhost", 8080)
}
func Handler(w http.ResponseWriter, r *http.Request) {
name := cake.URLParam(r, "name", "there")
cake.Send(w, "Hi "+name+"!")
}
Middleware Support
Cake comes boxed with common middleware to get you started. You can load middleware per route or at the global scope.
package main
import (
"github.com/arivictor/cake"
"github.com/arivictor/cake/middleware"
"net/http"
)
func main() {
app := cake.New()
// Global middleware
app.Use(middleware.CORS)
// Route scoped middleware
app.Route(http.MethodGet, "/:name", app.WrapRoute(Handler, middleware.LogRequest))
app.Run("localhost", 8080)
}
func Handler(w http.ResponseWriter, r *http.Request) {
name := cake.URLParam(r, "name", "there")
cake.Send(w, "Hi "+name+"!")
}
You can provide your own by using the below signature
package myapp
import "net/http"
func ExampleMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func (w http.ResponseWriter, r *http.Request) {
// Do stuff here
next.ServeHTTP(w, r)
}
}
^ No preservatives, no added colours, just delicious routing…