Skip to content

Commit

Permalink
Merge pull request #15 from horan-geeker/develop
Browse files Browse the repository at this point in the history
feat: add helper function and mysql WR divided
  • Loading branch information
horan-geeker authored Apr 2, 2019
2 parents 52dfffd + f5586be commit 6550e87
Show file tree
Hide file tree
Showing 22 changed files with 270 additions and 220 deletions.
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* [排序](#排序)
* [分页](#分页)
* [使用原生 sql](#使用原生-sql)
* [读写分离](#读写分离)
* [Redis](#Redis)
* [综合](#综合)
* [Random](#Random)
Expand Down Expand Up @@ -79,7 +80,7 @@ local _M = {}

function _M:index()
local args = request:all() -- get all args
response:json(0, 'request args', args) -- return response 200 and json content
return response:json(0, 'request args', args) -- return response 200 and json content
end

return _M
Expand Down Expand Up @@ -152,7 +153,7 @@ route:group({
```lua
function _M:comments(user_id, comment_id)
ngx.log(ngx.ERR, user_id, comment_id)
common:response(0, 'comments', {user_id=user_id, comment_id=comment_id})
return:response:json(0, 'comments', {user_id=user_id, comment_id=comment_id})
end
```
Expand All @@ -166,12 +167,13 @@ end
```lua
function _M:handle()
if not auth_service:check() then
common:response(4,'no authorized in authenticate')
return false, response:json(4,'no authorized in authenticate')
end
end

```
你可以把你自定义的中间件写到 `middleware` 的文件夹下, 该文件夹下已有了一个示例中间件 `example_middleware.lua`
当返回 false 的时候会直接返回第二个 `response` 参数,从而不再执行 `controller` 的内容,当返回 true 的时候继续执行,你可以把你自定义的中间件写到 `middleware` 的文件夹下, 该文件夹下已有了一个示例中间件 `example_middleware.lua`
### 控制器
Expand Down Expand Up @@ -216,7 +218,7 @@ local data = cjson.decode(res.body)
4. 第四个参数是返回的 `http 状态码`,可省略,默认是200
```lua
response:json(0x000000, 'success message', data, 200)
return response:json(0x000000, 'success message', data, 200)
--[[
{
"msg": "success message",
Expand All @@ -229,7 +231,7 @@ response:json(0x000000, 'success message', data, 200)
或者返回错误信息
```lua
response:json(0x000001)
return response:json(0x000001)
--[[
{
"msg": "验证错误",
Expand Down Expand Up @@ -382,21 +384,15 @@ local userPages = User:paginate(1)
-- user.lua
local Model = require("models.model")
local Post = require('models.post')

local User = Model:new('users')

function User:posts()
return User:has_many(Post, 'user_id', 'id')
end

return User

-- post.lua
local Model = require("models.model")
local config = require("config.app")

local Post = Model:new('posts')

return Post

-- controller 调用
Expand Down Expand Up @@ -430,8 +426,6 @@ local user_and_post = User:where('id', '=', user_id):with('posts'):get()
-- post.lua
local Model = require("models.model")
local Tag = require('models.tag')
local config = require("config.app")

local Post = Model:new('posts')

function Post:tag()
Expand All @@ -440,12 +434,10 @@ end

return Post


-- tag.lua
local Model = require("models.model")
local config = require("config.app")

local Tag = Model:new('tags')

return Tag

-- controller 调用
Expand All @@ -465,6 +457,10 @@ local posts_with_tag = Post:where('id', '=', 1):with('tag'):first()
--]]
```
#### 读写分离
通过配置 `config/database.lua` 文件中 `mysql_config.READ``mysql_config.WRITE` 框架会根据 model 的操作自动分配读写,如果不做分离则配置为相同的
### Redis
```lua
Expand Down Expand Up @@ -538,7 +534,7 @@ end
## 用户 Auth API 接口说明
> 所有接口均返回json数据,你也可以自定义 `config/app.lua``user_table_name` 用户表名,`login_id` 用于登录的列名,并且在根目录执行 `chmod 755 install.sh && ./install.sh` 迁移数据库结构。
> 所有接口均返回json数据,(你也可以更加你已有的数据库更改模型) `users` 是用户表名,`phone` 用于登录的列名,并且在根目录执行 `chmod 755 install.sh && ./install.sh` 迁移数据库结构。
```json
{
Expand Down
4 changes: 2 additions & 2 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ you can use `Dockerfile` to build nana that located
* execute `cp env.example.lua env.lua` configure `mysql redis`
* config `content_by_lua_file` point to `bootstrap.lua`(at project root directory) in your `nginx conf file` and run nginx.

> Note: if you want to use login/register function in nana framework, you need to configure `config/app.lua`: `user_table_name` name of user table in database, `login_id` name of user table's column for login as username and execute `chmod 755 install.sh && ./install.sh` to migrate database structure.
> Note: if you want to use login/register function in nana framework, you need to configure `config/app.lua`: `users` is the name of user table in database, `phone` of user table's column for login as username and execute `chmod 755 install.sh && ./install.sh` to migrate database structure.
## Document

### Config

* All of your configuration files for Nana Framework are stored in the app.lua, and it has many config keys in that file, such as `db_name` which represents the database name, `user & password` that represents database username and password, `user_table_name` that represents the table name which you want store user data, `login_id` is a column name which is used for authentication.
* All of your configuration files for Nana Framework are stored in the app.lua, and it has many config keys in that file, such as `db_name` which represents the database name, `user & password` that represents database username and password, `users` that represents the table name which you want store user data, `phone` is a column name which is used for authentication.
* Write your routes in router.lua.

### middleware
Expand Down
6 changes: 3 additions & 3 deletions bootstrap.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
-- Github Document: https://github.com/horan-geeker/nana
-- Author: hejunwei
-- Version: v0.3.0
-- Version: v0.4.0

local Core = {}

function Core:bootstrap()
-- get helper function
require('lib.helpers'):init(_G)
-- dispatche route to controller
require("lib.dispatcher"):run()
-- run application
require("lib.application"):init():run()
end

Core:bootstrap()
21 changes: 10 additions & 11 deletions config/app.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
local env = require('../env')

return {
env = env.APP_ENV or 'production',
env = env('APP_ENV', 'production'),

locale = 'zh',
fallback_locale = 'en',

time_zone = "+8:00", -- UTC + 8

session_lifetime = 3600 * 24 * 30, --sec, here means a month
session_refresh_time = 3600 * 24 * 7, --sec, here means a week
max_request_per_second = 3000, -- throttle flow request per second
per_page = env.per_page or 10,
redis_prefix = 'NANA:',
-- auth
user_table_name = "users",
login_id = "phone", -- login colunm name use email/username/phone...

phone_code_len = 4,
per_page = env('per_page', 10),

-- oauth
wechat = {
web = {
Expand All @@ -24,8 +23,8 @@ return {
},
sendcloud = {
url = "http://www.sendcloud.net/smsapi/send",
smsUser = env.sendcloud.SMSUSER,
smsKey = env.sendcloud.SMSKEY,
templateId = env.sendcloud.TEMPLATEID
smsUser = env('sendcloud.SMSUSER'),
smsKey = env('sendcloud.SMSKEY'),
templateId = env('sendcloud.TEMPLATEID')
}
}
23 changes: 23 additions & 0 deletions config/database.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
return {
redis_prefix = 'NANA:',
mysql = {
db_name = env("mysql_config.db_name", "nana"),
write = { -- mysql write database
host=env("mysql_config.write.host", "10.200.10.1"),
port=env("mysql_config.write.port", 3306),
user=env("mysql_config.write.user", "root"),
password=env("mysql_config.write.password", "root"),
},
read = { -- mysql read database
host=env("mysql_config.read.host", "10.200.10.1"),
port=env("mysql_config.read.port", 3307),
user=env("mysql_config.read.user", "root"),
password=env("mysql_config.read.password", "root"),
},
charset = 'utf8',
pool_timeout = 1000, -- mysql pool timeout
pool_size = 10000, -- mysql pool size
timeout = 1000, -- mysql timeout
},

}
42 changes: 21 additions & 21 deletions controllers/auth_controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,36 @@ function _M:login()
}
)
if not ok then
response:json(0x000001, msg)
return response:json(0x000001, msg)
end
local user = User:where('phone', '=', args.phone):first()
if not user then
response:json(0x010003)
return response:json(0x010003)
end
if args.smscode then
ok = user_service:verify_checkcode(args.phone, args.smscode)
if not ok then
response:json(0x000001, 'invalidate sms code')
return response:json(0x000001, 'invalidate sms code')
else
user_service:authorize(user)
end
elseif args.password then
ok, err = user_service:verify_password(args.password, user.password)
if not ok then
-- login fail
response:json(0x010002)
return response:json(0x010002)
else
ok, err = user_service:authorize(user)
if not ok then
-- @todo should render only error message
response:json(0x000001, err)
return response:json(0x000001, err)
end
end
else
response:json(0x000001, 'need sms or password')
return response:json(0x000001, 'need sms or password')
end

response:json(0, 'ok', table_remove(user, {'password'}))
return response:json(0, 'ok', table_remove(user, {'password'}))
end

function _M:register()
Expand All @@ -68,12 +68,12 @@ function _M:register()
}
)
if not ok then
response:json(0x000001, msg)
return response:json(0x000001, msg)
end
-- check if repeat
local user = User:where('phone', '=', args.phone):first()
if user then
response:json(0x010001)
return response:json(0x010001)
end
local name = args.name
if name == nil or name == '' then
Expand All @@ -91,18 +91,18 @@ function _M:register()

ok = User:create(user_obj)
if not ok then
response:json(0x000005)
return response:json(0x000005)
end
local user = User:where('phone', '=', args.phone):first()
user_service:authorize(user)
response:json(0, 'ok', table_remove(user, {'password'}))
return response:json(0, 'ok', table_remove(user, {'password'}))
end

function _M:logout()
local ok, err = auth:clear_token()
if not ok then
ngx.log(ngx.ERR, err)
response:json(0x00000A)
return response:json(0x00000A)
end
return response:json(0)
end
Expand All @@ -114,29 +114,29 @@ function _M:reset_password()
'new_password'
})
if not ok then
response:json(0x000001, msg)
return response:json(0x000001, msg)
end
if args.old_password == args.new_password then
response:json(0x010007)
return response:json(0x010007)
end
local user = auth:user()
local password = args.old_password
ok = user_service:verify_password(args.old_password, user.password)
if not ok then
-- password error
response:json(0x010005)
return response:json(0x010005)
end
local ok, err = User:where('id', '=', user.id):update({
password=hash(args.new_password)
})
if not ok then
response:json(0x000005)
return response:json(0x000005)
end
ok, err = auth:clear_token()
if not ok then
response:json(0x010006)
return response:json(0x010006)
end
response:json(0)
return response:json(0)
end

-- use sms verify
Expand All @@ -147,17 +147,17 @@ function _M:forget_password()
'new_password'
})
if not ok then
response:json(0x000001, msg)
return response:json(0x000001, msg)
end
local user = auth:user()
local ok, err = User:where('phone', '=', args.phone):update({
password=hash(args.new_password)
})
if not ok then
ngx.log(ngx.ERR, err)
response:json(0x010006)
return response:json(0x010006)
end
response:json(0)
return response:json(0)
end

return _M
2 changes: 1 addition & 1 deletion controllers/index_controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local _M = {}

function _M:index()
local args = request:all() -- get all args
response:json(0, 'request args', args)
return response:json(0, 'request args', args)
end

return _M
Loading

0 comments on commit 6550e87

Please sign in to comment.