Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package middleware
- import (
- "github.com/valyala/fasthttp"
- )
- type RequesParams struct {
- ResponseFormat string
- Keyword string
- Location string
- Referrer string
- Direction string
- ScreenWidth uint32
- ScreenHeight uint32
- Campaign uint32
- MagazineStyle bool
- TestMode bool
- Pub1 string
- Pub2 string
- Pub3 string
- RedirectParams string
- CustomMacro map[string]string
- }
- func ParamLoader(next fasthttp.RequestHandler) fasthttp.RequestHandler {
- return func(ctx *fasthttp.RequestCtx) {
- params := &RequesParams{
- ResponseFormat: parseStringParam(ctx, "f", "iframe"),
- Keyword: parseStringParam(ctx, "k", ""),
- Location: parseStringParam(ctx, "loc", ""),
- Referrer: parseStringParam(ctx, "ref", ""),
- ScreenWidth: uint32(ctx.Request.URI().QueryArgs().GetUintOrZero("swidth")),
- ScreenHeight: uint32(ctx.Request.URI().QueryArgs().GetUintOrZero("sheight")),
- TestMode: parseBoolParam(ctx, "tm", false),
- // .......
- }
- ctx.SetUserValue("params", params)
- next(ctx)
- }
- }
- func parseStringParam(ctx *interface{}, name string, defValue string) string {
- val := ctx.Request.URI().QueryArgs().Peek(name)
- if val == nil {
- return defValue
- }
- return string(val)
- }
- func parseBoolParam(ctx *interface{}, name string, defVal bool) bool {
- val := ctx.Request.URI().QueryArgs().Peek(name)
- if val == "true" || val == "1" {
- return true
- }
- return false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement