A. Vapor 설치하기
1. 호환 가능한 환경 체크하기:
curl -sL check.vapor.sh | bash
2. 다운로드 받고 설치 하기
curl -sL toolbox.vapor.sh | sh
3. 설치 확인하기
vapor –help
B. Vapor 프로젝트 생성하고 실행하기
1. 새 프로젝트 생성하기
vapor new hello-vapor
2. Xcode에서 작업하기 (새로 생성된 폴더 안에 들어가서)
vapor xcode
3. 서버 시작하기
- Xcode에서 빌드 타겟을 변경한 후 Build & Run을 누른다.
- 브라우져에 가서 localhost:8080을 입력하여 서버가 작동하는지 확인한다.
C. Droplet 사용하기
1. Source/App/main.swift 파일 안에 있는 내용을 다 지운다. 그리고 다음을 입력한다.
2. Get request에 응답 하고 싶다. 다음을 입력한다.
(Drop의 get 메서드는 parameter 하나가 들어가고 표시 가능한 리소스를 반환해주는 클로져를 인자로 받는다.)
3. Get response에 JSON 타입을 반환하고 싶을 때 다음을 입력한다.
4. 하위 경로 URL의 Get response 만들기
- drop.get("hello") { request in
- return try JSON(node: [
- "message" : "Hello, again!"
- ])
- }
- drop.get("hello", "there") { request in
- return try JSON(node: [
- "message" : "Hello, there!"
- ])
- }
5. Get request에서 String Querey 받기
- drop.get("beers", Int.self) { request, beers in
- return try JSON(node: [
- "message" : "Take one down, pass it around, \(beers - 1) bottles of beer on the wall..."
- ])
- }
6. Post request 하기
- drop.post("post"){ request in
- guard let name = request.data["name"]?.string else{
- throw Abort.badRequest
- }
- return try JSON(node: ["message" : "Hello, \(name)!" ])
- }
D. Heroku에 배포하기
1. 프로젝트를 Git에 commit하기
2. Heroku 가입하기
3. https://devcenter.heroku.com/articles/heroku-cli 에 가서 관련 도구 다운 받아 설치하기
4. 설치 확인하기
heroku –version
5. Command line에서 Heroku 접속하기
heroku login
6. Heroku에 배포하기
vapor heroku init