modproxy

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 7 Imported by: 0

README

modproxy

modproxy directs go get from one location to another.

Configuration

The behaviour of modproxy can be configured using the following environment variables:

  • HOST_PATTERN: Specifies the pattern for host matching. Defaults to "go.loafoe.dev".
  • HOST_REPLACEMENT: Determines the replacement for the host. Defaults to "github.com".
  • PATH_PATTERN: Sets the pattern for path matching. Defaults to "/".
  • PATH_REPLACEMENT: Defines the replacement for the path. Defaults to "/loafoe-dev/go-".

Run locally

FUNCTION_TARGET=ModProxy LOCAL_ONLY=true go run cmd/main.go
  • FUNCTION_TARGET: Specifies the name of the function to be executed when the server is started.
  • LOCAL_ONLY: When set to true, the server listens only on 127.0.0.1 (localhost), restricting access to the local machine. This is useful for local testing, avoiding firewall warnings, and preventing external access to the server during development or testing phases. If not set, listen on all interfaces.

Confirm the url is correctly being rewritten:

curl -H 'Host: go.loafoe.dev' localhost:8080/modproxy
# Output: <html><head><meta name="go-import" content="go.loafoe.dev/modproxy git https://github.com/loafoe-dev/go-modproxy"></head><body></body></html>

Run using pack and Docker

pack build \
  --builder gcr.io/buildpacks/builder:v1 \
  --env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
  --env GOOGLE_FUNCTION_TARGET=ModProxy \
  go-modproxy
  • GOOGLE_FUNCTION_SIGNATURE_TYPE: Specifies the type of function signature the application uses.
  • GOOGLE_FUNCTION_TARGET: Specifies the name of the function to execute in the application.

Run the built image:

docker run --rm -p 8080:8080 go-modproxy

Confirm the url is correctly being rewritten:

curl -H 'Host: go.loafoe.dev' localhost:8080/modproxy
# Output: <html><head><meta name="go-import" content="go.loafoe.dev/modproxy git https://github.com/loafoe-dev/go-modproxy"></head><body></body></html>

Run using Google Cloud Platform

SERVICE="go-modproxy"
REGION="europe-west4"
BUILD_REGION="europe-west1" # https://cloud.google.com/build/docs/locations#restricted_regions_for_some_projects
BILLING_ACCOUNT_ID=$(gcloud billing accounts list --filter="OPEN = True" --format="value(ACCOUNT_ID)") # use first enabled billing account
PROJECT_ID="go-modproxy"
CLOUDBUILD_BUCKET="gs://${PROJECT_ID}_cloudbuild"
ARTIFACTS_REPOSITORY="cloud-run-source-deploy"
REPOSITORY_URI=$REGION-docker.pkg.dev/$PROJECT_ID/$ARTIFACTS_REPOSITORY/$SERVICE

# Create new GCP project
gcloud projects create "$PROJECT_ID"

# Link billing account
gcloud billing projects link "$PROJECT_ID" --billing-account="$BILLING_ACCOUNT_ID"

# Enable required GCP services
gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com --project="$PROJECT_ID"

# Create GCS bucket for builds
gcloud storage buckets create "$CLOUDBUILD_BUCKET" --location="$REGION" --project="$PROJECT_ID"

# Create Docker artifact repository
gcloud artifacts repositories create "$ARTIFACTS_REPOSITORY" --repository-format=docker --location="$REGION" --project="$PROJECT_ID"

# Submit build to Cloud Build
gcloud builds submit . --config cloudbuild.yaml --substitutions=_REPOSITORY_URI=$REPOSITORY_URI,COMMIT_SHA=$(git rev-parse HEAD) --region="$BUILD_REGION" --project="$PROJECT_ID"

# Deploy service to Cloud Run
gcloud run deploy "$SERVICE" --image="$REPOSITORY_URI:$(git rev-parse HEAD)" --no-allow-unauthenticated --region="$REGION" --project="$PROJECT_ID"

Enable unauthenticated invocations in an organisation enforcing DRS using Resource Manager tags and a conditional DRS policy:

PROJECT_NUMBER=$(gcloud projects describe "$PROJECT_ID" --format="value(projectNumber)")
ORGANIZATION_ID=$(gcloud projects describe "$PROJECT_ID" --format="value(parent.id)")

