Skip to content

Commit

Permalink
Merge pull request #4 from thoughtgears/feature/multiple-proxies
Browse files Browse the repository at this point in the history
addded a workflow to build dockerfile
  • Loading branch information
jensskott authored Mar 6, 2024
2 parents e93f3c2 + 429640d commit f7baae6
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 11 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Publish Docker image

on:
push:
branches:
- main

env:
IMAGE_NAME: proximo

jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
tags: |
type=sha
- name: Set outputs
id: vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
GIT_REPO=${{ github.repository }}
GIT_COMMIT=${{ steps.vars.outputs.short_sha }}
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
###########################
# Build container for app #
###########################

# Build arg VERSION only works before the FROM statement
ARG VERSION=latest
FROM golang:${VERSION} AS builder

# Sets build args for container
ARG SERVICE_NAME
ARG GIT_COMMIT
ARG GIT_REPO

# Set proper workdir for build
WORKDIR /go/src/github.com/${GIT_REPO}

# Copy, get all packages and build application
COPY . .
RUN go mod tidy && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-X main.Version=${GIT_COMMIT}" -o ./builds/${SERVICE_NAME}-linux-amd64

##############################
# Deployment container build #
##############################

FROM alpine:latest
# Sets build args for container
ARG GIT_REPO
ARG SERVICE_NAME

# Install Root Certificates for https endpoints
RUN apk update
RUN apk --no-cache add ca-certificates

# Set workdir for app and copy from the builder image
WORKDIR /root/
COPY --from=builder /go/src/github.com/${GIT_REPO}/builds/${SERVICE_NAME}-linux-amd64 ./app
ENTRYPOINT ["./app"]
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ endif
OS = $(shell uname -s | tr '[:upper:]' '[:lower:]')
ARCH = $(shell uname -m)
GIT_COMMIT = $(shell git rev-parse --short HEAD)
GIT_REPO = "thoughtgears/proximo"
SERVICE_NAME = proximo

.PHONY: clean build test
Expand All @@ -18,3 +19,10 @@ clean:

build:
@CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build -a -installsuffix cgo -ldflags="-X main.Version=$(GIT_COMMIT)" -o builds/$(SERVICE_NAME)-$(OS)-$(ARCH)

docker:
@docker build \
--build-arg=GIT_COMMIT=$(GIT_COMMIT) \
--build-arg=GIT_REPO=$(GIT_REPO) \
-t $(SERVICE_NAME):$(GIT_COMMIT) .
@docker tag $(SERVICE_NAME):$(GIT_COMMIT) $(SERVICE_NAME):latest
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Proximo

A simple, lightweight HTTP proxy to allow anyone to access cloud run endpoints behind authentication. This will proxy requests to the cloud run endpoint, and add the `Authorization` header to the request based on your credentials.
A simple, lightweight HTTP proxy to allow anyone to access cloud run endpoints behind authentication. This will proxy
requests to the cloud run endpoint, and add the `Authorization` header to the request based on your credentials.

## Usage

Expand All @@ -20,23 +21,26 @@ gcloud auth login
proximo -targets service=https://my-cloud-run-endpoint.run.app
```

This will start the proxy on port 8080, proxying requests to the given Cloud Run URL and adding the Authorization header with your Google credentials. You should be able to access the endpoint at http://localhost:8080.
This will start the proxy on port 8080, proxying requests to the given Cloud Run URL and adding the Authorization
header with your Google credentials. You should be able to access the endpoint at http://localhost:8080.

**Start with multiple targets**

```shell
proximo -targets service1=https://service1.com,service2=https://service2.com
```

This will start the proxy on port 8080, proxying requests to the given Cloud Run URLs and adding the Authorization header with your Google credentials. You should be able to access the endpoints at http://localhost:8080.
This will start the proxy on port 8080, proxying requests to the given Cloud Run URLs and adding the Authorization
header with your Google credentials. You should be able to access the endpoints at http://localhost:8080.

**Start with config file**

```shell
proximo -file config.yaml
```

This will start the proxy on port 8080, proxying requests to the given Cloud Run URLs and adding the Authorization header with your Google credentials. You should be able to access the endpoints at http://localhost:8080.
This will start the proxy on port 8080, proxying requests to the given Cloud Run URLs and adding the Authorization
header with your Google credentials. You should be able to access the endpoints at http://localhost:8080.
This will use a config file to specify target and url mappings.

```json
Expand Down
24 changes: 17 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,33 @@ type Config struct {
func main() {
targets := flag.String("targets", "", "Comma-separated list of target URLs and hosts, e.g.-targets search=https://search.com,api=https://api.com")
configFile := flag.String("file", "", "Path to the configuration file, e.g. -file config.json")
localPort := flag.String("port", "8080", "Local port to listen on, e.g. -port 8080 (default: 8080)")
auth := flag.Bool("auth", true, "Enable authentication, e.g. -auth false (default: true)")
localPort := flag.String("port", "8080", "Local port to listen on, e.g. -port 8080")
auth := flag.Bool("auth", true, "Enable ADC for Google Cloud authentication, e.g. -auth false")
helpFlag := flag.Bool("help", false, "Show usage help, e.g. -help")
flag.Parse()

var hostToTarget map[string]string
if *helpFlag {
fmt.Println("Usage:")
flag.PrintDefaults() // Print the usage for all defined flags .
os.Exit(0) // Exit after displaying help
}

var hostMapping map[string]string

if *configFile != "" {
cfg, err := loadConfig(*configFile)
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
hostToTarget = cfg.Targets
hostMapping = cfg.Targets
} else if *targets != "" {
hostToTarget = parseHostToTarget(*targets)
hostMapping = parseHostToTarget(*targets)
} else {
log.Fatal("Missing required flag: either -file or -url must be provided")
}

proxies := make(map[string]*httputil.ReverseProxy)
for host, targetURL := range hostToTarget {
for host, targetURL := range hostMapping {
target, err := url.Parse(targetURL)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -77,7 +84,10 @@ func main() {

http.HandleFunc("/", loggingMiddleware(handleRequest(proxies)))

log.Printf("Starting server at http://localhost:%s\n", *localPort)
log.Printf("Proxy started @ http://localhost:%s - Handling:\n", *localPort)
for host, targetURL := range hostMapping {
fmt.Printf("host: %s - proxy origin: %s\n", host, targetURL)
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", *localPort), nil))
}

Expand Down

0 comments on commit f7baae6

Please sign in to comment.