sjs_caomei 2021-12-14 22:52 采纳率: 50%
浏览 10
已结题

kong 自定义插件 api.lua写的暴露接口无效

kong版本是2.6.0,简单开发测试了一个插件,代码如下:

api.lua


local kong = kong

return {
    ["/pluginCeck"] = {
        GET = function()
            return kong.response.exit(200, { message = "test" })
        end
    },
}

handler.lua

-- hello-world.handlar.lua
local BasePlugin = require "kong.plugins.base_plugin"

local CustomHandler = BasePlugin:extend()

local resultAns = ">>插件开始运行了\n"

CustomHandler.VERSION = "1.0.0"
CustomHandler.PRIORITY = 10

function CustomHandler:new()
    CustomHandler.super.new(self, "hello-world")
end
function CustomHandler:access(config)
    CustomHandler.super.access(self)

    resultAns = resultAns .. ">>>>>>>执行:access阶段开始\n输出嵌入的内容(请求在还未到达上游服务器):\n"
    resultAns = resultAns .. "kong.version:\t" .. kong.version .. "\n"
    resultAns = resultAns .. "kong.client.get_ip():\t" .. kong.client.get_ip() .. "\n"
    resultAns = resultAns .. "kong.request.get_scheme():\t" .. kong.request.get_scheme() .. "\n"
    resultAns = resultAns .. "kong.request.get_host():\t" .. kong.request.get_host() .. "\n"
    resultAns = resultAns .. "kong.request.get_port()\t:" .. kong.request.get_port() .. "\n"
    resultAns = resultAns .. "kong.request.get_http_version():\t" .. kong.request.get_http_version() .. "\n"
    resultAns = resultAns .. "kong.request.get_method():\t" .. kong.request.get_method() .. "\n"
    resultAns = resultAns .. "kong.request.get_path():\t" .. kong.request.get_path() .. "\n"
    resultAns = resultAns .. "<<<<<<<执行access阶段结束 \n"

    return kong.response.exit(
            200,
            resultAns,
            {
                ["Content-Type"] = "application/json",
                ["WWW-Authenticate"] = "Basic"
            }
    )
end

return CustomHandler



schema.lua

--  hello-world.schema.lua
local typedefs = require "kong.db.schema.typedefs"
return {
    name = "hello-world",
    fields = {
        {
            consumer = typedefs.no_consumer
        },
        {
            config = {
                type = "record",
                fields = {
                    -- 这里的username, 会显示在插件配置页
                    {
                        username = {
                            type = "array",
                            elements = {type = "string"},
                            default = {}
                        }
                    }
                }
            }
        }
    }
}


并且能在konga配置生效,但是在本地 curl 404

img

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月22日
    • 创建了问题 12月14日

    悬赏问题

    • ¥50 三种调度算法报错 有实例
    • ¥15 关于#python#的问题,请各位专家解答!
    • ¥200 询问:python实现大地主题正反算的程序设计,有偿
    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥20 java在应用程序里获取不到扬声器设备