修改配置文件形式,直接使用配置的值进行获取
This commit is contained in:
parent
32b50a90c1
commit
5e6f9aa94e
|
|
@ -1,6 +1,5 @@
|
||||||
local jwt = require "resty.jwt"
|
local jwt = require "resty.jwt"
|
||||||
local cjson = require("cjson.safe")
|
local cjson = require("cjson.safe")
|
||||||
local conf = require("config")
|
|
||||||
local jsonschema = require("jsonschema")
|
local jsonschema = require("jsonschema")
|
||||||
|
|
||||||
-- 定义一个JSON Schema
|
-- 定义一个JSON Schema
|
||||||
|
|
@ -34,7 +33,7 @@ end
|
||||||
--获取token的数据值
|
--获取token的数据值
|
||||||
local token = string.sub(auth_header,8)
|
local token = string.sub(auth_header,8)
|
||||||
--校验令牌
|
--校验令牌
|
||||||
local jwt_obj = jwt:verify(conf.secret_key, token)
|
local jwt_obj = jwt:verify(SYSTEM_CONFIG.secret_key, token)
|
||||||
--如果校验结果中的verified==false,则表示令牌无效
|
--如果校验结果中的verified==false,则表示令牌无效
|
||||||
if jwt_obj.verified == false then
|
if jwt_obj.verified == false then
|
||||||
ngx.log(ngx.WARN, "Invalid token: ".. jwt_obj.reason)
|
ngx.log(ngx.WARN, "Invalid token: ".. jwt_obj.reason)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
--- DateTime: 2025/9/24 16:31
|
--- DateTime: 2025/9/24 16:31
|
||||||
---
|
---
|
||||||
|
|
||||||
return {
|
SYSTEM_CONFIG = {
|
||||||
APP_ENV = "dev", -- dev/prod
|
APP_ENV = "dev", -- dev/prod
|
||||||
|
|
||||||
locale = 'zh',
|
locale = 'zh',
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ end
|
||||||
|
|
||||||
--初始化,获取系统默认的用户权限,为实现RBAC框架做权限数据准备
|
--初始化,获取系统默认的用户权限,为实现RBAC框架做权限数据准备
|
||||||
local function handler()
|
local function handler()
|
||||||
local conf = require("config")
|
|
||||||
--引用使用的库文件
|
--引用使用的库文件
|
||||||
local Model = require("share.model")
|
local Model = require("share.model")
|
||||||
--创建一个数据表相关的模型
|
--创建一个数据表相关的模型
|
||||||
|
|
@ -41,14 +40,14 @@ local function handler()
|
||||||
red:set_timeout(conf.REDIS.TIMEOUT) -- 1秒
|
red:set_timeout(conf.REDIS.TIMEOUT) -- 1秒
|
||||||
|
|
||||||
-- 连接到 Redis
|
-- 连接到 Redis
|
||||||
local ok, err = red:connect(conf.REDIS.HOST, conf.REDIS.PORT)
|
local ok, err = red:connect(SYSTEM_CONFIG.REDIS.HOST, SYSTEM_CONFIG.REDIS.PORT)
|
||||||
if not ok then
|
if not ok then
|
||||||
ngx.log(ngx.ERR, "redis failed to connect: "..err)
|
ngx.log(ngx.ERR, "redis failed to connect: "..err)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
--需要密码时对密码进行处理
|
--需要密码时对密码进行处理
|
||||||
if conf.REDIS.PASSWORD ~= nil then
|
if SYSTEM_CONFIG.REDIS.PASSWORD ~= nil then
|
||||||
local res, err = red:auth(conf.REDIS.PASSWORD)
|
local res, err = red:auth(SYSTEM_CONFIG.REDIS.PASSWORD)
|
||||||
if not res then
|
if not res then
|
||||||
ngx.log(ngx.ERR, "redis failed to connect, password error: "..err)
|
ngx.log(ngx.ERR, "redis failed to connect, password error: "..err)
|
||||||
return
|
return
|
||||||
|
|
@ -56,7 +55,7 @@ local function handler()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 从连接池中获取连接
|
-- 从连接池中获取连接
|
||||||
red:set_keepalive(conf.REDIS.POOL_MAX_IDLE_TIME, conf.REDIS.POOL_SIZE)
|
red:set_keepalive(SYSTEM_CONFIG.REDIS.POOL_MAX_IDLE_TIME, SYSTEM_CONFIG.REDIS.POOL_SIZE)
|
||||||
|
|
||||||
-- 设置 key-value
|
-- 设置 key-value
|
||||||
local ok, err = red:set("admin-system:user:add", "1")
|
local ok, err = red:set("admin-system:user:add", "1")
|
||||||
|
|
|
||||||
|
|
@ -127,8 +127,7 @@ local function get_cookie(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_local_time()
|
local function get_local_time()
|
||||||
local config = require("config")
|
local time_zone = ngx.re.match(SYSTEM_CONFIG.time_zone, "[0-9]+")
|
||||||
local time_zone = ngx.re.match(config.time_zone, "[0-9]+")
|
|
||||||
if time_zone == nil then
|
if time_zone == nil then
|
||||||
local err = "not set time zone or format error, time zone should look like `+8:00` current is: " .. config.time_zone
|
local err = "not set time zone or format error, time zone should look like `+8:00` current is: " .. config.time_zone
|
||||||
ngx.log(ngx.ERR, err)
|
ngx.log(ngx.ERR, err)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
local Database = require('share.database')
|
local Database = require('share.database')
|
||||||
local conf = require("config")
|
|
||||||
|
|
||||||
local helpers = require('share.helpers')
|
local helpers = require('share.helpers')
|
||||||
local implode = helpers.implode
|
local implode = helpers.implode
|
||||||
local unique = helpers.unique
|
local unique = helpers.unique
|
||||||
|
|
@ -12,28 +10,28 @@ local WRITE = 'WRITE'
|
||||||
local READ = 'READ'
|
local READ = 'READ'
|
||||||
|
|
||||||
local database_write = Database:new({
|
local database_write = Database:new({
|
||||||
host = conf.POSTGRES.HOST,
|
host = SYSTEM_CONFIG.POSTGRES.HOST,
|
||||||
port = conf.POSTGRES.PORT,
|
port = SYSTEM_CONFIG.POSTGRES.PORT,
|
||||||
user = conf.POSTGRES.USERNAME,
|
user = SYSTEM_CONFIG.POSTGRES.USERNAME,
|
||||||
password = conf.POSTGRES.PASSWORD,
|
password = SYSTEM_CONFIG.POSTGRES.PASSWORD,
|
||||||
database = conf.POSTGRES.DATABASE,
|
database = SYSTEM_CONFIG.POSTGRES.DATABASE,
|
||||||
charset = conf.POSTGRES.CHARSET,
|
charset = SYSTEM_CONFIG.POSTGRES.CHARSET,
|
||||||
timeout = conf.POSTGRES.TIMEOUT,
|
timeout = SYSTEM_CONFIG.POSTGRES.TIMEOUT,
|
||||||
db_pool_timeout = conf.POSTGRES.POOL_TIMEOUT,
|
db_pool_timeout = SYSTEM_CONFIG.POSTGRES.POOL_TIMEOUT,
|
||||||
db_pool_size = conf.POSTGRES.POOL_SIZE,
|
db_pool_size = SYSTEM_CONFIG.POSTGRES.POOL_SIZE,
|
||||||
db_type = WRITE
|
db_type = WRITE
|
||||||
})
|
})
|
||||||
|
|
||||||
local database_read = Database:new({
|
local database_read = Database:new({
|
||||||
host = conf.POSTGRES.HOST,
|
host = SYSTEM_CONFIG.POSTGRES.HOST,
|
||||||
port = conf.POSTGRES.PORT,
|
port = SYSTEM_CONFIG.POSTGRES.PORT,
|
||||||
user = conf.POSTGRES.USERNAME,
|
user = SYSTEM_CONFIG.POSTGRES.USERNAME,
|
||||||
password = conf.POSTGRES.PASSWORD,
|
password = SYSTEM_CONFIG.POSTGRES.PASSWORD,
|
||||||
database = conf.POSTGRES.DATABASE,
|
database = SYSTEM_CONFIG.POSTGRES.DATABASE,
|
||||||
charset = conf.POSTGRES.CHARSET,
|
charset = SYSTEM_CONFIG.POSTGRES.CHARSET,
|
||||||
timeout = conf.POSTGRES.TIMEOUT,
|
timeout = SYSTEM_CONFIG.POSTGRES.TIMEOUT,
|
||||||
db_pool_timeout = conf.POSTGRES.POOL_TIMEOUT,
|
db_pool_timeout = SYSTEM_CONFIG.POSTGRES.POOL_TIMEOUT,
|
||||||
db_pool_size = conf.POSTGRES.POOL_SIZE,
|
db_pool_size = SYSTEM_CONFIG.POSTGRES.POOL_SIZE,
|
||||||
db_type = READ
|
db_type = READ
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
local redis = require("resty.redis")
|
local redis = require("resty.redis")
|
||||||
local conf = require('config')
|
|
||||||
|
|
||||||
local _M = setmetatable({}, {__index = function(self, key)
|
local _M = setmetatable({}, {__index = function(self, key)
|
||||||
local red = redis:new()
|
local red = redis:new()
|
||||||
local ok, err = red:connect(conf.REDIS.HOST, conf.REDIS.PORT)
|
local ok, err = red:connect(SYSTEM_CONFIG.REDIS.HOST, SYSTEM_CONFIG.REDIS.PORT)
|
||||||
if not ok then
|
if not ok then
|
||||||
ngx.log(ngx.ERR, err)
|
ngx.log(ngx.ERR, err)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
---
|
---
|
||||||
|
|
||||||
local jwt = require("resty.jwt")
|
local jwt = require("resty.jwt")
|
||||||
local conf = require("config")
|
|
||||||
local jsonschema = require("jsonschema")
|
local jsonschema = require("jsonschema")
|
||||||
|
|
||||||
local _M = {}
|
local _M = {}
|
||||||
|
|
@ -39,7 +38,7 @@ function _M.generateToken(userid, username)
|
||||||
obj.payload.userid = userid
|
obj.payload.userid = userid
|
||||||
obj.payload.username = username
|
obj.payload.username = username
|
||||||
--获取的登陆的用户信息,返回tocken
|
--获取的登陆的用户信息,返回tocken
|
||||||
local jwt_token = jwt:sign(conf.secret_key, obj)
|
local jwt_token = jwt:sign(SYSTEM_CONFIG.secret_key, obj)
|
||||||
return "Bearer "..jwt_token
|
return "Bearer "..jwt_token
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -68,7 +67,7 @@ function _M.authorizationToken(auth_header)
|
||||||
--查找令牌中的Bearer前缀字符,并进行截取
|
--查找令牌中的Bearer前缀字符,并进行截取
|
||||||
local token = string.sub(auth_header,8)
|
local token = string.sub(auth_header,8)
|
||||||
--校验令牌
|
--校验令牌
|
||||||
local jwt_obj = jwt:verify(conf.secret_key, token)
|
local jwt_obj = jwt:verify(SYSTEM_CONFIG.secret_key, token)
|
||||||
--如果校验结果中的verified==false,则表示令牌无效
|
--如果校验结果中的verified==false,则表示令牌无效
|
||||||
if jwt_obj.verified == false then
|
if jwt_obj.verified == false then
|
||||||
response["code"] = 401
|
response["code"] = 401
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user