-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Optimize Dockerfile, refactor copy function, and implement grac…
…eful shutdown
- Loading branch information
Showing
10 changed files
with
287 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
FROM golang:1.20-alpine AS builder | ||
RUN apk --no-cache add tzdata | ||
RUN apk add upx | ||
FROM --platform=$BUILDPLATFORM golang:1.23.1-alpine AS builder | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
ENV GO111MODULE=on \ | ||
CGO_ENABLED=0 | ||
WORKDIR /build | ||
RUN apk --no-cache add tzdata | ||
COPY . . | ||
RUN go mod tidy && go build -ldflags "-s -w" -o main && upx -9 main | ||
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o main | ||
|
||
|
||
FROM scratch | ||
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo | ||
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /etc/localtime | ||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
COPY --from=builder /build/main / | ||
|
||
ENTRYPOINT ["/main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module github.com/go-bai/http-proxy | ||
|
||
go 1.20 | ||
go 1.23.1 | ||
|
||
require github.com/google/uuid v1.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pkg | ||
|
||
import ( | ||
"encoding/base64" | ||
"strings" | ||
) | ||
|
||
func BasicProxyAuth(proxyAuth string) (username, password string, ok bool) { | ||
if proxyAuth == "" { | ||
return | ||
} | ||
|
||
if !strings.HasPrefix(proxyAuth, "Basic ") { | ||
return | ||
} | ||
c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(proxyAuth, "Basic ")) | ||
if err != nil { | ||
return | ||
} | ||
cs := string(c) | ||
s := strings.IndexByte(cs, ':') | ||
if s < 0 { | ||
return | ||
} | ||
|
||
return cs[:s], cs[s+1:], true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pkg | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func HandleHTTP(w http.ResponseWriter, req *http.Request) { | ||
resp, err := http.DefaultTransport.RoundTrip(req) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusServiceUnavailable) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
copyHeader(w.Header(), resp.Header) | ||
w.WriteHeader(resp.StatusCode) | ||
// ignore errors | ||
io.Copy(w, resp.Body) | ||
} | ||
|
||
func copyHeader(dst, src http.Header) { | ||
for k, vv := range src { | ||
for _, v := range vv { | ||
dst.Add(k, v) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package pkg | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"sync/atomic" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func ListenAndServeWithGracefulShutdown(server *http.Server) { | ||
shutdownCtx, shutdownCancelFunc = context.WithCancel(context.Background()) | ||
stop := make(chan os.Signal, 1) | ||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
log.Fatalf("listen: %s\n", err) | ||
} | ||
}() | ||
|
||
<-stop | ||
log.Println("shutting down server...") | ||
shutdownCancelFunc() | ||
|
||
// create a deadline for graceful shutdown | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
// attempt graceful shutdown | ||
if err := server.Shutdown(ctx); err != nil { | ||
log.Printf("error during server shutdown: %s", err.Error()) | ||
} | ||
|
||
// wait for hijacked connections to finish | ||
for atomic.LoadInt64(&ActiveConnections) > 0 { | ||
log.Printf("waiting for %d hijacked connections to finish", atomic.LoadInt64(&ActiveConnections)) | ||
time.Sleep(200 * time.Millisecond) | ||
} | ||
|
||
// wait for the server goroutine to finish | ||
wg.Wait() | ||
|
||
log.Println("server gracefully stopped") | ||
} |
Oops, something went wrong.