Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.50 KB | None | 0 0
  1. package middleware
  2.  
  3. import (
  4.     "github.com/valyala/fasthttp"
  5. )
  6.  
  7. type RequesParams struct {
  8.     ResponseFormat string
  9.     Keyword        string
  10.     Location       string
  11.     Referrer       string
  12.     Direction      string
  13.     ScreenWidth    uint32
  14.     ScreenHeight   uint32
  15.     Campaign       uint32
  16.     MagazineStyle  bool
  17.     TestMode       bool
  18.     Pub1           string
  19.     Pub2           string
  20.     Pub3           string
  21.     RedirectParams string
  22.     CustomMacro    map[string]string
  23. }
  24.  
  25. func ParamLoader(next fasthttp.RequestHandler) fasthttp.RequestHandler {
  26.     return func(ctx *fasthttp.RequestCtx) {
  27.         params := &RequesParams{
  28.             ResponseFormat: parseStringParam(ctx, "f", "iframe"),
  29.             Keyword:        parseStringParam(ctx, "k", ""),
  30.             Location:       parseStringParam(ctx, "loc", ""),
  31.             Referrer:       parseStringParam(ctx, "ref", ""),
  32.             ScreenWidth:    uint32(ctx.Request.URI().QueryArgs().GetUintOrZero("swidth")),
  33.             ScreenHeight:   uint32(ctx.Request.URI().QueryArgs().GetUintOrZero("sheight")),
  34.             TestMode:       parseBoolParam(ctx, "tm", false),
  35.             // .......
  36.         }
  37.  
  38.         ctx.SetUserValue("params", params)
  39.  
  40.         next(ctx)
  41.  
  42.     }
  43. }
  44.  
  45. func parseStringParam(ctx *interface{}, name string, defValue string) string {
  46.     val := ctx.Request.URI().QueryArgs().Peek(name)
  47.     if val == nil {
  48.         return defValue
  49.     }
  50.  
  51.     return string(val)
  52. }
  53.  
  54. func parseBoolParam(ctx *interface{}, name string, defVal bool) bool {
  55.     val := ctx.Request.URI().QueryArgs().Peek(name)
  56.     if val == "true" || val == "1" {
  57.         return true
  58.     }
  59.  
  60.     return false
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement