Hello World
Sua primeira aplicação Wippy - uma API HTTP simples que retorna JSON.
O Que Vamos Construir
Uma API web mínima com um endpoint:
GET /hello → {"message": "hello world"}
Estrutura do Projeto
hello-world/
├── wippy.lock # Arquivo lock gerado
└── src/
├── _index.yaml # Definições de entradas
└── hello.lua # Código do handler
Passo 1: Criar Diretório do Projeto
mkdir hello-world && cd hello-world
mkdir src
Passo 2: Definições de Entradas
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: 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: hello
path: /hello
Quatro entradas trabalham juntas:
gateway- Servidor HTTP escutando na porta 8080api- Router anexado ao gateway viameta.serverhello- Função Lua que processa requisiçõeshello.endpoint- RoteiaGET /hellopara a função
Passo 3: Código do Handler
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.
Passo 4: Inicializar e Executar
# 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"}
Passo 5: Testar
curl http://localhost:8080/hello
Resposta:
{"message":"hello world"}
Como Funciona
gatewayaceita a conexão TCP na porta 8080apirouter faz match do prefixo de caminho/hello.endpointfaz match deGET /hello- função
helloexecuta e escreve a resposta JSON
Referência CLI
| Comando | Descrição |
|---|---|
wippy init |
Gerar arquivo lock a partir de src/ |
wippy run |
Iniciar runtime a partir do arquivo lock |
wippy run -c |
Iniciar com saída colorida no console |
wippy run -v |
Iniciar com logging verbose de debug |
wippy run -s |
Iniciar em modo silencioso (sem logs no console) |
Próximos Passos
- Echo Service - Tratar parâmetros de requisição
- Task Queue - REST API com processamento em background
- HTTP Router - Padrões de roteamento