4 Star 5 Fork 1

poempro / tiny

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

tiny

openresty blog demo

目的:新手快速入门openresty开发 本demo有借鉴: https://git.oschina.net/love_linger/OmniWeb 可惜本人未能联系上作者;

好了,进入正题

目录

Tiny
|++ app				应用目录
|	|++ controllers	控制器
|	|++ models		模型
|	|++ views		视图
|	|++ boot		引导执行时初始化, 如验证登陆
|	|++ common		公共配置文件
|	|-- config.lua	应用配置,默认加载到全局就是_CONF中
|   |++ public		入口文件目录 
|	|----+ css 
|	|----+ js
|	|----+ upload
|	|----+ index.lua  单入口
|
|++ core				框架核心
|	|++ vendor			插件库
|	|-- tiny.lua		预加载文件列表及开始执行
|	|-- common.lua		内置的常用函数
|	|-- controller.lua	controller基类
|	|-- cookie.lua		cookie的实现
|	|-- error.lua		默认的错误处理
|	|-- model.lua		model层的实现 
|	|-- router.lua		不是路由的路由
|	|-- template.lua	导入lua-resty-template库
|

++++++++++ +++++ +++++ +++++ +++++ 控制器方法 +++++ +++++ +++++ +++++ +++++ +++++ +++++ +++++

  1. 创建控制器 Tiny/app/controllers/index.lua
  2. 文件内容如下 local index = controller.new();
function index:index() 
          _GET  = ngx.req.get_uri_args();  --接收get参数
	      local page =  vendor("page");  --加载分页库 .
	      
	      local m = model.__get("article");--加载模型 .
	      
	      local countNum = tonumber(m:count({}));  --总条数
	      local res ={};  
	      local  pageres ={}
	      if countNum > 0 then 
	           pageres = page:get(countNum, 15, _GET['page']);  --调用分页
	           res = m:getall({}, pageres['limit']);   --得到limit分页值
	      end 
     
         self.assign('list', res);  --结果传入到view
         self.assign('page', pageres);--分页传入到view
         self.assign('title', "首页"); --传入
         self.render('zh_cn/index/main.html');  --渲染模板
end 

return index;

++++++++++ +++++ +++++ +++++ +++++ 模型 +++++ +++++ +++++ +++++ +++++ 1.创建模型 Tiny/app/models/article.lua

  1. 文件内容如下 local article = model.__new( _CONF["mysql"] , "mysql"); --mysql 就是mysql 模型 redis 就是redis模型 如果都要用到就再次加载 --加载模型用 local m = model.__get("cache");

function article:getall(where, limit) local str = ""; if where['category_id'] and #(where['category_id']) > 0 then
str = str .. ' category_id IN ( '; for k, v in pairs(where['category_id']) do
str = str .. v .. ','; end
str = string.sub(str, 1, -2); str = str .. ') ';
end

 	if  string.len(str) >= 3 then
		       str = " WHERE " .. str; 
	end
  
     if limit then 
		 limit = " ORDER BY id DESC LIMIT  " .. limit;
	 end

     local sql = 'SELECT * FROM  ' .. self.table .. str .. limit;   
    -- dump(sql);
     local res, err, errno, sqlstate = self.mysql(sql); 
     return res;
end 

return article;

+++++ +++++ +++++ +++++ +++++ 视图 +++++ +++++ +++++ +++++ +++++ +++++ 1.创建模型 Tiny/app/view/article.lua

  1. 文件内容如下

+++++ Cookie 1.查看 Tiny/app/boot/init.lua

+++++++++++++++++++加载扩展库 local aes = vendor("aes"); --加载aes扩展库 返回的是table

+++++ ++++++++++ 初始化 框架最先加载 Tiny/app/boot/init.lua 文件中的init方法

[root@www webbench-1.5]# webbench -c 500 -t 60 http://www.im.cn/index/index Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.im.cn/index/index 500 clients, running 60 sec.

Speed=435480 pages/min, 1714964 bytes/sec. Requests: 435326 susceed, 154 failed.

The MIT License (MIT) Copyright (c) 2016 leo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

新手快速入门openresty开发 展开 收起
Lua
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Lua
1
https://gitee.com/sixiong/tiny.git
git@gitee.com:sixiong/tiny.git
sixiong
tiny
tiny
master

搜索帮助