Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
fengfei committed Mar 30, 2018
1 parent 36101fc commit bf291ec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
1 change: 1 addition & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ http {
keepalive_timeout 65;
client_max_body_size 1024m;
lua_shared_dict cache 10m;
lua_shared_dict session 1m;

#换成你的实际路径,这里将源码中src目录加入到 lua_package_path
lua_package_path '/usr/local/openresty/nginx/proxygateway/src/?.lua;;';
Expand Down
14 changes: 9 additions & 5 deletions src/manage/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ function _M.checkLogin()
end
local uri = ngx.var.uri
if uri ~= "/api/v1/login" and uri ~= "/login.html" then
local cache = ngx.shared.cache
local token = cache:get("login-token")
local session = ngx.shared.session
local ck = require "resty.cookie"
local cookie, err = ck:new()
local cookie, _ = ck:new()
local token = cookie:get("token")
local user;
if token ~= nil then
user = session:get(token)
end
local isApi = string.find(uri, "/api/v1/");
if isApi and (token == nil or token ~= cookie:get("token")) then
if isApi and user == nil then
local response = {}
response["status"] = 401
response["errno"] = 40100
Expand All @@ -24,7 +28,7 @@ function _M.checkLogin()
ngx.exit(401)
return;
end
if token == nil or token ~= cookie:get("token") then
if user == nil then
return ngx.redirect("/login.html")
end
end
Expand Down
64 changes: 34 additions & 30 deletions src/manage/controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,60 @@ function check_require_params(params, names)
for k, name in pairs(names) do
if params[name] == nil then
ngx.header.content_type = 'application/json;charset=UTF-8';
ngx.say(cjson.encode({errno = 101, status = 400, msg = "missing param:"..name}))
ngx.say(cjson.encode({ errno = 101, status = 400, msg = "missing param:" .. name }))
ngx.status = 400
ngx.exit(400)
end
end
end


function _M.run()
app:route("/api/v1/login", function(params)
check_require_params(params, {"username","password"})
check_require_params(params, { "username", "password" })
if params["username"] ~= config["admin_name"] then
return nil,"error username",102
return nil, "error username", 102
end
if params["password"] ~= config["admin_pass"] then
return nil,"error password",102
return nil, "error password", 102
end
local headers = ngx.req.get_headers()
local resty_md5 = require "resty.md5"
local str = require "resty.string"
local md5 = resty_md5:new()
local ok = md5:update(config["admin_name"]..config["admin_pass"]..headers["user-agent"]..os.time())
md5:update(config["admin_name"] .. config["admin_pass"] .. headers["user-agent"] .. os.time())
local digest = md5:final()
local token = str.to_hex(digest)
local ck = require "resty.cookie"
local cookie, err = ck:new()
cookie:set({key = "token", value = token, path = "/"})
local cache = ngx.shared.cache
cache:set("login-token", token)
return {token = token}
cookie:set({ key = "token", value = token, path = "/" })
local session = ngx.shared.session
session:set(token, "1", 86400 * 30) -- 一个月过期
return { token = token }
end)

app:route("/api/v1/logout", function(params)
local cache = ngx.shared.cache
cache:set("login-token", nil)
local ck = require "resty.cookie"
local cookie, _ = ck:new()
local token = cookie:get("token")
local session = ngx.shared.session
if token ~= nil and session:get(token) ~= nil then
session:delete(token)
end
return ngx.redirect("/login.html")
end)

app:route("/api/v1/domain/add", function(params)
check_require_params(params, {"name"})
check_require_params(params, { "name" })
return domain_model.addDomain(params["name"])
end)

app:route("/api/v1/domain/delete", function(params)
check_require_params(params, {"domain_id"})
check_require_params(params, { "domain_id" })
return agw_service.deleteDomain(params["domain_id"])
end)

app:route("/api/v1/domain/update", function(params)
check_require_params(params, {"domain_id","name"})
check_require_params(params, { "domain_id", "name" })
return domain_model.update(params["domain_id"], params["name"])
end)

Expand All @@ -71,77 +75,77 @@ function _M.run()
end)

app:route("/api/v1/service/delete", function(params)
check_require_params(params, {"service_id"})
check_require_params(params, { "service_id" })
return agw_service.deleteService(params["service_id"])
end)

app:route("/api/v1/service/list", function(params)
check_require_params(params, {"domain_id"})
check_require_params(params, { "domain_id" })
return agw_service.getServices(params["domain_id"])
end)

app:route("/api/v1/service/get", function(params)
check_require_params(params, {"service_id"})
check_require_params(params, { "service_id" })
return service_model.getService(params["service_id"])
end)

app:route("/api/v1/service/add", function(params)
check_require_params(params, {"domain_id", "name","description"})
check_require_params(params, { "domain_id", "name", "description" })
return service_model.add(params["domain_id"], params["name"], params["host"], params["description"])
end)

app:route("/api/v1/service/edit", function(params)
check_require_params(params, {"service_id", "name", "host", "description"})
check_require_params(params, { "service_id", "name", "host", "description" })
return service_model.update(params["service_id"], params["name"], params["host"], params["description"])
end)

app:route("/api/v1/server/list", function(params)
check_require_params(params, {"service_id"})
check_require_params(params, { "service_id" })
return server_model.getServiceServers(params["service_id"])
end)

app:route("/api/v1/server/add", function(params)
check_require_params(params, {"service_id", "ip", "port", "weight", "description", "protocol"})
check_require_params(params, { "service_id", "ip", "port", "weight", "description", "protocol" })
return server_model.add(params["service_id"], params["ip"], params["port"], params["weight"], params["description"], params["protocol"])
end)

app:route("/api/v1/server/delete", function(params)
check_require_params(params, {"server_id"})
check_require_params(params, { "server_id" })
return server_model.delete(params["server_id"])
end)

app:route("/api/v1/server/get", function(params)
check_require_params(params, {"server_id"})
check_require_params(params, { "server_id" })
return server_model.getServer(params["server_id"])
end)

app:route("/api/v1/server/edit", function(params)
check_require_params(params, {"server_id", "ip", "port", "weight", "description", "protocol"})
check_require_params(params, { "server_id", "ip", "port", "weight", "description", "protocol" })
return server_model.update(params["server_id"], params["ip"], params["port"], params["weight"], params["description"], params["protocol"])
end)

app:route("/api/v1/api/list", function(params)
check_require_params(params, {"service_id"})
check_require_params(params, { "service_id" })
return api_model.getServiceApis(params["service_id"])
end)

app:route("/api/v1/api/add", function(params)
check_require_params(params, {"service_id","request_uri","original_uri","description"})
check_require_params(params, { "service_id", "request_uri", "original_uri", "description" })
return api_model.add(params["service_id"], params["request_uri"], params["original_uri"], params["description"])
end)

app:route("/api/v1/api/delete", function(params)
check_require_params(params, {"api_id"})
check_require_params(params, { "api_id" })
return api_model.delete(params["api_id"])
end)

app:route("/api/v1/api/get", function(params)
check_require_params(params, {"api_id"})
check_require_params(params, { "api_id" })
return api_model.getApi(params["api_id"])
end)

app:route("/api/v1/api/edit", function(params)
check_require_params(params, {"api_id","request_uri","original_uri","description"})
check_require_params(params, { "api_id", "request_uri", "original_uri", "description" })
return api_model.update(params["api_id"], params["request_uri"], params["original_uri"], params["description"])
end)

Expand Down

0 comments on commit bf291ec

Please sign in to comment.