Download project at step 7 (.zip)

7 / 14

Step 7 · Build a live chat app

🤖 Only admins

Right now anyone who signs in can add rooms. Admins are the people whose addresses are listed in CHAT_ADMINS, separated by commas.

requireAdmin is a guard: it runs before the admin pages, and lets the request through only for an admin. Everyone else gets Page not found, not Forbidden, so nobody else can tell the admin pages exist.

In routes.go, move the admin routes into a group of their own with the guard on it. Then restart rastrillo dev with CHAT_ADMINS set to the address you sign in with.

Check it worked

Signed in as the address you listed, you can use the admin page. Signed in as any other address, you get Page not found.

🤖 These pages were written by an LLM, on the ideas, instruction, and editing of humans. AI-written text here is always marked, visibly — that's factor X.

admin.go Edit

internal/chat/admin.go
package chat

import (
	"net/http"
	"os"
	"slices"
	"strings"

	"amadan.net/rastrillo/rastrillo/action"
	"amadan.net/rastrillo/rastrillo/auth"
	"amadan.net/rastrillo/rastrillo/form"
)

const adminRoomsPage action.Path = "/admin/rooms"

// requireAdmin lets through only the addresses listed in CHAT_ADMINS.
// Anyone else gets 404, so the admin pages do not show they exist.
func (app *App) requireAdmin(next http.Handler) http.Handler {
	return http.HandlerFunc(func(response http.ResponseWriter,
		request *http.Request) {
		identity, _ := auth.From(request)
		admins := strings.Split(os.Getenv("CHAT_ADMINS"), ",")
		if identity.Address == "" ||
			!slices.Contains(admins, identity.Address) {
			ErrorPage(response, request, http.StatusNotFound, "")
			return
		}
		next.ServeHTTP(response, request)
	})
}

// newRoomForm is the admin page: the rooms, and the form that adds one.
type newRoomForm struct {
	app    *App
	parsed *form.Parsed
}

func (page newRoomForm) ShowAgain(
	visit *action.Visit, status int, errors form.Errors,
) {
	rooms, err := page.app.listRooms(visit.Context())
	if visit.Refused(err) {
		return
	}
	visit.Render("admin_rooms", status, map[string]any{
		"Rooms": rooms, "Echo": echoOf(page.parsed), "Errors": errors,
	})
}

func (app *App) adminRooms(visit *action.Visit) {
	newRoomForm{app: app}.ShowAgain(visit, http.StatusOK, nil)
}

func (app *App) addRoom(visit *action.Visit) {
	parsed, err := visit.ReadForm(form.Field{Name: "Name"})
	if visit.RefusedShowing(err, newRoomForm{app, parsed}) {
		return
	}
	room, err := app.createRoom(visit.Context(), parsed.String("Name"))
	if visit.RefusedShowing(err, newRoomForm{app, parsed}) {
		return
	}
	visit.Flash("Added " + room.Name + ".")
	visit.Redirect(string(adminRoomsPage))
}

// echoOf is what the person typed, to fill the form in again.
func echoOf(parsed *form.Parsed) map[string]string {
	if parsed == nil {
		return map[string]string{}
	}
	return parsed.Echo()
}

routes.go Edit

internal/chat/routes.go
package chat

import (
	"net/http"

	"amadan.net/rastrillo/rastrillo/action"
	"amadan.net/rastrillo/rastrillo/csrf"
)

const homePage action.Path = "/"
const accountPage action.Path = "/account"
const livePage action.Path = "/live"

// Routes registers everything the app serves. The API and MCP are outside
// the page group: a bearer token is their only credential, so they get no
// CSRF check and no page middleware.
func Routes(routes *action.Routes, app *App) {
	routes.Mount("/static", assets.Handler(), "static")
	routes.Mount("/api/v1/mcp", app.mcp, "mcp")
	routes.Mount("/api/v1", app.api, "api")
	// Every page and form goes in this group: it is what checks CSRF.
	routes.Group(func(guarded *action.Routes) {
		guarded.Guard("csrf", csrf.Protect(app.origin))
		guarded.Guard("pages", pages.Middleware)
		guarded.Guard("signedIn", app.signedIn)
		// After a first link, the offer of a faster way in, once.
		guarded.Guard("signinGate", app.signins.Gate)
		app.mountAuth(guarded)
		guarded.Get(homePage, app.home)
		// Owned pages and the stream: a visitor signs in first.
		guarded.Group(func(owned *action.Routes) {
			owned.Guard("session", app.auth.RequireSession)
			owned.Get(accountPage, app.account)
			owned.Group(func(admin *action.Routes) {
				admin.Guard("admin", app.requireAdmin)
				admin.Get(adminRoomsPage, app.adminRooms)
				admin.Post(adminRoomsPage, app.addRoom)
			})
			owned.Handle(http.MethodGet, livePage, app.live.Handler(),
				"live")
		})
	})
}
Terminal
CHAT_ADMINS=you@example.com rastrillo dev