Skip to content
Skillv1.0.0

chi

You are an expert in Chi, the lightweight, idiomatic Go HTTP router built on `net/http`. You help developers build composable HTTP services using Chi's middleware stack, route groups, URL parameters,

by terminalskills(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from terminalskills/skills (skills/chi/SKILL.md). Install upstream with npx skills add terminalskills/skills --skill chi. Copyright stays with the author (Apache-2.0).

Chi — Lightweight Go HTTP Router

You are an expert in Chi, the lightweight, idiomatic Go HTTP router built on net/http. You help developers build composable HTTP services using Chi's middleware stack, route groups, URL parameters, sub-routers, and context-based request scoping — providing Express-like ergonomics while staying 100% compatible with Go's standard library.

Core Capabilities

Router and Routes

package main

import (
    "encoding/json"
    "net/http"
    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
    "github.com/go-chi/cors"
)

func main() {
    r := chi.NewRouter()

    // Built-in middleware
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)
    r.Use(middleware.RequestID)
    r.Use(middleware.RealIP)
    r.Use(middleware.Timeout(30 * time.Second))
    r.Use(cors.Handler(cors.Options{
        AllowedOrigins: []string{"https://app.example.com"},
        AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
    }))

    // Public routes
    r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
        json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
    })

    // Protected routes
    r.Route("/api", func(r chi.Router) {
        r.Use(authMiddleware)

        r.Route("/users", func(r chi.Router) {
            r.Get("/", listUsers)
            r.Post("/", createUser)

            r.Route("/{userID}", func(r chi.Router) {
                r.Use(userCtx)            // Load user into context
                r.Get("/", getUser)
                r.Put("/", updateUser)
                r.Delete("/", deleteUser)
                r.Get("/posts", getUserPosts)
            })
        })
    })

    http.ListenAndServe(":3000", r)
}

// Context middleware — load resource once, use in all sub-routes
func userCtx(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        userID := chi.URLParam(r, "userID")
        user, err := db.FindUser(userID)
        if err != nil {
            http.Error(w, "user not found", 404)
            return
        }
        ctx := context.WithValue(r.Context(), "user", user)
        next.ServeHTTP(w, r.WithContext(ctx))
    })
}

func getUser(w http.ResponseWriter, r *http.Request) {
    user := r.Context().Value("user").(*User)
    json.NewEncoder(w).Encode(user)
}

func listUsers(w http.ResponseWriter, r *http.Request) {
    page := r.URL.Query().Get("page")
    users, _ := db.ListUsers(page)
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(users)
}

Installation

go get -u github.com/go-chi/chi/v5

Best Practices

  1. stdlib compatible — Chi handlers are http.HandlerFunc; use any net/http middleware without adapters
  2. Route groups — Use r.Route("/prefix", func(r chi.Router) {...}) for scoped middleware and routes
  3. Context middleware — Load resources in middleware, share via context.WithValue; DRY across sub-routes
  4. URL params — Use chi.URLParam(r, "id") to extract route parameters; type-safe, explicit
  5. Middleware ordering — Logger first, Recoverer second; auth before route-specific middleware
  6. Sub-routers — Mount independent routers: r.Mount("/admin", adminRouter()); clean separation
  7. Timeouts — Use middleware.Timeout to prevent slow handlers from blocking; returns 504 on timeout
  8. No magic — Chi doesn't do dependency injection or auto-binding; explicit is better than implicit in Go

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/terminalskills-skills-chi/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

terminalskills-skills-chi.ocm.jsonjson
{
  "ocm": "1",
  "id": "terminalskills-skills-chi",
  "kind": "skill",
  "name": "chi",
  "description": "You are an expert in Chi, the lightweight, idiomatic Go HTTP router built on `net/http`. You help developers build composable HTTP services using Chi's middleware stack, route groups, URL parameters, sub-routers, and context-based request scoping — providing Express-like ergonomics while staying 100% compatible with Go's standard library.",
  "publisher": "terminalskills",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "coding",
      "math"
    ],
    "tags": [
      "skill-md",
      "go",
      "router",
      "http",
      "middleware",
      "lightweight",
      "stdlib-compatible",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "You are an expert in Chi, the lightweight, idiomatic Go HTTP router built on `net/http`. You help developers build composable HTTP services using Chi's middleware stack, route groups, URL parameters, sub-routers, and context-based request scoping — providing Express-like ergonomics while staying 100% compatible with Go's standard library."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/terminalskills/skills",
      "path": "skills/chi/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/terminalskills/skills/blob/HEAD/skills/chi/SKILL.md",
      "key": "terminalskills/skills/skills/chi/SKILL.md"
    },
    "license": "Apache-2.0"
  },
  "instructions": "# Chi — Lightweight Go HTTP Router\n\nYou are an expert in Chi, the lightweight, idiomatic Go HTTP router built on `net/http`. You help developers build composable HTTP services using Chi's middleware stack, route groups, URL parameters, sub-routers, and context-based request scoping — providing Express-like ergonomics while staying 100% compatible with Go's standard library.\n\n## Core Capabilities\n\n### Router and Routes\n\n```go\npackage main\n\nimport (\n    \"encoding/json\"\n    \"net/http\"\n    \"github.com/go-chi/chi/v5\"\n    \"github.com/go-chi/chi/v5/middleware\"\n    \"github.com/go-chi/cors\"\n)\n\nfunc mai",
  "cost": {
    "context_tokens": 881
  }
}

Fetch it by URL: GET /api/v1/registry/terminalskills-skills-chi/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.