@@ -21,7 +21,7 @@ var defaultRobotsTxt []byte
2121
2222// staticPageWriter is used to write static pages.
2323type staticPageWriter struct {
24− pages map[string][]byte
24+ pageGetter *pageGetter
2525 errorPageWriter *errorPageWriter
2626}
2727
@@ -32,13 +32,7 @@ func (s *staticPageWriter) WriteRobotsTxt(rw http.ResponseWriter, req *http.Requ
3232
3333// writePage writes the content of the page to the response writer.
3434func (s *staticPageWriter) writePage(rw http.ResponseWriter, req *http.Request, pageName string) {
35− content, ok := s.pages[pageName]
36− if !ok {
37− // If the page isn't regiested, something went wrong and there is a bug.
38− // Tests should make sure this code path is never hit.
39− panic(fmt.Sprintf("Static page %q not found", pageName))
40− }
41− _, err := rw.Write(content)
35+ _, err := rw.Write(s.pageGetter.getPage(pageName))
4236 if err != nil {
4337 logger.Printf("Error writing %q: %v", pageName, err)
4438 scope := middlewareapi.GetRequestScope(req)
@@ -52,13 +46,13 @@ func (s *staticPageWriter) writePage(rw http.ResponseWriter, req *http.Request,
5246}
5347
5448func newStaticPageWriter(customDir string, errorWriter *errorPageWriter) (*staticPageWriter, error) {
55− pages, err := loadStaticPages(customDir)
49+ pageGetter, err := loadStaticPages(customDir)
5650 if err != nil {
5751 return nil, fmt.Errorf("could not load static pages: %v", err)
5852 }
5953
6054 return &staticPageWriter{
61− pages: pages,
55+ pageGetter: pageGetter,
6256 errorPageWriter: errorWriter,
6357 }, nil
6458}
@@ -68,31 +62,57 @@ func newStaticPageWriter(customDir string, errorWriter *errorPageWriter) (*stati
6862// instead.
6963// Statis files include:
7064// - robots.txt
71−func loadStaticPages(customDir string) (map[string][]byte, error) {
72− pages := make(map[string][]byte)
65+func loadStaticPages(customDir string) (*pageGetter, error) {
66+ pages := newPageGetter(customDir)
7367
74− if err := addStaticPage(pages, customDir, robotsTxtName, defaultRobotsTxt); err != nil {
68+ if err := pages.addPage(robotsTxtName, defaultRobotsTxt); err != nil {
7569 return nil, fmt.Errorf("could not add robots.txt: %v", err)
7670 }
7771
7872 return pages, nil
7973}
8074
81−// addStaticPage tries to load the named file from the custom directory.
82−// If no custom directory is provided, the default content is used instead.
83−func addStaticPage(pages map[string][]byte, customDir, fileName string, defaultContent []byte) error {
84− filePath := filepath.Join(customDir, fileName)
85− if customDir != "" && isFile(filePath) {
75+// pageGetter is used to load and read page content for static pages.
76+type pageGetter struct {
77+ pages map[string][]byte
78+ dir string
79+}
80+
81+// newPageGetter creates a new page getter for the custom directory.
82+func newPageGetter(customDir string) *pageGetter {
83+ return &pageGetter{
84+ pages: make(map[string][]byte),
85+ dir: customDir,
86+ }
87+}
88+
89+// addPage loads a new page into the pageGetter.
90+// If the given file name does not exist in the custom directory, the default
91+// content will be used instead.
92+func (p *pageGetter) addPage(fileName string, defaultContent []byte) error {
93+ filePath := filepath.Join(p.dir, fileName)
94+ if p.dir != "" && isFile(filePath) {
8695 content, err := os.ReadFile(filePath)
8796 if err != nil {
8897 return fmt.Errorf("could not read file: %v", err)
8998 }
9099
91− pages[fileName] = content
100+ p.pages[fileName] = content
92101 return nil
93102 }
94103
95104 // No custom content defined, use the default.
96− pages[fileName] = defaultContent
105+ p.pages[fileName] = defaultContent
97106 return nil
98107}
108+
109+// getPage returns the page content for a given page.
110+func (p *pageGetter) getPage(name string) []byte {
111+ content, ok := p.pages[name]
112+ if !ok {
113+ // If the page isn't registered, something went wrong and there is a bug.
114+ // Tests should make sure this code path is never hit.
115+ panic(fmt.Sprintf("Static page %q not found", name))
116+ }
117+ return content
118+}