lua + openresty web API 框架

这是一个非常易用简单的 web api 框架,采取了一些较好的 php 框架的设计

主要目录结构分为 lib(公共方法),model(数据库相关),controller(由 nginx location 指向的 lua文件也就是控制器)

以下是一些基本用法:

local cjson = require('cjson')
local conf = require('config.app')
local Model = require('models.model')
local request = require('lib.request')
local validator = require('lib.validator')

--use request to get all http args
ngx.say(cjson.encode(request))
--curl "localhost:8001?id=1" -d name=foo     
--{"name":"foo","id":"1"}

local ok,msg = validator:check({
    name = {require=1,max=6,min=4},
    id = {require=0}},
    request)

if not ok then
    ngx.say(msg)
end

local User = Model:new('users')
ngx.say('where demo:\n',cjson.encode(User:where('username','=','cgreen'):where('password','=','7c4a8d09ca3762af61e59520943dc26494f8941b'):get()))
-- {"password":"7c4a8d09ca3762af61e59520943dc26494f8941b","gender":"?","id":99,"username":"cgreen","email":"[email protected]"}

ngx.say('orwhere demo:\n',cjson.encode(User:where('id','=','1'):orwhere('id','=','2'):get()))
-- {"password":"7c4a8d09ca3762af61e59520943dc26494f8941b","gender":"?","id":1,"username":"hejunwei","email":"[email protected]"},
-- {"password":"7c4a8d09ca3762af61e59520943dc26494f8941b","gender":"?","id":2,"username":"ward.antonina","email":"[email protected]"}

local Admin = Model:new('admins')
local admin = Admin:find(1)
ngx.say('find demo:\n',cjson.encode(admin))
-- {"password":"d033e22ae348aeb5660fc2140aec35850c4da997","id":1,"email":"[email protected]","name":"admin"}
--Admin:update({name='update demo'}):where('id','=','3'):query()
Admin:update({
        name='update test',
        password="111111"
    }):where('id','=',3):query()

Admin:insert({
    id=3,
    password='123456',
    name='horanaaa',
    email='[email protected]',
})

github:nana framework

你可能感兴趣的:(nginx)