Skip to content

Commit

Permalink
update to use Rewrite properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ruckc committed Aug 3, 2024
1 parent da11712 commit 25fc138
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /app

COPY . /app

RUN CGO_ENABLED=0 go build -o goproxygo cmd/goproxygo/main.go
RUN CGO_ENABLED=0 go build -trimpath -o goproxygo cmd/goproxygo/main.go

FROM gcr.io/distroless/static-debian12

Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ a simple reverse proxy implementation for quick container assmebling behind a si

## Usage

This example runs a webserver at `http://localhost:8080` with /api/ mapped to localhost:8081 and / mapped to localhost:8082. The first matching regular expression gets the request.
This example runs a webserver at `http://localhost:8080` with /api/ mapped to localhost:8081 and / mapped to localhost:8082. The first matching path to match gets the request.
```
goproxygo /api/.*:http://localhost:8081 /.*:http://localhost:8082
goproxygo /api:http://localhost:8081 /:http://localhost:8082
```

This example runs a webserver at `http://0.0.0.0:8080` with /api/ mapped to localhost:8081 and / mapped to localhost:8082. The first matching regular expression gets the request.
This example runs a webserver at `http://0.0.0.0:8080` with /api/ mapped to localhost:8081 and / mapped to localhost:8082. The first matching path to match gets the request.
```
goproxygo --host 0.0.0.0 /api/.*:http://localhost:8081 /.*:http://localhost:8082
goproxygo --host 0.0.0.0 /api:http://localhost:8081 /:http://localhost:8082
```

This example runs a webserver at `http://0.0.0.0:8000` with /api/ mapped to localhost:8081 and / mapped to localhost:8082. The first matching regular expression gets the request.
This example runs a webserver at `http://0.0.0.0:8000` with /api/ mapped to localhost:8081 and / mapped to localhost:8082. The first matching path to match gets the request.
```
goproxygo --host 0.0.0.0 --port 8000 /api/.*:http://localhost:8081 /.*:http://localhost:8082
goproxygo --host 0.0.0.0 --port 8000 /api:http://localhost:8081 /:http://localhost:8082
```

## Docker Compose example
Expand All @@ -35,9 +35,9 @@ services:
- "0.0.0.0"
- "--port"
- "8080"
- /auth/.*:http://keycloak:8080/auth/
- /api/.*:http://api:8080/api/
- /.*:http://ui:8080/
- /auth:http://keycloak:8080/auth
- /api:http://api:8080/api
- /:http://ui:8080
links:
- keycloak
```
Expand Down
23 changes: 10 additions & 13 deletions cmd/goproxygo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
)

Expand All @@ -29,7 +28,7 @@ func main() {
}

type ProxyRoute struct {
path *regexp.Regexp
path *string
proxy *httputil.ReverseProxy
}

Expand All @@ -38,20 +37,19 @@ type Router struct {
}

func (router *Router) Handle(path string, destination string) {
regexpPath := regexp.MustCompile(path)
destinationUrl, err := url.Parse(destination)
if err != nil {
log.Fatal(err)
}
handler := reverseProxyHandler(destinationUrl)

log.Printf("Registering handler for %s to %s\n", regexpPath, destinationUrl)
router.routes = append(router.routes, &ProxyRoute{regexpPath, handler})
log.Printf("Registering handler for %s to %s\n", path, destinationUrl)
router.routes = append(router.routes, &ProxyRoute{&path, handler})
}

func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, route := range router.routes {
matched := route.path.MatchString(r.URL.Path)
matched := strings.HasPrefix(r.URL.Path, *route.path)
//log.Printf("Checking if %s matches %s and the result is %t", r.URL.Path, route.path, matched)
if matched {
route.proxy.ServeHTTP(w, r)
Expand All @@ -62,13 +60,12 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func reverseProxyHandler(destination *url.URL) *httputil.ReverseProxy {
proxy := httputil.NewSingleHostReverseProxy(destination)

proxy.Rewrite = func(r *httputil.ProxyRequest) {
r.Out.URL.Scheme = destination.Scheme
r.Out.URL.Host = destination.Host

fmt.Println(req)
proxy := &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(destination)
r.SetXForwarded()
r.Out.Host = r.In.Host
},
}

return proxy
Expand Down

0 comments on commit 25fc138

Please sign in to comment.