Skip to content

Commit

Permalink
Merge pull request #5 from ffay/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ffay committed Mar 31, 2018
2 parents 08bc990 + bf291ec commit 1a89b75
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 48 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
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
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
23 changes: 17 additions & 6 deletions src/manage/access.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
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 session = ngx.shared.session
local ck = require "resty.cookie"
local cookie, err = ck:new()
local isApi = string.find(uri,"/api/v1/");
if isApi and (token == nil or token ~= cookie:get("token")) then
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 user == nil 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
if user == nil then
return ngx.redirect("/login.html")
end
end
return 1
end

return _M
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
Loading

0 comments on commit 1a89b75

Please sign in to comment.