Skip to content

novrin/reply

Repository files navigation

reply

GoDoc tests Go Report Card

reply is a Go HTTP response engine. It provides convenience methods for common HTTP responses.

Installation

go get github.com/novrin/reply

Usage

In the example below, we add a reply engine to a application and use it in its HTTP handlers to compose replies to requests.

package main

import (
	"fmt"
	"net/http"

	"github.com/novrin/reply/internal/database" 
	"github.com/novrin/reply"
)

// Application orchestrates replies to server requests.
type Application struct {
	db    *database.Queries
	reply reply.Engine // Use a reply Engine to write responses 
}

// Home renders the home template.
func (app *Application) Home(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		app.reply.NotFound(w)
		return
	}
	users, err := app.db.Users(r.Context())
	if err != nil {
		app.reply.InternalServerError(w, fmt.Errorf("failed to retrieve users: %s", err.Error()))
		return
	}
	app.reply.OK(w, reply.Options{
		Key:  "home.html",
		Name: "base",
		Data: struct{ Users []database.Users }{Users: users},
	})
}

License

MIT

Copyright (c) 2023-present novrin