• lapis使用


    lapis:

    安装

    http://leafo.net/lapis/ 官网安装方式依赖lua-cjson,但是已经安装了openresty的可能会报错。
    解决方法,使用下面issue中的luarocks install lua-cjson2
    https://github.com/leafo/lapis/issues/539

    使用

    lapis提供lua和moonscript两种代码。

    生成一个新的项目

    默认生成moonscript脚本,增加--lua这个flag可以生成lua的脚本。包含框架的四个基本的文件。
    lapis new --lua 
    
    

    运行

    如果安装了openresty,使用

    lapis server   
    

    打开http://localhost:8080可以看到欢迎界面。

    配置,在当前文件夹创建一个文件config.lua

    local config = require("lapis.config")
    
    config("development", {
      port = 9090
    })
    

    启动之后就端口就改成了9090端口。

    视图

    创建一个views的文件夹,里面放一个文件index.etlua,内容如下:

    <h1>Hello world</h1>
    <p>Welcome to my page</p>
    

    lapis将会将index.etlua文件内容解析输出为html网页格式。需要使用app:enable("etlua")来让lapis解析对应的etlua文件为html。

    简单封装使用

    local respond_to = require ("lapis.application").respond_to
    app:enable("etlua")
    
    local blm_hc_status = require "controllers.blm_hc_status"
    app:match("/hello", respond_to(blm_hc_status))
    

    新建的文件夹controllers文件夹下面,创建一个文件,因为是match,里面可以写四种方法,访问http://localhost:9090/hello

    local db = require("lapis.db")
    
    local mt = {}
    function mt:GET()
        local res = db.query("select * from tb_hc_status")
        self.services = res
        return { render = "index" }
    end
    
    return mt
    
  • 相关阅读:
    Redis 安全
    Redis 数据备份与恢复
    Redis 服务器
    Redis 连接
    Redis 脚本
    Linux中使用netstat命令的基本操作,排查端口号的占用情况
    ElasticSearch 常用查询语句
    GO代码风格指南 Uber Go (转载)
    coding 注意事项(总结中)
    Byte字节
  • 原文地址:https://www.cnblogs.com/mentalidade/p/7716592.html
Copyright © 2020-2023  润新知