Skip to content

Storage for simple vertices inside table with Swift 5 and Vapor 4

License

Notifications You must be signed in to change notification settings

mkoshtenko/svit-service

Repository files navigation

Build Status codecov codebeat badge

Overview

Storage for simple vertices inside table

Dependencies

If Vapor Toolbox is already installed skip to step 3.

  1. Install homebrew, it will be used as package manager for Vapor toolbox.

  2. Install Vapor Toolbox, it requires Swift 4.1 or higher

brew tap vapor/tap
brew install vapor/tap/vapor
  1. Update Vapor Toolbox brew upgrade vapor

  2. Updates dependencies vapor update

If you are experiencing problems with Swift Package Manager, sometimes cleaning can help. vapor clean

Run tests

Locally using Xcode on Mac OS

Open Xcode vapor xcode -y

To run unit-tests, select Run scheme and hit Command+U.

In a Docker container

The following command builds docker container with tag svit-service-tests and executes unit-tests inside.

docker build -t svit-service-tests -f 'test.Dockerfile' .

Code-coverage report can be received by copying from the container with docker cp

containerId=$(docker create svit-service-tests)
docker cp "${containerId}":app.coverage.lcov .
docker rm "${containerId}"

How to build and run

Docker

Before start you can remove all cached data:

docker system prune -a

Create file with necessary environment variables.

Build environment example '.env':

SVIT_DB_PASSWORD=<db password>
SVIT_DB_USER=<db username>
SVIT_DB_PORT=54320
SVIT_DB_NAME=svit_db

Do not forget to replace values with the real data.

You can verify the environment with the config command, which prints resolved application config to the terminal:

docker-compose config

Run containers in the background:

docker-compose build
docker-compose up -d db
docker-compose up -d app

After these commands the service should be accessible at configured location (e.g. http://localhost:8080/health). The configuration can be changed in web.Dockerfile.

Using docker-compose ps, check the status of your services:

docker-compose ps
docker-compose stop

How to run a debug build

For development purposes might be necessary to run the service from the Xcode and play with the API. Since this will require the database service running, the following steps explain how it can be launched.

1. Run PostgreSQL container

Start a container with database instance:

docker run -d --name svit_db_postgres \
  -e POSTGRES_USER=svit_db_user \
  -e POSTGRES_DB=svit_db \
  -e POSTGRES_PASSWORD=password \
  -p 54320:5432 \
  postgres:12

The version of the container is 12, it is taken from https://hub.docker.com/_/postgres This will add a user svit_db_user and create the database svit_db. The database will be accessible via 54320 public port. These values are specified in PSQLFactory.swift as defaults.

Print and check the container is listed there: docker ps -a

To stop the db container run:

docker stop svit_db_postgres
docker rm svit_db_postgres

To run commands inside the container:

# run bash
docker exec -it svit_db_postgres bash

# or run psql
docker exec -it svit_db_postgres psql -d svit_db -U svit_db_user

2. Migrations

In vapor 4 db migration do not run automatically, to do so you need to execute: swift run Run migrate

Or, as an alternative way, it is also possible to run them from the xcode with migrate -y argument added to the scheme.

it takes configurations and migrations from configure.swift and will try to execute them.

3. Launch From the XCode

  1. Create xcodeproj from repo with vapor cli vapor xcode -y
  2. Choose Run scheme against the Mac machine
  3. Hit CMD+R

API usage examples

VERTEX

  • create: curl -H "Content-Type: application/json" -d '{"type":"test", "data":""}' -X POST http://localhost:8080/vertices

  • delete: curl -H "Content-Type: application/json" -X DELETE http://localhost:8080/vertices/3

  • update: curl -H "Content-Type: application/json" -d '{"data":"UPDATED"}' -X PATCH http://localhost:8080/vertices/1

RELATION

  • create: curl -H "Content-Type: application/json" -d '{"type":"implements", "from": 1, "to": 2, "data":""}' -X POST http://localhost:8080/relations

  • delete: curl -H "Content-Type: application/json" -X DELETE http://localhost:8080/relations/1

  • update: curl -H "Content-Type: application/json" -d '{"data":"{\"aa\":100}"}' -X PATCH http://localhost:8080/relations/6

COUNT

  • get: curl http://127.0.0.1:8080/count?from=1&type=implements

Links

Docker cheat-sheet: https://www.saltycrane.com/blog/2017/08/docker-cheat-sheet/

Vapor repo: https://github.com/vapor/vapor

About

Storage for simple vertices inside table with Swift 5 and Vapor 4

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published