coco is a lightweight, flexible web framework for Go that provides a simple yet powerful API for building web applications.
To get started with coco, you need to have Go installed on your machine. In your go module run:
go get github.com/tobolabs/coco
- 🛣️ Dynamic and static routing with
httprouter
. - 📦 Middleware support for flexible request handling.
- 📑 Template rendering with custom layout configurations.
- 🛠️ Simple API for managing HTTP headers, cookies, and responses.
- 🔄 Graceful shutdown for maintaining service integrity.
- 🔄 JSON and form data handling.
package main
import (
"github.com/tobolabs/coco"
"net/http"
)
func main() {
app := coco.NewApp()
app.Get("/", func(res coco.Response, req *coco.Request, next coco.NextFunc) {
res.Send("Welcome to coco!")
})
app.Listen(":8080")
}
Define routes easily with methods corresponding to HTTP verbs:
app.Get("/users", getUsers)
app.Post("/users", createUser)
app.Put("/users/:id", updateUser)
app.Delete("/users/:id", deleteUser)
app.Param("id", func(res coco.Response, req *coco.Request, next coco.NextFunc, param string) {
// runs for any route with a param named :id
next(res, req)
})
app.Use(func(res coco.Response, req *coco.Request, next coco.NextFunc) {
// Log, authenticate, etc.
next(res, req)
})
app.Get("/greet/:name", func(res coco.Response, req *coco.Request, next coco.NextFunc) {
name := req.GetParam("name")
res.Send("Hello, " + name + "!")
})
app.SetSetting("x-powered-by", false)
isEnabled := app.IsSettingEnabled("x-powered-by")
app.Get("/data", func(res coco.Response, req *coco.Request, next coco.NextFunc) {
data := map[string]interface{}{"key": "value"}
res.JSON(data)
})
var (
//go:embed views
views embed.FS
)
app.LoadTemplates(views, nil)
app.Get("/", func(rw coco.Response, r *coco.Request, next coco.NextFunc) {
rw.Render("index", map[string]interface{}{"title": "Home"})
})
app.Static(http.Dir("public"), "/static")
coco is inspired by Express, a popular web framework for Node.js. At the core of coco is httprouter, a fast HTTP router by Julien Schmidt.