A dockerized microservice written in Swift using Vapor.

Overview

price-calculation-service-swift

This is an example project for a university project. It uses Vapor to serve a microservice written in Swift.

The point of this assignment is to serve a microservice with Docker.

Unit Tests + Integration Tests are included in the Tests Folder.

How to use this API

This API offers three endpoints.

/alive endpoint

returns "Alive" to verify the service is running.

/price endpoint

returns the calculation of the requested product.

Valid URL parameters:

  • productId required - the UUID of the requested product (see /Sources/App/Resources/products.json for reference)
  • quantity required - the requested quantity of this product. Int
  • coupon optional - either FIVE or TEN to get 5% or 10% discount

/products endpoint

returns a list of all available products.

The 12-Factor App

Part of this assignment was to fullfill at least eight of the twelve factors described in The Twelve-Factor App.

1. Codebase

One codebase tracked in revision control, many deploys

The code and its history is tracked using git on GitHub. Deploys can be tracked using a branching model, e.g. development / staging / main to push to dev / staging / prod environments.

2. Dependencies

Explicitly declare and isolate dependencies

The app's dependencies are explicitly declared in the Package.swift file, where the Swift Package Manager handles the dependencies.

3. Config

Store config in the environment

Environment configurations for dev / staging / prod should not be put in code, but handled via configuration files that can be loaded into the environment. This can be handled with Vapor by using .env files injected into the environment. See here for reference. Per default a template .env file is part of the git repository. Environment files should not be committed into git, because they can contain secrets such as passwords.

4. Backing services ☑️

Treat backing services as attached resources

Without changing code, backing services (e.g. databases, other APIs) can be exchanged. Since this example does not rely on other backing services, it is not implemented.

5. Build, release, run

Strictly separate build and run stages

build, release and run phases are separated. Therefore it is not possible to change code at runtime. For a real microservice, a CI/CD service could be used to allow for automatic build, release and run integration.

6. Processes

Execute the app as one or more stateless processes

The app is run using a stateless, isolated docker container.

7. Port binding

Export services via port binding

The app is run by port binding the 8080 port to the docker container.

8. Concurrency ☑️

Scale out via the process model

New with Swift 5.5 it enables concurrency via the new async/await API. Vapor supports the new concurrency features.

9. Disposability

Maximize robustness with fast startup and graceful shutdown

Using docker, starting / stopping a container is as easy as a one line bash command.

10. Dev/prod parity ☑️

Keep development, staging, and production as similar as possible

For this example project no multiple releases to different environments were made. However, since configuration is done via .env files, the docker image could easily be run on different environments.

11. Logs

Treat logs as event streams

This behavior is built into Vapor. Logs can be handled differently depending on the environment.

12. Admin processes ☑️

Run admin/management tasks as one-off processes

For this project, no one-time processes were needed just yet. However, if database migrations or other one-time scripts were needed, they would have to be considered.

Build Docker Image

For reference see Vapor Documention.

to build the docker image, simply run: (this might take a while)

docker-compose build

to list all built docker images on your maschine, run:

docker image ls

to run the docker container, run:

docker run -dp 8080:8080 price-calculation-service-swift

now visit localhost:8080/alive.

to stop the docker container, run:

docker stop {CONTAINER ID}

License

MIT

You might also like...
Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

Simple server APIs in Swift

Simple server APIs in Swift

Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.

Why Zewo? • Support • Community • Contributing Zewo Zewo is a lightweight library for web applications in Swift. What sets Zewo apart? Zewo is not a w

💧 A server-side Swift HTTP web framework.
💧 A server-side Swift HTTP web framework.

Vapor is an HTTP web framework for Swift. It provides a beautifully expressive and easy-to-use foundation for your next website, API, or cloud project

Sinatra-like DSL for developing web apps in Swift

Swiftra Swiftra is a library that provides DSLs like Sinatra. System Requirements DEVELOPMENT-SNAPSHOT-2016-02-08-a Example See swiftra-example. impor

A minimal, fast and unopinionated web framework for Swift

![Fire Image] (http://i.imgur.com/1qR6Nl4.png) Blackfire An extremely fast Swift web framework 🔥 Getting Started If you're familiar with express.js t

HTTP Implementation for Swift on Linux and Mac OS X

Swift HTTP Server Simple HTTP implementation for Swift using POSIX socket API. Running on Mac OS X and Linux. For Mac users: You can install new Swift

libuv base Swift web HTTP server framework

Notice Trevi now open a Trevi Community. Yoseob/Trevi project split up into respective Trevi, lime, middlewares and sys packages at our community. If

A lightweight library for writing HTTP web servers with Swift

Taylor Disclaimer: Not actively working on it anymore. You can check out some alternatives Swift 2.0 required. Working with Xcode 7.1. Disclaimer: It

Owner
Fabian Geistert
Fabian Geistert
Auto Migration generation for Vapor projects

AutoMigrator A package that generates version based migrations from Fluent.Model types. The project will check your database scheme and understand whe

Mats Eikeland Mollestad 9 Jun 18, 2022
A template to get vapor working on Google App Engine

Swift Vapor App Engine This is a template to get vapor working on Google App Engine. Really all that's special is that it has a Dockerfile (included w

Michael 2 Oct 10, 2022
Test project to reproduce a lockup in Vapor

Test project to reproduce a lockup in Vapor The issue was first observed when backing up the SPI database during operation. Running pg_dump would caus

Sven A. Schmidt 1 Dec 30, 2021
Swift Express is a simple, yet unopinionated web application server written in Swift

Documentation <h5 align="right"><a href="http://demo.swiftexpress.io/">Live ?? server running Demo <img src="https://cdn0.iconfinder.com/data/icons/

Crossroad Labs 850 Dec 2, 2022
Tiny http server engine written in Swift programming language.

What is Swifter? Tiny http server engine written in Swift programming language. Branches * stable - lands on CocoaPods and others. Supports the latest

null 3.6k Dec 31, 2022
A light-weight server-side service framework written in the Swift programming language.

Smoke Framework The Smoke Framework is a light-weight server-side service framework written in Swift and using SwiftNIO for its networking layer by de

Amazon 1.4k Dec 22, 2022
Tiny http server engine written in Swift programming language.

What is Swifter? Tiny http server engine written in Swift programming language. Branches * stable - lands on CocoaPods and others. Supports the latest

null 3.6k Jan 5, 2023
Swift HTTP server using the pre-fork worker model

Curassow Curassow is a Swift Nest HTTP Server. It uses the pre-fork worker model and it's similar to Python's Gunicorn and Ruby's Unicorn. It exposes

Kyle Fuller Archive 397 Oct 30, 2022
PillowTalk - An iOS & SwiftUI server monitor tool for linux based machines using remote proc file system with script execution.

An iOS & SwiftUI server monitor tool for linux based machines using remote proc file system with script execution.

Lakr Aream 416 Dec 16, 2022
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)

Perfect: Server-Side Swift 简体中文 Perfect: Server-Side Swift Perfect is a complete and powerful toolbox, framework, and application server for Linux, iO

PerfectlySoft Inc. 13.9k Dec 29, 2022