Last active
August 13, 2025 21:48
-
-
Save egeneralov/0f5cd2ebe6543bcbf5c09ec50b3c8da8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module gist.github.com/egeneralov/0f5cd2ebe6543bcbf5c09ec50b3c8da8 | |
| go 1.24.4 | |
| require github.com/valyala/fasthttp v1.64.0 | |
| require ( | |
| github.com/andybalholm/brotli v1.2.0 // indirect | |
| github.com/klauspost/compress v1.18.0 // indirect | |
| github.com/valyala/bytebufferpool v1.0.0 // indirect | |
| ) |
This file contains hidden or 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
| github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ= | |
| github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= | |
| github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= | |
| github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= | |
| github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | |
| github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | |
| github.com/valyala/fasthttp v1.64.0 h1:QBygLLQmiAyiXuRhthf0tuRkqAFcrC42dckN2S+N3og= | |
| github.com/valyala/fasthttp v1.64.0/go.mod h1:dGmFxwkWXSK0NbOSJuF7AMVzU+lkHz0wQVvVITv2UQA= | |
| github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= | |
| github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= |
This file contains hidden or 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
| package main | |
| import ( | |
| "log" | |
| "fmt" | |
| "encoding/json" | |
| "github.com/valyala/fasthttp" | |
| ) | |
| var ( | |
| proxyClient = &fasthttp.HostClient{ | |
| Addr: "mirror.accum.se:80", | |
| } | |
| storage = make(map[string]*fasthttp.Response) | |
| ) | |
| func ReverseProxyHandler(ctx *fasthttp.RequestCtx) { | |
| req := &ctx.Request | |
| resp := &ctx.Response | |
| path := string(ctx.Request.URI().Path()) | |
| if path == "/stats" { | |
| storageDump := make(map[string]int) | |
| for k, v := range storage { | |
| storageDump[k] = len(v.Body()) | |
| } | |
| j, je := json.Marshal(storageDump) | |
| if je == nil { | |
| fmt.Fprintf(ctx, string(j)) | |
| } | |
| return | |
| } | |
| _, exist := storage[path] | |
| if !exist { | |
| ctx.Logger().Printf("request") | |
| if err := proxyClient.Do(req, resp); err != nil { | |
| ctx.Logger().Printf("error when proxying the request: %s", err) | |
| } | |
| storage[path] = &fasthttp.Response{} | |
| resp.CopyTo(storage[path]) | |
| } | |
| storage[path].CopyTo(resp) | |
| } | |
| func main() { | |
| if err := fasthttp.ListenAndServe(":8080", ReverseProxyHandler); err != nil { | |
| log.Fatalf("error in fasthttp server: %s", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment