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) {