zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    CodeForces 697B Barnicle 模拟
    15.三数之和
    167.两数之和
    209.长度最小子数组-sliding window
    COMP9313 Week9a-0
    树总纲(To be continued)
    COMP9517 Week8
    COMP9313 week8b Pipeline
    94. 二叉树的中序遍历
    COMP9313 Week8 Classification and PySpark MLlib
  • 原文地址:https://www.cnblogs.com/mentalidade/p/7716592.html
Copyright © 2011-2022 走看看