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 3bf42dc commit 36101fc
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 13 deletions.
112 changes: 112 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
## api_key
api_key可以在config.lua中增加配置
```lua
config['api_key'] = "your api key string"
```

## 域名
### 添加域名
#### 接口地址
- POST /api/v1/domain/add?api_key={api_key}
#### 表单参数
| 参数 | 是否必须 | 说明 |
| ---------------- | ------ | --------------------------------------- |
| name || 域名名称 |

#### 返回示例
```json
{
"info": {
"insert_id": 17,
"affected_rows": 1,
"server_status": 2,
"warning_count": 0
},
"errno": 0,
"status": 200,
"msg": "success"
}
```

### 删除域名
#### 接口地址
- POST /api/v1/domain/delete?api_key={api_key}
#### 表单参数
| 参数 | 是否必须 | 说明 |
| ---------------- | ------ | --------------------------------------- |
| domain_id || 域名ID |

#### 返回示例
```json
{
"errno": 0,
"status": 200,
"msg": "success"
}
```
> 域名删除时会删除所有域名相关的数据,慎用
### 修改域名
#### 接口地址
- POST /api/v1/domain/update?api_key={api_key}
#### 表单参数
| 参数 | 是否必须 | 说明 |
| ---------------- | ------ | --------------------------------------- |
| domain_id || 域名ID |
| name || 域名名称 |

#### 返回示例
```json
{
"errno": 0,
"status": 200,
"msg": "success"
}
```
> 域名删除时会删除所有域名相关的数据,慎用
### 查询已添加域名列表
#### 接口地址
- GET /api/v1/domain/all?api_key={api_key}

#### 返回示例
```json
{
"info": [
{
"name": "www.xxx.com",
"id": 2
}
],
"errno": 0,
"status": 200,
"msg": "success"
}
```

## 模块
> 每个域名下可以有多个模块
- /api/v1/service/add
- /api/v1/service/edit
- /api/v1/service/delete
- /api/v1/service/get
- /api/v1/service/list

## 服务器
> 每个模块可以有多个后端服务器
- /api/v1/server/add
- /api/v1/server/edit
- /api/v1/server/delete
- /api/v1/server/get
- /api/v1/server/list

## URI映射
> 每个模块可以有多个URI接口映射
- /api/v1/api/add
- /api/v1/api/edit
- /api/v1/api/delete
- /api/v1/api/get
- /api/v1/api/list
3 changes: 3 additions & 0 deletions src/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ config['mysql_db'] = "agw"
config['admin_name'] = "admin"
config['admin_pass'] = "admin"

-- 需要通过接口远程管理时打开下面的配置
-- config['api_key'] = "your apikey"

return config;
4 changes: 2 additions & 2 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ api_sync_timer = function(premature)
else
log(ERR, "sync api info failed:", result)
end
local ok, err = new_timer(api_sync_interval, api_sync_timer)
new_timer(api_sync_interval, api_sync_timer)
end
end

Expand All @@ -33,7 +33,7 @@ server_state_check_timer = function(premature)
else
log(ERR, "error occured when check server state:", result)
end
local ok, err = new_timer(server_state_check_interval, server_state_check_timer)
new_timer(server_state_check_interval, server_state_check_timer)
end
end

Expand Down
9 changes: 8 additions & 1 deletion src/manage/access.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
local _M = {}
local cjson = require "cjson"
local config = require "config"

function _M.checkLogin()
local params = ngx.req.get_uri_args()
if params["api_key"] ~= nil and params["api_key"] == config["api_key"] then
return 1
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 ck = require "resty.cookie"
local cookie, err = ck:new()
local isApi = string.find(uri,"/api/v1/");
local isApi = string.find(uri, "/api/v1/");
if isApi and (token == nil or token ~= cookie:get("token")) then
local response = {}
response["status"] = 401
response["errno"] = 40100
response["msg"] = "Authentication required"
ngx.header.content_type = 'application/json;charset=UTF-8';
ngx.say(cjson.encode(response))
ngx.exit(401)
return;
end
if token == nil or token ~= cookie:get("token") then
return ngx.redirect("/login.html")
end
end
return 1
end

return _M
29 changes: 19 additions & 10 deletions src/service/agw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function check_state(ip, port)
local ok, err = sock:connect(ip, port)
sock:close()
if not ok then
log(ERR, "server connect err:", err)
return false
log(ERR, "server connect err:", err)
return false
end
return true
end
Expand All @@ -28,37 +28,46 @@ function set_domain_api_info(domainId, domainName)
local service_api_uri_map = {}
local service_api_uri_array = {}
for k, service in pairs(services) do
local servers=server_model.getServiceServersWithState(service["id"], 1)
local apis=api_model.getServiceApis(service["id"])
local servers = server_model.getServiceServersWithState(service["id"], 1)
local apis = api_model.getServiceApis(service["id"])
for k, api in pairs(apis) do
api["servers"] = servers
api["host"] = service["host"]
if service["host"] == "" then
api["host"] = domainName
end
local original_uri, index=string.gsub(api["original_uri"], "%$([0-9]+)", "%%%1")
local original_uri, index = string.gsub(api["original_uri"], "%$([0-9]+)", "%%%1")
api["original_uri"] = original_uri
service_api_uri_map[api["request_uri"]] = api
table.insert(service_api_uri_array, api["request_uri"])
end
end
table.sort(service_api_uri_array, function(a,b) return string.len(a) > string.len(b) end)
local config = {api_uri_map = service_api_uri_map, api_uri_array = service_api_uri_array}
table.sort(service_api_uri_array, function(a, b) return string.len(a) > string.len(b) end)
local config = { api_uri_map = service_api_uri_map, api_uri_array = service_api_uri_array }
cache:set(domainName, cjson.encode(config))
end

function agw_service.syncApiInfo()
local cachedDomains = cache:get_keys(0);
local new_domain_map = {}
local domains, err = domain_model.getDomains()
for k, domain in pairs(domains) do
set_domain_api_info(domain["id"], domain["name"])
new_domain_map[domain["name"]] = domain
end
-- 清除缓存中已删除的域名配置
for k, domainName in pairs(cachedDomains) do
if new_domain_map[domainName] == nil then
cache:delete(domainName)
end
end
end

function agw_service.checkState()
local servers = server_model.getAllServers()
for k, server in pairs(servers) do
if check_state(server["ip"], server["port"]) == false then
log(ERR, "server down:", server["ip"]..":"..server["port"])
log(ERR, "server down:", server["ip"] .. ":" .. server["port"])
server_model.updateState(server["id"], 0)
else
server_model.updateState(server["id"], 1)
Expand All @@ -73,9 +82,9 @@ function agw_service.getServices(domainId)
local down_servers_count = table.getn(server_model.getServiceServersWithState(service["id"], 0))
local status = "no backend servers"
if up_servers_count ~= 0 or down_servers_count ~= 0 then
status = "up "..up_servers_count..",down "..down_servers_count
status = "up " .. up_servers_count .. ",down " .. down_servers_count
end
service["status"] = status
service["status"] = status
end
return services, err
end
Expand Down

0 comments on commit 36101fc

Please sign in to comment.