콘텐츠로 이동

Hello World

첫 번째 Wippy 애플리케이션 - JSON을 반환하는 간단한 HTTP API.

하나의 엔드포인트가 있는 최소한의 웹 API:

GET /hello → {"message": "hello world"}
hello-world/
├── wippy.lock # 생성된 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 모듈은 요청/응답 객체에 대한 접근을 제공합니다. 함수는 내보내진 handler 메서드가 있는 테이블을 반환합니다.

Terminal window
# 소스에서 lock 파일 생성
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.endpointGET /hello 매칭
  4. hello 함수가 실행되고 JSON 응답 작성
명령어설명
wippy initsrc/에서 lock 파일 생성
wippy runlock 파일에서 런타임 시작
wippy run -c컬러 콘솔 출력과 함께 시작
wippy run -v상세 디버그 로깅과 함께 시작
wippy run -s사일런트 모드로 시작 (콘솔 로그 없음)