From a8705c5df37a7599181a8da2dbcb46914c106a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Sat, 27 Jan 2024 16:28:03 -0800 Subject: [PATCH] handle robots.txt --- cmd/web/routes.go | 3 ++- internal/handlers/handlers.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 581fe08..1f63aff 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -29,8 +29,9 @@ func routes(app *config.AppConfig) http.Handler { mux.Get(handler.Handles, handler.Handler) } - // Setup home handler + // Setup extra handlers mux.Get("/", handlers.HomeHandler) + mux.Get("/robots.txt", handlers.RobotHandler) return mux } diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 6f655f0..4dfe824 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -75,6 +75,19 @@ func HomeHandler(writer http.ResponseWriter, request *http.Request) { render.RenderTemplateWithData(writer, "home.page.tmpl", &d) } +// RobotHandler creates a handler for robots.txt out of existing Handlers +func RobotHandler(writer http.ResponseWriter, request *http.Request) { + robots := fmt.Sprintf("User-agent: %s\nAllow: /\n", request.UserAgent()) + + for _, handler := range Handlers { + robots += fmt.Sprintf("Allow: %s\n", handler.Handles) + } + + robots += "Disallow: /*\n" + + fmt.Fprint(writer, robots) +} + // makeBasicHandler returns a simple handler that renders a template from `name`.page.tmpl func makeBasicHandler(name string) func(writer http.ResponseWriter, request *http.Request) { return func(writer http.ResponseWriter, request *http.Request) {