Pular para o conteúdo

Hello World

Sua primeira aplicação Wippy - uma API HTTP simples que retorna JSON.

Uma API web mínima com um endpoint:

GET /hello → {"message": "hello world"}
hello-world/
├── wippy.lock # Arquivo lock gerado
└── src/
├── _index.yaml # Definições de entradas
└── hello.lua # Código do handler
Terminal window
mkdir hello-world && cd hello-world
mkdir src

Crie src/_index.yaml:

version: "1.0"
namespace: app
entries:
# Servidor HTTP
- name: gateway
kind: http.service
addr: ":8080"
lifecycle:
auto_start: true
# Router
- name: api
kind: http.router
meta:
server: app:gateway
prefix: /
# Função handler
- name: hello
kind: function.lua
source: file://hello.lua
method: handler
modules:
- http
# Endpoint
- name: hello.endpoint
kind: http.endpoint
meta:
router: app:api
method: GET
func: app:hello
path: /hello

Quatro entradas trabalham juntas:

  1. gateway - Servidor HTTP escutando na porta 8080
  2. api - Router anexado ao gateway via meta.server
  3. hello - Função Lua que processa requisições
  4. hello.endpoint - Roteia GET /hello para a função

Crie 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
}

O módulo http fornece acesso aos objetos request/response. A função retorna uma tabela com o método handler exportado.

Terminal window
# Gerar arquivo lock a partir do source
wippy init
# Iniciar o runtime (-c para saída colorida no console)
wippy run -c

Você verá uma saída como:

╦ ╦╦╔═╗╔═╗╦ ╦ 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

Resposta:

{"message":"hello world"}
  1. gateway aceita a conexão TCP na porta 8080
  2. api router faz match do prefixo de caminho /
  3. hello.endpoint faz match de GET /hello
  4. função hello executa e escreve a resposta JSON
ComandoDescrição
wippy initGerar arquivo lock a partir de src/
wippy runIniciar runtime a partir do arquivo lock
wippy run -cIniciar com saída colorida no console
wippy run -vIniciar com logging verbose de debug
wippy run -sIniciar em modo silencioso (sem logs no console)