Skip to content

Commit

Permalink
update workflows,makefile; fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
potoo0 committed Jul 6, 2024
1 parent b6540e1 commit e9a2434
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ jobs:
uses: golangci/golangci-lint-action@v6
with:
version: latest
args: --exclude-use-default=true --exclude='Error return value of .(w\.Write). is not checked'
35 changes: 35 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: release

on:
release:
types: [ created ]

permissions:
contents: write

jobs:
release:
strategy:
matrix:
platform: [ ubuntu-latest ]
goos: [ linux, windows, darwin ]
goarch: [ amd64, arm64 ]
exclude:
- goarch: arm64
goos: windows
name: Release
runs-on: ${{ matrix.platform }}
steps:
- name: Show environment
run: export
- name: Checkout
uses: actions/checkout@v4
- name: go release matrix
uses: wangyoucao577/go-release-action@v1.51
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goversion: "go.mod"
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
build_command: "make all"
extra_files: README.md LICENSE
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
configurable-http-proxy
test.log

.idea
22 changes: 18 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
LDFLAGS =

# project info
PACKAGE := $(shell go list)
PACKAGES := $(shell go list ./...)
BINARY_NAME := $(shell basename $(PACKAGE))

# commit info
COMMIT_SHA := $(shell git rev-parse HEAD)
TAG := $(shell git describe --tags --abbrev=0)

# embed version info into binary
LDFLAGS = -ldflags "-X main.Tag=$(TAG) -X main.Build=$(COMMIT_SHA)"
LDFLAGS += -X main.Tag=$(TAG)
LDFLAGS += -X main.Build=$(COMMIT_SHA)

GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
CYAN := $(shell tput -Txterm setaf 6)
RESET := $(shell tput -Txterm sgr0)

.PHONY: help tidy dep build clean
.PHONY: help tidy dep vet test build clean
default: help
all: test build

help: ## Show this help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "${YELLOW}%-16s${GREEN}%s${RESET}\n", $$1, $$2}' $(MAKEFILE_LIST)
Expand All @@ -26,8 +32,16 @@ tidy: ## Tidy up the go modules
dep: tidy ## Install dependencies
@go mod download

vet: dep ## Run go vet
@go vet ./...

test: dep ## Run
@go test -v ./... -short 2>&1 | tee test.log
@echo "Written logs in test.log"

build: dep ## Build the binary file
@go build -o build/$(BINARY_NAME) $(LDFLAGS)
@go build -o $(BINARY_NAME) -ldflags '$(LDFLAGS)'

clean: ## Remove previous build
@rm -rf build
@go clean ./...
@rm -f test.log $(BINARY_NAME)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ go install github.com/potoo0/configurable-http-proxy

### Release

Download the latest release from the [releases page]()
Download the latest release from the releases page.

## Features

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func run(cmd *cobra.Command, args []string) {
log.Info(fmt.Sprintf("Proxy API at %s://%s:%d/api/routes", schema(options.Ssl),
defaultIfEmpty(listenerCfg.apiIp, "*"), listenerCfg.apiPort))
if listenerCfg.metricsPort != 0 {
log.Warn(fmt.Sprintf("Metrics server not implemented yet"))
log.Warn("Metrics server not implemented yet")
//log.Info(fmt.Sprintf("Serve metrics at %s://%s:%d/metrics", "http", listenerCfg.metricsIp, listenerCfg.metricsPort))
}

Expand Down
2 changes: 1 addition & 1 deletion lib/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (server *ApiServer) addRoute(w http.ResponseWriter, r *http.Request) {
}
// check target
var target any
targetValid := true
var targetValid bool
if target, targetValid = data["target"]; targetValid {
_, targetValid = target.(string)
}
Expand Down
13 changes: 9 additions & 4 deletions lib/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (

var redirectRegex = regexp.MustCompile(`^(201|30([1278]))$`)

type (
ctxTargetKey struct{}
ctxPrefixKey struct{}
)

// Server field TargetForReq should be non-nil
type Server struct {
Secure bool // verify SSL certificate
Expand Down Expand Up @@ -66,7 +71,7 @@ func (s *Server) Handler() http.HandlerFunc {

// rewrite request url, pr.Out and pr.In share the same context
func (s *Server) rewrite(pr *httputil.ProxyRequest) {
target := pr.In.Context().Value("target").(*url.URL)
target := pr.In.Context().Value(ctxTargetKey{}).(*url.URL)

// clean path if not prependPath before SetURL
if !s.PrependPath {
Expand Down Expand Up @@ -100,7 +105,7 @@ func (s *Server) rewrite(pr *httputil.ProxyRequest) {
}

func (s *Server) modifyResponse(r *http.Response) error {
prefix := r.Request.Context().Value("prefix").(string)
prefix := r.Request.Context().Value(ctxPrefixKey{}).(string)
if r.StatusCode < 300 {
s.updateLastActivity(prefix)
} else {
Expand Down Expand Up @@ -159,8 +164,8 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request) {
ctx, cancel = context.WithTimeout(ctx, time.Duration(s.ProxyTimeout)*time.Millisecond)
defer cancel()
}
ctx = context.WithValue(ctx, "target", targetUrl)
ctx = context.WithValue(ctx, "prefix", targetInfo.Prefix)
ctx = context.WithValue(ctx, ctxTargetKey{}, targetUrl)
ctx = context.WithValue(ctx, ctxPrefixKey{}, targetInfo.Prefix)

rn := r.WithContext(ctx)
s.handleHttp(w, rn)
Expand Down
3 changes: 2 additions & 1 deletion lib/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestProxy_rewrite(t *testing.T) {
serverUrl := mustParse(t, serverUrlRaw)
target := mustParse(t, "https://httpbin.org/get")

ctx := context.WithValue(context.Background(), "target", target)
ctx := context.WithValue(context.Background(), ctxTargetKey{}, target)
req, err := http.NewRequestWithContext(ctx, "GET", serverUrlRaw+prefix, nil)
if err != nil {
t.Fatalf("http.NewRequestWithContext error: %v", err)
Expand Down Expand Up @@ -105,6 +105,7 @@ func TestProxy_redirect(t *testing.T) {

serverUrl := mustParse(t, reqUrlRaw)
ctx := context.Background()
ctx = context.WithValue(ctx, ctxPrefixKey{}, serverUrl.Path)

reqHeader := http.Header{}
reqHeader.Set("Host", serverUrl.Host)
Expand Down

0 comments on commit e9a2434

Please sign in to comment.