appculture / LoyaltyBackend-SwiftVapor

Swift Vapor backend solution for the 1st appculture hackathon

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Loyalty Backend Hackathon

Swift Vapor backend solution for the 1st appculture hackathon

Brought to you by

This is what happens when bunch of mobile devs gets to work on a backend hackathon:

  • iOS team chose to use Swift (everybody laughed)
  • iOS team won (it was very close and totally unexpected)

As you can guess from title, the goal was to make a simple loyalty backend solution with some admin frontend and a client mobile app, you can see more about scope here.

Our team choose to use relatively new Swift web framework called Vapor.
While it was hard to keep up with very rapid development of Vapor (we had bad timing for this hackathon - just before Swift 3 is out of beta), we must say we were delighted with it, but also with all the help we got on Slack from the guys who are making it.

We decided to publish this repo for all the people that are learning to use Vapor.
Commit f3bafb1 won on hackathon, later we just added some simple authentication and refactored code a little bit. Keep in mind that we're primarly mobile developers (iOS Team speaking), but you can probably find some currently undocumented Vapor stuff in this project that may help you (made with Vapor version 0.16.2).

You can see our admin frontend on Heroku, and our API is added as a paw file inside project.

P.S. Please replace Config/mysql.json with Config/mysql-local.json if you wanna try something 'risky'. :)

Below is a default Vapor readme content, cheers! 🍻

Vapor Example

Fork this example project as a boilerplate for working with Vapor.

Check out the live demo running on Ubuntu.

Badges

Build Status PRs Welcome Slack Status

Deploy

Deploy

Documentation

View Vapor for documentation.

Requirements

Swift 3.0 preview 2 is required (Xcode 8 beta 2 on macOS).

Works on Ubuntu, Docker, Heroku, macOS.

Run the following script to check if you have Swift 3.0 beta 2 properly installed and configured.

curl -sL check.qutheory.io | bash

Building

Visit Getting Started in the documentation.

Compiling

If you have the Vapor Toolbox, use vapor new <project-name> to create your new application.

Then run vapor build and vapor run.

Otherwise, clone this repo and run swift build to compile your application, then run .build/debug/App.

Xcode 8

Run vapor xcode which will create the Xcode Project and open Xcode 8.

Xcode

Deploying

Check the Vapor documentation for more in-depth deployment instructions.

Upstart

To start your Vapor site automatically when the server is booted, add this file to your server.

You can check if Upstart is installed with

initctl --version

You may need to install Upstart if it is not already on your installation of Linux.

sudo apt-get install upstart

/etc/init/vapor-example.conf

description "Vapor Example"

start on startup

env PORT=8080

exec /home/<user_name>/vapor-example/.build/release/App --env=production

You additionally have access to the following commands for starting and stopping your server.

sudo stop vapor-example
sudo start vapor-example

The following script is useful for upgrading your website.

git pull
swift build --configuration release
sudo stop vapor-example
sudo start vapor-example

Heroku

Use the vapor heroku commands in the Vapor Toolbox to push to Heroku.

Docker

You can run this demo application locally in a Linux environment using Docker.

Make sure you have installed the Vapor Toolbox.

  1. Ensure Docker is installed on your local machine.
  2. Start the Docker terminal
  3. cd into vapor-example
  4. Create the Dockerfile vapor docker init
  5. Build the container vapor docker build
  6. Run the container vapor docker run
  7. Optionally enter the container vapor docker enter
  8. Configure VirtualBox to forward ports 8080 to 8080
  9. Visit http://0.0.0.0:8080

Nginx / Supervisor

You can also run your Vapor app through Nginx. It’s recommended you use Supervisor to run the app instance to protect against crashes and ensure it’s always running.

Supervisor

To setup Vapor running through Supervisor, follow these steps:

apt-get install -y supervisor

Edit the config below to match your environment and place it in /etc/supervisor/conf.d/your-app.conf:

[program:your-app]
command=/path/to/app/.build/release/App serve --ip=127.0.0.1 --port=8080
directory=/path/to/app
user=www-data
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log

Now register the app with Supervisor and start it up:

supervisorctl reread
supervisorctl add your-app
supervisorctl start your-app # `add` may have auto-started, so disregard an “already started” error here

Nginx

With the app now running via Supervisor, you can use this sample nginx config to proxy it through Nginx:

server {
	server_name your.host;
	listen 80;

	root /path/to/app/Public;

	# Serve all public/static files via nginx and then fallback to Vapor for the rest
	try_files $uri @proxy;

	location @proxy {
		# Make sure the port here matches the port in your Supervisor config
		proxy_pass http://127.0.0.1:8080;
		proxy_pass_header Server;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_connect_timeout 3s;
		proxy_read_timeout 10s;
	}
}

About

Swift Vapor backend solution for the 1st appculture hackathon

License:MIT License


Languages

Language:Swift 74.6%Language:HTML 20.3%Language:JavaScript 3.1%Language:CSS 2.0%