Skip to content

Commit

Permalink
Transform middleware to simple http handler
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Oct 14, 2022
1 parent 0a673b0 commit bfe40b0
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 671 deletions.
9 changes: 0 additions & 9 deletions internal/processor/options.go

This file was deleted.

56 changes: 0 additions & 56 deletions internal/processor/processor.go

This file was deleted.

138 changes: 0 additions & 138 deletions internal/processor/processor_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions internal/proxy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ type URLReplacerFactory interface {
Make(requestURL *url.URL) (*urlreplacer.Replacer, *urlreplacer.Replacer, error)
}

type MiddlewareOption = func(*ProxyMiddleware)
type HandlerOption = func(*Handler)

func WithURLReplacerFactory(replacerFactory URLReplacerFactory) MiddlewareOption {
return func(pm *ProxyMiddleware) {
func WithURLReplacerFactory(replacerFactory URLReplacerFactory) HandlerOption {
return func(pm *Handler) {
pm.replacerFactory = replacerFactory
}
}

func WithHTTPClient(http *http.Client) MiddlewareOption {
return func(pm *ProxyMiddleware) {
func WithHTTPClient(http *http.Client) HandlerOption {
return func(pm *Handler) {
pm.http = http
}
}
93 changes: 55 additions & 38 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,75 @@ import (
"net/http"
"strings"

"github.com/evg4b/uncors/internal/processor"
"github.com/evg4b/uncors/internal/responseprinter"
"github.com/evg4b/uncors/internal/urlreplacer"
"github.com/pterm/pterm"
)

// nolint: revive
type ProxyMiddleware struct {
type Handler struct {
replacerFactory URLReplacerFactory
http *http.Client
proxyWriter pterm.PrefixPrinter
}

func NewProxyMiddleware(options ...MiddlewareOption) *ProxyMiddleware {
middleware := &ProxyMiddleware{}
func NewProxyHandler(options ...HandlerOption) *Handler {
handler := &Handler{
proxyWriter: pterm.PrefixPrinter{
MessageStyle: &pterm.ThemeDefault.InfoMessageStyle,
Prefix: pterm.Prefix{
Style: &pterm.Style{pterm.FgBlack, pterm.BgLightBlue},
Text: " PROXY ",
},
},
}

for _, option := range options {
option(middleware)
option(handler)
}

return middleware
return handler
}

func (pm *ProxyMiddleware) Wrap(_ processor.HandlerFunc) processor.HandlerFunc {
proxyWriter := pterm.PrefixPrinter{
MessageStyle: &pterm.ThemeDefault.InfoMessageStyle,
Prefix: pterm.Prefix{
Style: &pterm.Style{pterm.FgBlack, pterm.BgLightBlue},
Text: " PROXY ",
},
func (handler *Handler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
updateRequest(request)

if err := handler.handle(response, request); err != nil {
pterm.Error.Printfln("UNCORS error: %v", err)
response.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(response, "UNCORS error:", err.Error())
}
}

return func(resp http.ResponseWriter, req *http.Request) error {
if strings.EqualFold(req.Method, http.MethodOptions) {
return makeOptionsResponse(proxyWriter, resp, req)
}
func (handler *Handler) handle(resp http.ResponseWriter, req *http.Request) error {
if strings.EqualFold(req.Method, http.MethodOptions) {
return makeOptionsResponse(handler.proxyWriter, resp, req)
}

targetR, sourceR, err := pm.replacerFactory.Make(req.URL)
if err != nil {
return fmt.Errorf("failed to transform general url: %w", err)
}
targetR, sourceR, err := handler.replacerFactory.Make(req.URL)
if err != nil {
return fmt.Errorf("failed to transform general url: %w", err)
}

originalReq, err := pm.makeOriginalRequest(req, targetR)
if err != nil {
return fmt.Errorf("failed to create reuest to original source: %w", err)
}
originalReq, err := handler.makeOriginalRequest(req, targetR)
if err != nil {
return fmt.Errorf("failed to create reuest to original source: %w", err)
}

originalResp, err := pm.http.Do(originalReq)
if err != nil {
return fmt.Errorf("failed to do reuest: %w", err)
}
originalResp, err := handler.http.Do(originalReq)
if err != nil {
return fmt.Errorf("failed to do reuest: %w", err)
}

defer originalResp.Body.Close()
defer originalResp.Body.Close()

err = pm.makeUncorsResponse(originalResp, resp, sourceR)
if err != nil {
return fmt.Errorf("failed to make uncors response: %w", err)
}
err = handler.makeUncorsResponse(originalResp, resp, sourceR)
if err != nil {
return fmt.Errorf("failed to make uncors response: %w", err)
}

proxyWriter.Println(responseprinter.PrintResponse(originalResp))
handler.proxyWriter.Println(responseprinter.PrintResponse(originalResp))

return nil
}
return nil
}

// nolint: unparam
Expand Down Expand Up @@ -105,3 +112,13 @@ func copyResponseData(header http.Header, resp http.ResponseWriter, targetResp *

return nil
}

func updateRequest(request *http.Request) {
request.URL.Host = request.Host

if request.TLS != nil {
request.URL.Scheme = "https"
} else {
request.URL.Scheme = "http"
}
}
Loading

0 comments on commit bfe40b0

Please sign in to comment.