Skip to content

Commit e9a2434

Browse files
committed
update workflows,makefile; fix lint
1 parent b6540e1 commit e9a2434

File tree

9 files changed

+71
-13
lines changed

9 files changed

+71
-13
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ jobs:
5454
uses: golangci/golangci-lint-action@v6
5555
with:
5656
version: latest
57+
args: --exclude-use-default=true --exclude='Error return value of .(w\.Write). is not checked'

.github/workflows/release.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: release
2+
3+
on:
4+
release:
5+
types: [ created ]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
release:
12+
strategy:
13+
matrix:
14+
platform: [ ubuntu-latest ]
15+
goos: [ linux, windows, darwin ]
16+
goarch: [ amd64, arm64 ]
17+
exclude:
18+
- goarch: arm64
19+
goos: windows
20+
name: Release
21+
runs-on: ${{ matrix.platform }}
22+
steps:
23+
- name: Show environment
24+
run: export
25+
- name: Checkout
26+
uses: actions/checkout@v4
27+
- name: go release matrix
28+
uses: wangyoucao577/go-release-action@v1.51
29+
with:
30+
github_token: ${{ secrets.GITHUB_TOKEN }}
31+
goversion: "go.mod"
32+
goos: ${{ matrix.goos }}
33+
goarch: ${{ matrix.goarch }}
34+
build_command: "make all"
35+
extra_files: README.md LICENSE

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
.idea
21
configurable-http-proxy
2+
test.log
3+
4+
.idea

Makefile

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1+
LDFLAGS =
2+
3+
# project info
14
PACKAGE := $(shell go list)
25
PACKAGES := $(shell go list ./...)
36
BINARY_NAME := $(shell basename $(PACKAGE))
47

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

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

1116
GREEN := $(shell tput -Txterm setaf 2)
1217
YELLOW := $(shell tput -Txterm setaf 3)
1318
WHITE := $(shell tput -Txterm setaf 7)
1419
CYAN := $(shell tput -Txterm setaf 6)
1520
RESET := $(shell tput -Txterm sgr0)
1621

17-
.PHONY: help tidy dep build clean
22+
.PHONY: help tidy dep vet test build clean
1823
default: help
24+
all: test build
1925

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

35+
vet: dep ## Run go vet
36+
@go vet ./...
37+
38+
test: dep ## Run
39+
@go test -v ./... -short 2>&1 | tee test.log
40+
@echo "Written logs in test.log"
41+
2942
build: dep ## Build the binary file
30-
@go build -o build/$(BINARY_NAME) $(LDFLAGS)
43+
@go build -o $(BINARY_NAME) -ldflags '$(LDFLAGS)'
3144

3245
clean: ## Remove previous build
33-
@rm -rf build
46+
@go clean ./...
47+
@rm -f test.log $(BINARY_NAME)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ go install github.com/potoo0/configurable-http-proxy
1414

1515
### Release
1616

17-
Download the latest release from the [releases page]()
17+
Download the latest release from the releases page.
1818

1919
## Features
2020

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func run(cmd *cobra.Command, args []string) {
102102
log.Info(fmt.Sprintf("Proxy API at %s://%s:%d/api/routes", schema(options.Ssl),
103103
defaultIfEmpty(listenerCfg.apiIp, "*"), listenerCfg.apiPort))
104104
if listenerCfg.metricsPort != 0 {
105-
log.Warn(fmt.Sprintf("Metrics server not implemented yet"))
105+
log.Warn("Metrics server not implemented yet")
106106
//log.Info(fmt.Sprintf("Serve metrics at %s://%s:%d/metrics", "http", listenerCfg.metricsIp, listenerCfg.metricsPort))
107107
}
108108

lib/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (server *ApiServer) addRoute(w http.ResponseWriter, r *http.Request) {
8282
}
8383
// check target
8484
var target any
85-
targetValid := true
85+
var targetValid bool
8686
if target, targetValid = data["target"]; targetValid {
8787
_, targetValid = target.(string)
8888
}

lib/proxy/proxy.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import (
1919

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

22+
type (
23+
ctxTargetKey struct{}
24+
ctxPrefixKey struct{}
25+
)
26+
2227
// Server field TargetForReq should be non-nil
2328
type Server struct {
2429
Secure bool // verify SSL certificate
@@ -66,7 +71,7 @@ func (s *Server) Handler() http.HandlerFunc {
6671

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

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

102107
func (s *Server) modifyResponse(r *http.Response) error {
103-
prefix := r.Request.Context().Value("prefix").(string)
108+
prefix := r.Request.Context().Value(ctxPrefixKey{}).(string)
104109
if r.StatusCode < 300 {
105110
s.updateLastActivity(prefix)
106111
} else {
@@ -159,8 +164,8 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request) {
159164
ctx, cancel = context.WithTimeout(ctx, time.Duration(s.ProxyTimeout)*time.Millisecond)
160165
defer cancel()
161166
}
162-
ctx = context.WithValue(ctx, "target", targetUrl)
163-
ctx = context.WithValue(ctx, "prefix", targetInfo.Prefix)
167+
ctx = context.WithValue(ctx, ctxTargetKey{}, targetUrl)
168+
ctx = context.WithValue(ctx, ctxPrefixKey{}, targetInfo.Prefix)
164169

165170
rn := r.WithContext(ctx)
166171
s.handleHttp(w, rn)

lib/proxy/proxy_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestProxy_rewrite(t *testing.T) {
2727
serverUrl := mustParse(t, serverUrlRaw)
2828
target := mustParse(t, "https://httpbin.org/get")
2929

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

106106
serverUrl := mustParse(t, reqUrlRaw)
107107
ctx := context.Background()
108+
ctx = context.WithValue(ctx, ctxPrefixKey{}, serverUrl.Path)
108109

109110
reqHeader := http.Header{}
110111
reqHeader.Set("Host", serverUrl.Host)

0 commit comments

Comments
 (0)