From 7529cb9114315965dc83e4e2fd095bdb3ddaeced Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Wed, 15 Oct 2025 17:22:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=86=85=E5=AE=B9=EF=BC=8C=E5=B9=B6=E5=AF=B9=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=95=B0=E6=8D=AE=E8=BF=9B=E8=A1=8C=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/api.lua | 84 +++++++++++++------------------------ src/api/system/user.lua | 5 +++ src/service/system/user.lua | 2 +- 3 files changed, 34 insertions(+), 57 deletions(-) diff --git a/src/api/api.lua b/src/api/api.lua index d39d955..1749044 100644 --- a/src/api/api.lua +++ b/src/api/api.lua @@ -4,51 +4,23 @@ --- DateTime: 2025/9/24 15:29 --- local radix = require("resty.radixtree") - -local function say_hello(req) - ngx.say("Hello, World!") -end - -local function get_user(req) - local user_id = req.args.id or "unknown" - ngx.say("User ID: " .. user_id) -end - -local function get_id(req) - local args = req.get_uri_args() - -- 获取单个参数 - local id = args["id"] -- 值为 "john" - ngx.say("User ID: " .. user_id) -end - -local function test(req) - local request_method = ngx.var.request_method - local args = nil - ngx.say(request_method) - - --1、获取参数的值 获取前端提交参数 - if "GET" == request_method then - args = ngx.req.get_uri_args() - elseif "POST" == request_method then - ngx.req.read_body() - args = ngx.req.get_post_args() - end -end +local userApi = require("api.system.user") local routes = { { - paths = { "/login/*action" }, - metadata = { "metadata /login/action" }, - methods = { "GET", "POST", "PUT" }, - remote_addrs = { "127.0.0.1", "192.168.0.0/16", "::1", "fe80::/32" } + paths = { "/api/user" }, + metadata = { "metadata /user" }, + methods = { "GET", "POST" }, + handler = userApi.get_allusers, }, { - paths = { "/user/:name" }, - metadata = { "metadata /user/name" }, - methods = { "GET" }, + paths = { "/api/user/:id" }, + metadata = { "metadata /user/id" }, + methods = { "GET", "PUT", "DELETE" }, + handler = userApi.get_users, }, { - paths = { "/admin/:name", "/superuser/:name" }, + paths = { "/api/admin/:name", "/api/superuser/:name" }, metadata = { "metadata /admin/name" }, methods = { "GET", "POST", "PUT" }, filter_fun = function(vars, opts) @@ -57,23 +29,23 @@ local routes = { } } -local function handle_request() - --获取接口请求的方法 - local request_method = ngx.var.request_method - ngx.say(request_method) - - local args = ngx.var.get_uri_args - ngx.say(request_method) - - local uri = ngx.var.request_uri - ngx.say("url: " .. uri) - local handler = routes[uri] - if handler then - handler(ngx.req) - else - ngx.status = 404 - ngx.say("Not Found") - end +local rx, err = radix.new(routes) +if not rx then + ngx.say("-----") + ngx.status = 404 + ngx.say("Not Found") + --ngx.exit() end -handle_request() \ No newline at end of file +local uri = ngx.var.uri +local opts = { + method = ngx.var.request_method, + matched = {} +} + +local ok = rx:dispatch(uri, opts, opts.matched) +if not ok then + ngx.status = 404 + ngx.say("Not Found") + --ngx.exit() +end \ No newline at end of file diff --git a/src/api/system/user.lua b/src/api/system/user.lua index c36e851..25e396a 100644 --- a/src/api/system/user.lua +++ b/src/api/system/user.lua @@ -11,4 +11,9 @@ function _M.get_allusers() end +--获取所有用户信息 +function _M.get_users(req) + +end + return _M \ No newline at end of file diff --git a/src/service/system/user.lua b/src/service/system/user.lua index 8c3db65..8e88541 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -21,7 +21,7 @@ conn:connect(function(err) end) function _M.getAllUser() - + end return _M \ No newline at end of file