EventServer基于lua和libevent的完全异步、高性能的web框架。既可以作为游戏服务,也可以做web服务,支持websocket和https。
关键是可以ffi使用c/c++库,比如libcurl和iconv,也可以调用第三方lua库,比如smtp、redis、luasql等,采用委托线程执行的方式实现对io阻塞操作的异步调用,方便简单。
lua-web网站www.lua-web.com框架代码开源下载gitee.comEventServer/bloggitee.com
简单用法如下:
--首页
local function http_html_index(req)
print('http_html_index')
local header = evhttp_get_input_headers(req)
local ip = evhttp_get_remote_host(req)
local id = evhttp_hash(ip)
local parameter = evhttp_parse_query(req)
local page = tonumber(parameter.page and parameter.page or 1)
page = (page<=0) and 1 or page
local tab = parameter.tab
local and_str = tab and ' and a.category=\''..tab..'\'' or ''
local hots = load_hot_docs()
local bookmarks = load_bookmarks()
local sql = [[SELECT count(1) as cnt FROM blogs.documents as a where a.post_status=0]]..and_str..[[ ;]]
local ret,rows=ExecuteSQL(THREADID_SQL+math.mod(id,10), sql,true)
if not ret then
print('http_html_index ************************')
return
end
local pages=math.floor(tonumber(rows[1].cnt)+9/10)
local offset = (page-1)*10
local sql = [[SELECT a.`id`,
b.nickname as post_author,
a.`post_date`,
a.`post_title`,
a.`post_status`,
a.`category`,
a.`post_desc`,
a.`read_count`,
a.`comment_count`
FROM `blogs`.`documents` as a,`blogs`.`accounts` as b where a.post_status=0 and a.post_author=b.userid]]..and_str..[[ order by a.post_date desc limit ]]..offset..[[,10;]]
local ret,rows=ExecuteSQLBatch(THREADID_SQL+math.mod(id,10), {sql},true)
if not ret then
print('http_html_index ************************')
return
end
local tabs = {
{name=GetUtf8ByGBK('全部'), url='index.html' },
{name=GetUtf8ByGBK('EventServer'), url='index.html?tab='.. GetUtf8ByGBK('EventServer') },
{name=GetUtf8ByGBK('Python'), url='index.html?tab=Python' },
{name=GetUtf8ByGBK('AI'), url='index.html?tab=AI' },
{name=GetUtf8ByGBK('Game'), url='index.html?tab='.. GetUtf8ByGBK('Game') },
{name=GetUtf8ByGBK('写文章'), url='edit.html'}}
local context = {title=convert2utf8('首页'), page=page,pages=pages,count=QUERY_COUNT,tabs=tabs,bookmarks=bookmarks,hots=hots,tab=tab and tab or GetUtf8ByGBK('全部'),articles=rows}
local template = require "html.template"
local res = template.render(SERVER_DIRECTORY.."/asset/html/index.html", context, "no-cache" )
HttpRespone(req, function()
evhttp_add_header(req, 'Content-Type', "text/html")
evhttp_send_reply(req,200,'ok', res)
end)
print('********************Cookie', json.encode(header), header.cookie)
end