gcloud resource-manager tags bindings create \
  --tag-value="$ORGANIZATION_ID/allUsersIngress/True" \
  --parent="//run.googleapis.com/projects/$PROJECT_NUMBER/locations/$REGION/services/$SERVICE" \
  --location=$REGION

# This can fail until the binding has propagated
gcloud run services add-iam-policy-binding "$SERVICE" \
  --member="allUsers" \
  --role="roles/run.invoker" \
  --region="$REGION" \
  --project="$PROJECT_ID"

Map the Cloud Run instance to a custom domain:

DOMAIN="go.loafoe.dev"

# It can take up to 30 minutes for Cloud Run to issue provision a certificate and route
gcloud beta run domain-mappings create --service="$SERVICE" --domain="$DOMAIN" --region="$REGION" --project="$PROJECT_ID"

Retrieve the necessary DNS record information for the domain mappings:

gcloud beta run domain-mappings describe --domain="$DOMAIN" --region="$REGION" --project="$PROJECT_ID"

Documentation

Index

Constants

View Source
const (
	DefaultSchemePattern     = "http"
	DefaultSchemeReplacement = "https"
	DefaultHostPattern       = "go.loafoe.dev"
	DefaultHostReplacement   = "github.com"
	DefaultPathPattern       = "/"
	DefaultPathReplacement   = "/loafoe-dev/go-"
)

Constants for default pattern and replacement values.

Variables

This section is empty.

Functions

func GetPackagePath

func GetPackagePath(r string) (string, error)

GetPackagePath extracts the host and path from the request URL, omitting the scheme. This is used for the go-import meta tag.

func GetRequestURL

func GetRequestURL(r *http.Request) string

GetRequestURL constructs the full request URL from an http.Request object.

func ModProxy

func ModProxy(cfg *Config, urlGetter RequestURLGetter, pathGetter PackagePathGetter, urlRewriter URLRewriter, w http.ResponseWriter, r *http.Request)

ModProxy is the main handler for the HTTP function. It rewrites the requested URL based on the provided configuration.

func NewModProxyHandler

func NewModProxyHandler(cfg *Config, urlGetter RequestURLGetter, pathGetter PackagePathGetter, urlRewriter URLRewriter) http.HandlerFunc

NewModProxyHandler creates a new HTTP handler for ModProxy with the provided configuration and dependencies.

func RewriteURL

func RewriteURL(originalURL string, cfg *Config) (string, error)

RewriteURL rewrites a given URL based on the provided patterns and replacements configuration.

Types

type Config

type Config struct {
	SchemePattern     string
	SchemeReplacement string
	HostPattern       string
	HostReplacement   string
	PathPattern       string
	PathReplacement   string
}

Config holds the configuration for ModProxy.

func NewConfigFromEnvironment

func NewConfigFromEnvironment() *Config

NewConfigFromEnvironment creates a new instance of Config with values from environment variables or default values.

type DefaultPackagePathGetter

type DefaultPackagePathGetter struct{}

func (DefaultPackagePathGetter) GetPackagePath

func (DefaultPackagePathGetter) GetPackagePath(url string) (string, error)

type DefaultRequestURLGetter

type DefaultRequestURLGetter struct{}

Concrete implementations

func (DefaultRequestURLGetter) GetRequestURL

func (DefaultRequestURLGetter) GetRequestURL(r *http.Request) string

type DefaultURLRewriter

type DefaultURLRewriter struct{}

func (DefaultURLRewriter) RewriteURL

func (DefaultURLRewriter) RewriteURL(originalURL string, cfg *Config) (string, error)

type PackagePathGetter

type PackagePathGetter interface {
	GetPackagePath(url string) (string, error)
}

type RequestURLGetter

type RequestURLGetter interface {
	GetRequestURL(r *http.Request) string
}

Interfaces

type URLManipulator

type URLManipulator struct {
	Config      *Config
	URLGetter   RequestURLGetter
	PathGetter  PackagePathGetter
	URLRewriter URLRewriter
}

URLManipulator contains the dependencies for the ModProxy function.

type URLRewriter

type URLRewriter interface {
	RewriteURL(originalURL string, cfg *Config) (string, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL