Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add default robots.txt #2834

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
extra: c.Web.Extra,
}

static, theme, tmpls, err := loadWebConfig(web)
static, theme, robots, tmpls, err := loadWebConfig(web)
if err != nil {
return nil, fmt.Errorf("server: failed to load web static: %v", err)
}
Expand Down Expand Up @@ -390,6 +390,8 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)

handlePrefix("/static", static)
handlePrefix("/theme", theme)
handleFunc("/robots.txt", robots)

s.mux = r

s.startKeyRotation(ctx, rotationStrategy, now)
Expand Down
14 changes: 10 additions & 4 deletions server/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func getFuncMap(c webConfig) (template.FuncMap, error) {
// |- themes
// | |- (theme name)
// |- templates
func loadWebConfig(c webConfig) (http.Handler, http.Handler, *templates, error) {
func loadWebConfig(c webConfig) (http.Handler, http.Handler, http.HandlerFunc, *templates, error) {
// fallback to the default theme if the legacy theme name is provided
if c.theme == "coreos" || c.theme == "tectonic" {
c.theme = ""
Expand All @@ -105,18 +105,24 @@ func loadWebConfig(c webConfig) (http.Handler, http.Handler, *templates, error)

staticFiles, err := fs.Sub(c.webFS, "static")
if err != nil {
return nil, nil, nil, fmt.Errorf("read static dir: %v", err)
return nil, nil, nil, nil, fmt.Errorf("read static dir: %v", err)
}
themeFiles, err := fs.Sub(c.webFS, path.Join("themes", c.theme))
if err != nil {
return nil, nil, nil, fmt.Errorf("read themes dir: %v", err)
return nil, nil, nil, nil, fmt.Errorf("read themes dir: %v", err)
}
robotsContent, err := fs.ReadFile(c.webFS, "robots.txt")
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("read robots.txt dir: %v", err)
}

static := http.FileServer(http.FS(staticFiles))
theme := http.FileServer(http.FS(themeFiles))
robots := func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, string(robotsContent)) }

templates, err := loadTemplates(c, "templates")
return static, theme, templates, err

return static, theme, robots, templates, err
}

// loadTemplates parses the expected templates from the provided directory.
Expand Down
2 changes: 2 additions & 0 deletions web/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
2 changes: 1 addition & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io/fs"
)

//go:embed static/* templates/* themes/*
//go:embed static/* templates/* themes/* robots.txt
var files embed.FS

// FS returns a filesystem with the default web assets.
Expand Down