用 Rust 编写的快速 Lua 运行时
Fast Lua runtime written in Rust

原始链接: https://astra.arkforge.net/

``` -- 创建一个新的本地服务器 local server = require("http").server.new() -- 注册一个路由 server:get("/", function() return "hello from default Astra instance!" end) -- 你也可以在路由中使用局部变量 local counter = 0 server:get("/count", function(request, response) -- 消费请求体 print(request:body():text()) -- 设置状态码 (可选) response:set_status_code(300) -- 设置头部 (可选) response:set_header("header-key", "header-value") counter = counter + 1 -- 也可以返回JSON return { counter = counter } end) -- 配置服务器 server.port = 3000 -- 运行服务器 server:run() ```

## Astra:一个用 Rust 构建的 Lua Web 服务器运行时 - 摘要 一个名为 Astra 的新项目旨在为 Lua (5.1-5.4)、Luau 和 LuaJIT 提供一个 Web 服务器运行时,使用 Rust 构建。然而,讨论很快表明 Astra **并没有用 Rust 实现一个新的 Lua 引擎**,而是通过像 `mlua` 这样的 crates 包装现有的 C/C++ Lua 实现,提供 Rust 接口并将它们打包成一个单独的二进制文件。 该项目的命名和描述引发了争论,一些人质疑使用“运行时”,因为它并没有重写 Lua 解释器本身。作者承认这种混淆,并计划澄清文档。Astra 的目标是通过提供一个捆绑的环境,其中包含可以从 Lua 访问的现成 Rust 库,从而加快开发速度,最初专注于 Web 服务器应用程序,但会扩展到其他用例。 虽然 Astra 不是一种新颖的 Lua 实现,但它旨在提供便利性和与 Rust 生态系统的集成,类似于 JavaScript 世界中的 Bun 或 Deno 等项目。讨论中还提到了其他几个 Lua Web 服务器框架,如 Redbean 和 Luvit。
相关文章

原文
-- Create a new server
local server = require("http").server.new()

-- Register a route
server:get("/", function()
    return "hello from default Astra instance!"
end)

-- You can also use the local variables within routes
local counter = 0
server:get("/count", function(request, response)
    -- consume the request body
    print(request:body():text())

    -- set header code (Optional)
    response:set_status_code(300)
    -- set headers (Optional)
    response:set_header("header-key", "header-value")

    counter = counter + 1
    -- and also can return JSON
    return { counter = counter }
end)

-- Configure the server
server.port = 3000

-- Run the server
server:run()
联系我们 contact @ memedata.com