This repository has been archived on 2026-09-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
claudeandClaude Sonnet 5 3d1000113c
CI / test (pull_request) Successful in 57s
add CI workflow running go vet and go test
Adds .gitea/workflows/ci.yml: on push to main and on pull requests, run
`go vet ./...` then `go test ./...` in a golang:1.21-alpine container.

Also removes .github/workflows/test.yml, which only triggered on a
`develop` branch that no longer exists and depended on an external
Discord notification action and an unset DISCORD_WEBHOOK secret.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-07 12:50:59 +01:00
2024-07-19 14:46:28 +01:00
2023-10-10 21:50:39 +01:00
2023-10-13 20:44:55 +01:00
2023-11-25 16:51:25 +00:00
2026-07-10 15:42:50 +01:00
2024-06-22 17:29:07 +01:00
2024-07-11 17:33:18 +01:00
2024-07-19 14:46:46 +01:00

Go REST

Some handy functions for developing JSON-based REST APIs. In particular, it simplifies reading HTTP request bodies, writing HTTP response bodies, and handling errors.

Error handling

You can use errors.Is() to ascertain the type of errors thrown by validation functions, but for the most part, this isn't necessary because the write functions already do that.

Example

package main

import (
	"errors"
	"math/rand"
	"net/http"

	"github.com/annybs/go/rest"
)

type Handler struct{}

func (*Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	n := rand.Intn(3)
	if n == 0 {
		rest.WriteResponseJSON(w, http.StatusOK, map[string]string{"status": "OK"})
	} else if n == 1 {
		rest.WriteErrorJSON(w, errors.New("the original error message is added to data.error"))
	} else {
		rest.WriteErrorJSON(w, rest.ErrNotFound)
	}
}

func main() {
	http.ListenAndServe("localhost:8000", &Handler{})
}

Open http://localhost:8000 in your browser and refresh a bunch of times to see the different possible responses.

License

See LICENSE.md

S
Description
No description provided
Readme
56 KiB
Languages
Go 100%