跳转到内容

Hello World

你的第一个 Wippy 应用程序 - 一个返回 JSON 的简单 HTTP API。

一个带有单个端点的最小 Web API:

GET /hello → {"message": "hello world"}
hello-world/
├── wippy.lock # 生成的锁文件
└── src/
├── _index.yaml # 入口定义
└── hello.lua # 处理程序代码
Terminal window
mkdir hello-world && cd hello-world
mkdir src

创建 src/_index.yaml

version: "1.0"
namespace: app
entries:
# HTTP 服务器
- name: gateway
kind: http.service
addr: ":8080"
lifecycle:
auto_start: true
# 路由器
- name: api
kind: http.router
meta:
server: app:gateway
prefix: /
# 处理函数
- name: hello
kind: function.lua
source: file://hello.lua
method: handler
modules:
- http
# 端点
- name: hello.endpoint
kind: http.endpoint
meta:
router: app:api
method: GET
func: app:hello
path: /hello

四个入口协同工作:

  1. gateway - 监听 8080 端口的 HTTP 服务器
  2. api - 通过 meta.server 附加到 gateway 的路由器
  3. hello - 处理请求的 Lua 函数
  4. hello.endpoint - 将 GET /hello 路由到该函数

创建 src/hello.lua

local http = require("http")
local function handler()
local res = http.response()
res:set_content_type(http.CONTENT.JSON)
res:set_status(http.STATUS.OK)
res:write_json({message = "hello world"})
end
return {
handler = handler
}

http 模块提供对 request/response 对象的访问。函数返回一个包含导出的 handler 方法的表。

Terminal window
# 从源代码生成锁文件
wippy init
# 启动运行时(-c 表示彩色控制台输出)
wippy run -c

你将看到类似以下的输出:

╦ ╦╦╔═╗╔═╗╦ ╦ Adaptive Application Runtime
║║║║╠═╝╠═╝╚╦╝ v0.1.20
╚╩╝╩╩ ╩ ╩ by Spiral Scout
0.00s INFO run runtime ready
0.11s INFO core service app:gateway is running {"details": "service listening on :8080"}
Terminal window
curl http://localhost:8080/hello

响应:

{"message":"hello world"}
  1. gateway 在 8080 端口接受 TCP 连接
  2. api 路由器匹配路径前缀 /
  3. hello.endpoint 匹配 GET /hello
  4. hello 函数执行并写入 JSON 响应
命令描述
wippy initsrc/ 生成锁文件
wippy run从锁文件启动运行时
wippy run -c启动并显示彩色控制台输出
wippy run -v启动并显示详细调试日志
wippy run -s以静默模式启动(无控制台日志)