git.iko.soy/typeable/oauth2-proxy
on master help sign in
Create seperate page getter
6c6fd4fJoel Speed
pkg/app/pagewriter/static_pages.go+40−20
@@ -21,7 +21,7 @@ var defaultRobotsTxt []byte
21
21
22// staticPageWriter is used to write static pages.
22// staticPageWriter is used to write static pages.
23type staticPageWriter struct {
23type staticPageWriter struct {
24− pages map[string][]byte
24+ pageGetter *pageGetter
25 errorPageWriter *errorPageWriter
25 errorPageWriter *errorPageWriter
26}
26}
27
27
@@ -32,13 +32,7 @@ func (s *staticPageWriter) WriteRobotsTxt(rw http.ResponseWriter, req *http.Requ
32
32
33// writePage writes the content of the page to the response writer.
33// writePage writes the content of the page to the response writer.
34func (s *staticPageWriter) writePage(rw http.ResponseWriter, req *http.Request, pageName string) {
34func (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))
42 if err != nil {
36 if err != nil {
43 logger.Printf("Error writing %q: %v", pageName, err)
37 logger.Printf("Error writing %q: %v", pageName, err)
44 scope := middlewareapi.GetRequestScope(req)
38 scope := middlewareapi.GetRequestScope(req)
@@ -52,13 +46,13 @@ func (s *staticPageWriter) writePage(rw http.ResponseWriter, req *http.Request,
52}
46}
53
47
54func newStaticPageWriter(customDir string, errorWriter *errorPageWriter) (*staticPageWriter, error) {
48func newStaticPageWriter(customDir string, errorWriter *errorPageWriter) (*staticPageWriter, error) {
55− pages, err := loadStaticPages(customDir)
49+ pageGetter, err := loadStaticPages(customDir)
56 if err != nil {
50 if err != nil {
57 return nil, fmt.Errorf("could not load static pages: %v", err)
51 return nil, fmt.Errorf("could not load static pages: %v", err)
58 }
52 }
59
53
60 return &staticPageWriter{
54 return &staticPageWriter{
61− pages: pages,
55+ pageGetter: pageGetter,
62 errorPageWriter: errorWriter,
56 errorPageWriter: errorWriter,
63 }, nil
57 }, nil
64}
58}
@@ -68,31 +62,57 @@ func newStaticPageWriter(customDir string, errorWriter *errorPageWriter) (*stati
68// instead.
62// instead.
69// Statis files include:
63// Statis files include:
70// - robots.txt
64// - robots.txt
71−func loadStaticPages(customDir string) (map[string][]byte, error) {
65+func loadStaticPages(customDir string) (*pageGetter, error) {
72− pages := make(map[string][]byte)
66+ pages := newPageGetter(customDir)
73
67
74− if err := addStaticPage(pages, customDir, robotsTxtName, defaultRobotsTxt); err != nil {
68+ if err := pages.addPage(robotsTxtName, defaultRobotsTxt); err != nil {
75 return nil, fmt.Errorf("could not add robots.txt: %v", err)
69 return nil, fmt.Errorf("could not add robots.txt: %v", err)
76 }
70 }
77
71
78 return pages, nil
72 return pages, nil
79}
73}
80
74
81−// addStaticPage tries to load the named file from the custom directory.
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
82−// If no custom directory is provided, the default content is used instead.
91+// content will be used instead.
83−func addStaticPage(pages map[string][]byte, customDir, fileName string, defaultContent []byte) error {
92+func (p *pageGetter) addPage(fileName string, defaultContent []byte) error {
84− filePath := filepath.Join(customDir, fileName)
93+ filePath := filepath.Join(p.dir, fileName)
85− if customDir != "" && isFile(filePath) {
94+ if p.dir != "" && isFile(filePath) {
86 content, err := os.ReadFile(filePath)
95 content, err := os.ReadFile(filePath)
87 if err != nil {
96 if err != nil {
88 return fmt.Errorf("could not read file: %v", err)
97 return fmt.Errorf("could not read file: %v", err)
89 }
98 }
90
99
91− pages[fileName] = content
100+ p.pages[fileName] = content
92 return nil
101 return nil
93 }
102 }
94
103
95 // No custom content defined, use the default.
104 // No custom content defined, use the default.
96− pages[fileName] = defaultContent
105+ p.pages[fileName] = defaultContent
97 return nil
106 return nil
98}
107}
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+}
pkg/app/pagewriter/static_pages_test.go+7−7
@@ -16,7 +16,7 @@ import (
16
16
17var _ = Describe("Static Pages", func() {
17var _ = Describe("Static Pages", func() {
18 var customDir string
18 var customDir string
19− const customRobots = "I AM A ROBOT!!!"
19+ const customRobots = "User-agent: *\nAllow: /\n"
20 var errorPage *errorPageWriter
20 var errorPage *errorPageWriter
21 var request *http.Request
21 var request *http.Request
22
22
@@ -110,8 +110,8 @@ var _ = Describe("Static Pages", func() {
110 It("Loads the custom content", func() {
110 It("Loads the custom content", func() {
111 pages, err := loadStaticPages(customDir)
111 pages, err := loadStaticPages(customDir)
112 Expect(err).ToNot(HaveOccurred())
112 Expect(err).ToNot(HaveOccurred())
113− Expect(pages).To(HaveLen(1))
113+ Expect(pages.pages).To(HaveLen(1))
114− Expect(pages).To(HaveKeyWithValue(robotsTxtName, []byte(customRobots)))
114+ Expect(pages.getPage(robotsTxtName)).To(BeEquivalentTo(customRobots))
115 })
115 })
116 })
116 })
117
117
@@ -122,8 +122,8 @@ var _ = Describe("Static Pages", func() {
122
122
123 pages, err := loadStaticPages(customDir)
123 pages, err := loadStaticPages(customDir)
124 Expect(err).ToNot(HaveOccurred())
124 Expect(err).ToNot(HaveOccurred())
125− Expect(pages).To(HaveLen(1))
125+ Expect(pages.pages).To(HaveLen(1))
126− Expect(pages).To(HaveKeyWithValue(robotsTxtName, defaultRobotsTxt))
126+ Expect(pages.getPage(robotsTxtName)).To(BeEquivalentTo(defaultRobotsTxt))
127 })
127 })
128 })
128 })
129 })
129 })
@@ -132,8 +132,8 @@ var _ = Describe("Static Pages", func() {
132 It("Loads the default content", func() {
132 It("Loads the default content", func() {
133 pages, err := loadStaticPages("")
133 pages, err := loadStaticPages("")
134 Expect(err).ToNot(HaveOccurred())
134 Expect(err).ToNot(HaveOccurred())
135− Expect(pages).To(HaveLen(1))
135+ Expect(pages.pages).To(HaveLen(1))
136− Expect(pages).To(HaveKeyWithValue(robotsTxtName, defaultRobotsTxt))
136+ Expect(pages.getPage(robotsTxtName)).To(BeEquivalentTo(defaultRobotsTxt))
137 })
137 })
138 })
138 })
139 })
139 })