handle robots.txt

This commit is contained in:
Lilian Jónsdóttir 2024-01-27 16:28:03 -08:00
parent f82809bfb1
commit a8705c5df3
2 changed files with 15 additions and 1 deletions

View file

@ -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
}

View file

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