SHARE
TWEET

Untitled

a guest Jul 20th, 2019 434 in 148 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2. import (
  3.         "github.com/VictoriaMetrics/fastcache"
  4.         "github.com/valyala/fasthttp"
  5.         "log"
  6. )
  7.  
  8.  
  9. var (
  10.         //pool *redis.Pool
  11.         Fcache = fastcache.New(12)
  12.         //      FcacheTTL = fastcache.New(128)
  13. )
  14.  
  15.  
  16.  
  17. var proxyClient = &fasthttp.HostClient{
  18.         Addr: "127.0.0.1:808",
  19.         // set other options here if required - most notably timeouts.
  20. }
  21.  
  22. func ReverseProxyHandler(ctx *fasthttp.RequestCtx) {
  23.         req := &ctx.Request
  24.         resp := &ctx.Response
  25.         prepareRequest(req)
  26.         httpHeader := Fcache.Get(nil, req.RequestURI())
  27.  
  28.         if err := proxyClient.Do(req, resp); err != nil {
  29.                 ctx.Logger().Printf("error when proxying the request: %s", err)
  30.         }
  31.  
  32.         if len(httpHeader) > 0 {
  33.                 //USE THE HEADER FROM fastcache
  34.                 //resp.Header.Header() = httpHeader ???
  35.                 //resp.SetHEADER(httpHeader)
  36.         }
  37.         postprocessResponse(req,resp)
  38. }
  39.  
  40. func prepareRequest(req *fasthttp.Request) {
  41.         // do not proxy "Connection" header.
  42.         req.Header.Del("Connection")
  43.         // strip other unneeded headers.
  44.  
  45.         // alter other request params before sending them to upstream host
  46. }
  47.  
  48. func postprocessResponse(req *fasthttp.Request, resp *fasthttp.Response) {
  49.         // do not proxy "Connection" header
  50.         resp.Header.Del("Connection")
  51.  
  52.         //      statusCode := resp.StatusCode()
  53.         //      httpBody := resp.Body()
  54.         httpHeader := resp.Header.Header()
  55.         Fcache.Set(req.RequestURI(),httpHeader)
  56.  
  57. }
  58.  
  59. func main() {
  60.         if err := fasthttp.ListenAndServe(":8080", ReverseProxyHandler); err != nil {
  61.                 log.Fatalf("error in fasthttp server: %s", err)
  62.         }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top