Skip to content

Commit

Permalink
Move all pagewriter related code to dedicated pagewriter package
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed Feb 13, 2021
1 parent b7c76d3 commit b06ff52
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 27 deletions.
6 changes: 3 additions & 3 deletions oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
sessionsapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/app"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/app/pagewriter"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/authentication/basic"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/cookies"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
Expand Down Expand Up @@ -101,7 +101,7 @@ type OAuthProxy struct {
sessionChain alice.Chain
headersChain alice.Chain
preAuthChain alice.Chain
pageWriter app.PageWriter
pageWriter pagewriter.Writer
}

// NewOAuthProxy creates a new instance of OAuthProxy from the options provided
Expand All @@ -121,7 +121,7 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
}
}

pageWriter, err := app.NewPageWriter(app.PageWriterOpts{
pageWriter, err := pagewriter.NewWriter(pagewriter.Opts{
TemplatesPath: opts.Templates.Path,
ProxyPrefix: opts.ProxyPrefix,
Footer: opts.Templates.Footer,
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/error_page.go → pkg/app/pagewriter/error_page.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package pagewriter

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package pagewriter

import (
"errors"
Expand Down
16 changes: 8 additions & 8 deletions pkg/app/pagewriter.go → pkg/app/pagewriter/pagewriter.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package app
package pagewriter

import (
"fmt"
"net/http"
)

// PageWriter is an interface for rednering html templates for both sign-in and
// Writer is an interface for rednering html templates for both sign-in and
// error pages.
// It can also be used to write errors for the http.ReverseProxy used in the
// upstream package.
type PageWriter interface {
type Writer interface {
WriteSignInPage(rw http.ResponseWriter, redirectURL string)
WriteErrorPage(rw http.ResponseWriter, status int, redirectURL string, appError string, messages ...interface{})
ProxyErrorHandler(rw http.ResponseWriter, req *http.Request, proxyErr error)
}

// pageWriter implements the PageWriter interface
// pageWriter implements the Writer interface
type pageWriter struct {
*errorPageWriter
*signInPageWriter
}

// PageWriterOpts contains all options required to configure the template
// Opts contains all options required to configure the template
// rendering within OAuth2 Proxy.
type PageWriterOpts struct {
type Opts struct {
// TemplatesPath is the path from which to load custom templates for the sign-in and error pages.
TemplatesPath string

Expand Down Expand Up @@ -51,9 +51,9 @@ type PageWriterOpts struct {
SignInMessage string
}

// NewPageWriter constructs a PageWriter fro the options given to allow
// NewWriter constructs a Writer fro the options given to allow
// rendering of sign-in and error pages.
func NewPageWriter(opts PageWriterOpts) (PageWriter, error) {
func NewWriter(opts Opts) (Writer, error) {
templates, err := loadTemplates(opts.TemplatesPath)
if err != nil {
return nil, fmt.Errorf("error loading templates: %v", err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package pagewriter

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package pagewriter

import (
"io/ioutil"
Expand All @@ -10,13 +10,13 @@ import (
. "github.com/onsi/gomega"
)

var _ = Describe("PageWriter", func() {
Context("NewPageWriter", func() {
var writer PageWriter
var opts PageWriterOpts
var _ = Describe("Writer", func() {
Context("NewWriter", func() {
var writer Writer
var opts Opts

BeforeEach(func() {
opts = PageWriterOpts{
opts = Opts{
TemplatesPath: "",
ProxyPrefix: "/prefix",
Footer: "<Footer>",
Expand All @@ -31,7 +31,7 @@ var _ = Describe("PageWriter", func() {
Context("With no custom templates", func() {
BeforeEach(func() {
var err error
writer, err = NewPageWriter(opts)
writer, err = NewWriter(opts)
Expect(err).ToNot(HaveOccurred())
})

Expand Down Expand Up @@ -70,7 +70,7 @@ var _ = Describe("PageWriter", func() {

opts.TemplatesPath = customDir

writer, err = NewPageWriter(opts)
writer, err = NewWriter(opts)
Expect(err).ToNot(HaveOccurred())
})

Expand Down Expand Up @@ -117,7 +117,7 @@ var _ = Describe("PageWriter", func() {
})

It("Should return an error", func() {
writer, err := NewPageWriter(opts)
writer, err := NewWriter(opts)
Expect(err).To(MatchError(ContainSubstring("template: sign_in.html:1: function \"Custom\" not defined")))
Expect(writer).To(BeNil())
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package pagewriter

import (
"html/template"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package pagewriter

import (
"html/template"
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/templates.go → pkg/app/pagewriter/templates.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package pagewriter

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package pagewriter

import (
"bytes"
Expand Down

0 comments on commit b06ff52

Please sign in to comment.