Hello World
Hello World
Seção intitulada “Hello World”Sua primeira aplicação Wippy - uma API HTTP simples que retorna JSON.
O Que Vamos Construir
Seção intitulada “O Que Vamos Construir”Uma API web mínima com um endpoint:
GET /hello → {"message": "hello world"}Estrutura do Projeto
Seção intitulada “Estrutura do Projeto”hello-world/├── wippy.lock # Arquivo lock gerado└── src/ ├── _index.yaml # Definições de entradas └── hello.lua # Código do handlerPasso 1: Criar Diretório do Projeto
Seção intitulada “Passo 1: Criar Diretório do Projeto”mkdir hello-world && cd hello-worldmkdir srcPasso 2: Definições de Entradas
Seção intitulada “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: 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: /helloQuatro 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
Seção intitulada “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
Seção intitulada “Passo 4: Inicializar e Executar”# Gerar arquivo lock a partir do sourcewippy init
# Iniciar o runtime (-c para saída colorida no console)wippy run -cVocê verá uma saída como:
╦ ╦╦╔═╗╔═╗╦ ╦ Adaptive Application Runtime║║║║╠═╝╠═╝╚╦╝ v0.1.20╚╩╝╩╩ ╩ ╩ by Spiral Scout
0.00s INFO run runtime ready0.11s INFO core service app:gateway is running {"details": "service listening on :8080"}Passo 5: Testar
Seção intitulada “Passo 5: Testar”curl http://localhost:8080/helloResposta:
{"message":"hello world"}Como Funciona
Seção intitulada “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
Seção intitulada “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
Seção intitulada “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