Sitelet https://github.com/tobolabs/coco/tree/main/.github
Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

coco

coco is a lightweight, flexible web framework for Go that provides a simple yet powerful API for building web applications.

Go Report Card GoDoc Release

Getting Started

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

Features 🚀

  • 🛣️ 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.

Basic Example

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")
}

API Overview

Routing

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)

Parameter Routing

 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)
 })

Middleware

app.Use(func(res coco.Response, req *coco.Request, next coco.NextFunc) {
    // Log, authenticate, etc.
    next(res, req)
})

Dynamic URL Parameters

app.Get("/greet/:name", func(res coco.Response, req *coco.Request, next coco.NextFunc) {
    name := req.GetParam("name")
    res.Send("Hello, " + name + "!")
})

Settings and Custom Configuration

app.SetSetting("x-powered-by", false)
isEnabled := app.IsSettingEnabled("x-powered-by")

Responses

app.Get("/data", func(res coco.Response, req *coco.Request, next coco.NextFunc) {
    data := map[string]interface{}{"key": "value"}
    res.JSON(data)
})

Templates

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"})
 })

Static Files

app.Static(http.Dir("public"), "/static")

Acknowledgments

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.

Author