Thursday, November 23rd, 2017
- Creation of a Notes API using Vapor 2.0
- Specifications through an ad-hoc "Validator" project
- Vapor ready to use in a Docker image
- Team with highest number of green tests wins!
- Ready-to-use Docker image contains Swift 4, Vapor 2, SQLite, tmux, zsh…
- Based on Phusion Base Image
- Dockerfiles available for reference
- User
developer, passworddeveloper - Can be used interactively
- Local Desktop becomes image desktop
- Simple reverse forwarding for your API
- Provide me with the temporary URL for your API at the end of the exercise
- The API responds to a "ping"
- Anyone can create a new user in the system
- Logged-in users can create notes
- Default user:
- username "vapor@theswiftalps.com"
- password "swiftalps"
- Notes are identified using UUIDs
- Notes contain Markdown text
- Users can get all of their notes
- …get one single note
- …edit a single note
- …delete a single note
- …delete all notes at once
- …publish a note in HTML
- …unpublish notes
- …request a backup of their notes in ZIP format
- …search their notes for the occurrence of a string
- Swift package
- macOS & Linux
- Makefile for common tasks
- Dockerfiles available in
dockerproject for reference
---?code=Sources/Infrastructure/BaseScenario.swift&title=Assertions&lang=swift @[191-195](Asserting for "Content-Type" headers) @[204-208](Asserting for response status codes) @[231-233](Asserting for particular headers)
---?code=Sources/Scenarios/PingScenario.swift&title=Ping Scenario&lang=swift @[18-29](Example scenario)
---?code=Sources/Infrastructure/API.swift&title=API Wrapper&lang=swift @[37-48](Log in) @[29-31](Create user) @159-161
- GET & DELETE requests: 3 points
- POST & PUT requests: 5 points
- ZIP Backup: 10 points
Maximum Score: 152 Points
- Study
Sources/Scenarios&Sources/Infrastructure/API.swift - Start with the easiest possible endpoint:
ping - Vapor provides a lot!
- Use an in-memory SQLite database for storage
- ZIP archive is the most difficult part
- Middlewares!
vapor new Notes --template=apivapor buildvapor run servevapor xcodevapor clean
sudo docker run --interactive --tty --privileged --publish 80:8080 --user=developer --volume ~/Desktop:/home/developer/Desktop akosma/vapor zshdocker container listdocker images
ngrok http 80- Monitor interface: http://127.0.0.1:4040/
- Forwarding: http://xxxxxxxx.eu.ngrok.io/
-
curl http://localhost/ping -
curl http://localhost/api/v1/login --silent --user "vapor@theswiftalps.com:swiftalps" --request POST | jq -r .token | pbcopy
curl http://localhost/api/v1/notes --request POST --header "Authorization: Bearer SNjmXWikniZZLaed8jDG5A==" --header "Content-Type: application/json" --data @Fixtures/request_create_note.json
curl http://localhost/api/v1/notes/search --request POST --header "Authorization: Bearer FPG1Llqfl5Ju200ZDPb/ig==" --header "Content-Type: application/json" --data @Fixtures/request_search.json
---?code=Fixtures/request_create_note.json&title=JSON for creating notes&lang=json
---?code=Fixtures/response_files.json&title=Typical JSON response&lang=json
- Validator: https://github.com/TheSwiftAlps/Validator
- Vapor docs: https://docs.vapor.codes/2.0/
- Docker docs: https://docs.docker.com/
- ngrok docs: https://ngrok.com/docs
- ZIP Foundation: https://github.com/weichsel/ZIPFoundation
- Docker base image: http://phusion.github.io/baseimage-docker/
