DocsOverview

Overview

A small chi-backed HTTP server, router, middleware, params, JSON helpers, and Server-Sent Events.

http/server is the server half of github.com/toaweme/http. It wraps net/http.Server behind a chi-backed Router and a Name, Start, Stop lifecycle, and bundles the middleware, path params, JSON helpers, and Server-Sent Events you reach for on every service. chi stays an implementation detail, so handlers keep plain net/http signatures and never import it.

Install

go get github.com/toaweme/http/server

Mount a router and serve JSON

Build a Router, register a handler with a method helper, then wrap it in a Server and start it. Handlers use the standard http.ResponseWriter and *http.Request, and reach for WriteJSON to send a response.

package main

import (
	"net/http"

	"github.com/toaweme/http/server"
	"github.com/toaweme/log"
)

func main() {
	logger := log.New()

	r := server.NewRouter()
	r.Get("/health", func(w http.ResponseWriter, _ *http.Request) {
		server.WriteJSON(w, http.StatusOK, map[string]string{"status": "ok"})
	})

	srv := server.NewServer(server.Config{Host: "127.0.0.1", Port: 8080}, r, logger)
	if err := srv.Start(); err != nil {
		log.Fatal("http server failed", "error", err)
	}
}

Start blocks and serves until Stop(ctx) shuts it down gracefully. The Logger argument is satisfied structurally by github.com/toaweme/log, so you can inject that directly or a null logger to discard output.

Note

The module depends only on go-chi/chi. The Server-Sent Events helpers live in the server/sse sub-package, imported as sse.

Where to go next

Start with the guides to learn how each piece behaves, then reach for the reference when you need the exact signature.