Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base Plugin Deprecated #12

Open
bpiv400 opened this issue Oct 26, 2022 · 1 comment · May be fixed by #14
Open

Base Plugin Deprecated #12

bpiv400 opened this issue Oct 26, 2022 · 1 comment · May be fixed by #14

Comments

@bpiv400
Copy link

bpiv400 commented Oct 26, 2022

https://docs.konghq.com/gateway/latest/plugin-development/custom-logic/

I'll submit a PR if I can get this working. Right now, I've just copied handler.lua and shcema.lua into the relevant subdir and made the changes recommended above. I'm hoping that works

@kfirfer
Copy link

kfirfer commented Aug 24, 2023

this is the handler.lua:

        local plugin = {
          VERSION  = "1.0.0",
          PRIORITY = 800,
        }

        local function escape_hyphen(conf)
            local path_prefix = conf.path_prefix
            local should_escape = conf.escape

            if should_escape then
                return string.gsub(path_prefix, "%-", "%%%1")
            end

            return path_prefix
        end

        local function add_header(conf, path)
            local forwarded_header = conf.forwarded_header
            if forwarded_header then
                kong.log("Adding Header: X-Forwarded-Prefix ", conf.path_prefix)
                ngx.var.upstream_x_forwarded_prefix = conf.path_prefix
            end
        end

        function plugin:access(plugin_conf)

            local service_path = ngx.ctx.service.path or ""
            local full_path = kong.request.get_path()
            local replace_match = escape_hyphen(plugin_conf)
            local path_without_prefix = full_path:gsub(replace_match, "", 1)

            if path_without_prefix == "" and service_path == "" then
                path_without_prefix = "/"
            end

            local new_path = path_without_prefix
            kong.log("rewriting ", full_path, " to ", path_without_prefix)
            if service_path ~= "" then
                kong.log("Prefixing request with service path ", service_path)
                new_path = service_path .. new_path
            end
            add_header(plugin_conf, path_without_prefix)
            kong.service.request.set_path(new_path)
        end

        return plugin

@HairyMike HairyMike linked a pull request Oct 18, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants