Create a server-side app in Swift

Gabriel Mazzei
Apr 29 · 6 min read


/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew tap vapor/homebrew-tap
brew update
brew install vapor

vapor new woloxusers

cd woloxusers/
vapor update -y

woloxusers/
├── Package.swift
├── Sources/
│ ├── App/
│ │ ├── Controllers/
│ │ │ └── TodoController.swift
│ │ ├── Models/
│ │ │ └── Todo.swift
│ │ ├── app.swift
│ │ ├── boot.swift
│ │ ├── configure.swift
│ │ └── routes.swift
│ └── Run/
│ └── main.swift
├── Tests/
├── Public/
├── Dependencies/
└── Products/
woloxusers/
├── Package.swift
├── Sources/
│ ├── App/
│ │ ├── Controllers/
│ │ │ └── TodoController.swift
│ │ ├── Models/
│ │ │ └── Todo.swift
│ │ ├── app.swift
│ │ ├── boot.swift
│ │ ├── configure.swift
│ │ └── routes.swift
│ └── Run/
│ └── main.swift
├── Tests/
├── Public/
├── Dependencies/
└── Products/




curl -H "Content-Type: application/json" \
-X POST \
-d '{"username": "Leonardo"}' \
http://localhost:8080/users
curl -H "Content-Type: application/json" \
-X POST \
-d '{"username": "Michelangelo"}' \
http://localhost:8080/users
curl http://localhost:8080/users
curl -H "Content-Type: application/json" \
-X PATCH \
-d '{"username": "Raphael"}' \
http://localhost:8080/users/1
curl -H "Content-Type: application/json" \
-X DELETE \
http://localhost:8080/users/1

Connect your app to a PostgreSQL database

brew install postgres
brew services start postgresql
createuser postgres -s
createdb woloxusers -O postgres
vapor update -y

Host your app on Heroku

brew tap heroku/brew && brew install heroku
heroku login
heroku create projectname
heroku buildpacks:set vapor/vapor
web: Run serve --env production --port $PORT --hostname 0.0.0.0
git add .
git commit -m "Wolox-users app"
vapor heroku push
curl https://projectname.herokuapp.com/users
curl -H "Content-Type: application/json" \
-X POST \
-d '{"username": "Leonardo"}' \
https://projectname.herokuapp.com/users
curl -H "Content-Type: application/json" \
-X PATCH \
-d '{"username": "Raphael"}' \
https://projectname.herokuapp.com/users/1
curl -H "Content-Type: application/json" \
-X DELETE \
https://projectname.herokuapp.com/users/1

Troubleshooting

# Uninstall postgres
brew uninstall postgresql
brew doctor
brew cleanup
# Remove remaining folders
rm -r /usr/local/var/postgres
rm -r /Users/<username>/Library/Application\ Support/Postgres
# Install the last version
brew install postgresql
# Start the process
brew services start postgresql

Wolox

Wolox stands for innovation, engineering and working culture that transforms problems into solutions and ideas into products. www.wolox.com.ar

Thanks to Romina Blasucci.

Gabriel Mazzei

Written by

Wolox

Wolox

Wolox stands for innovation, engineering and working culture that transforms problems into solutions and ideas into products. www.wolox.com.ar