lapis 基本开发

作者: dalongrong | 来源:发表于2017-11-16 10:17 被阅读14次

    1. 生成项目代码

    // 支持lua 以及 moonscript, 默认是moonscript 通过--lua 可以生成lua 的代码

    lapisnew--lua​

    ├──app.lua

    ├──mime.types

    ├──models.lua

    ├──nginx.conf

    2. 启动项目

    //  可以守护进程模式运行,修改nginx.conf 模板代码  daemon on;

    lapis server​

    3. 修改项目默认环境配置(比如开发、生产)

    // 使用config.lua  默认是development

    local config=require("lapis.config")

    config("development", {

    port=9090

    })

    config("product",{

    port=8080

    })

    // 启动执行的配置

    lapis server product

    备注:以上的配置文件可以直接在nginx.conf使用

    events{

    worker_connections${{WORKER_CONNECTIONS}};

    }

    同时可以使用api直接进行访问,类似大家在nodejs开发中需要获取package.json信息(pkginfonpm包)

    4. 视图view 创建

    // 默认在 views 目录,使用的是模板 etlua

    local lapis=require("lapis")

    local app=lapis.Application()

    app:enable("etlua")//  默认未启用

    app:get("/",function(self)

    return{ render="index"}

    end)

    return app

    //views/index.etlua

    <h1>Hello world</h1>

    <p>welcome to my page</p>

    5. 创建模板布局

    // views/layout.etlua

    <!DOCTYPE html>

    <html lang="zh">

    <head>

         <meta  charset="utf-8" >

         <title><%= page_title or "my page"%></title>

    </head>

    <body>

             <h1>Greetings</h1>

             <%  content_for("inner") %>

    </body

    // 启用模板布局 app.lua 注意顺序比较重要,必须在 app:enable("etlua") 下面

    app.layout=require"views.layout"

    <h1>Hello world</h1>

    <p>welcome to my page</p>

    6. 参考文档

    http://leafo.net/lapis/reference/configuration.html

    https://github.com/leafo/etlua

    http://leafo.net/lapis/reference/etlua_templates.html

    相关文章

      网友评论

        本文标题:lapis 基本开发

        本文链接:https://www.haomeiwen.com/subject/pxzkvxtx.html