Skip to content

Commit

Permalink
Update Proxy to use callback, removing references to Logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-osman committed May 6, 2022
1 parent 471015c commit 76f3405
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
6 changes: 0 additions & 6 deletions proxy/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package proxy

import (
"github.com/nathan-osman/i5/logger"
)

// Config provides the configuration for a proxy instance.
type Config struct {
// Addr provides the remote address to use for proxying requests to.
Addr string
// Mountpoints provides a list of static mountpoints for this proxy.
Mountpoints []*Mountpoint
// Logger is a pointer to a Logger instance.
Logger *logger.Logger
}
20 changes: 14 additions & 6 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"net/url"

"github.com/go-chi/chi"
"github.com/nathan-osman/i5/logger"
"github.com/nathan-osman/i5/util"
)

const ContextSecure = "secure"

// CallbackFn provides a callback function for logging request data.
type CallbackFn func(resp *http.Response)

// Mountpoint represents a static path and directory on-disk.
type Mountpoint struct {
Path string
Expand All @@ -21,9 +23,9 @@ type Mountpoint struct {

// Proxy acts as a reverse proxy and static file server for a specific site.
type Proxy struct {
addr string
router *chi.Mux
logger *logger.Logger
addr string
router *chi.Mux
callbackFn CallbackFn
}

func applyBranding(header http.Header) {
Expand Down Expand Up @@ -55,7 +57,9 @@ func (p *Proxy) handle(w http.ResponseWriter, r *http.Request) {
}
},
ModifyResponse: func(resp *http.Response) error {
p.logger.LogResponse(resp)
if p.callbackFn != nil {
p.callbackFn(resp)
}
applyBranding(resp.Header)
return nil
},
Expand All @@ -70,7 +74,6 @@ func New(cfg *Config) *Proxy {
p := &Proxy{
addr: cfg.Addr,
router: chi.NewRouter(),
logger: cfg.Logger,
}
for _, m := range cfg.Mountpoints {
p.router.Handle(
Expand All @@ -89,6 +92,11 @@ func New(cfg *Config) *Proxy {
return p
}

// SetCallback specifies a callback for requests & responses.
func (p *Proxy) SetCallback(callbackFn CallbackFn) {
p.callbackFn = callbackFn
}

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.router.ServeHTTP(w, r)
}

0 comments on commit 76f3405

Please sign in to comment.