/yachtsServer

yachtsServer

Primary LanguageSwift

Evaluation demo and prototype for sharing swift server side code modules with native iOS clients.

##Abstract

Open source Swift support on server machines makes it possible for developers to use common code modules on both the server and in native iOS app clients. This project explores how sharing code modules between the server and the client may accelerate development, improve reliability and/or reduce the overall cost of delivery and maintenance for swift iPhone apps.

JavaScript and Java Android developers have some great examples on taking advantage of this such as how airbnb leverages Isomorphic JavaScript using nodejs to improve their application delivery.

This app shares kitura web API server side model structs and their JSON and dictionary serializers with the native iOS mobile app using Swift Package Manager (SPM) and Cathage to insure API model dependencies via semantic versioning.

Use the Building and running with Docker to quick start the API server infastructure and then build and run the companion mobile iPhone app to utilize the API on local host.

##Building and running with docker compose

###Prerequisites

###Running with docker compose

  1. Clone the repo git clone git@github.com:mschmulen/yachtsServer && cd yachtsServer
  2. Copy example.env to .env with cp example.env .env
  3. Init and run with docker-compose up
  4. Intialize the data store with Tools/seed_couchdb.sh you can modify the seed data from the json doc files Tools/yachts.json
  5. Verify with open http://localhost:8090/ and http://localhost:8090/yachts
  6. Run the iOS app https://github.com/mschmulen/yachtsApp

##Building and running local OSX configuration

###Prerequisites

###Building and running the server with Xcode or local SPM ( Swift Package Manager)

Building and running with command line SPM (Swift Package Manager)

  1. Clone the repo git clone git@github.com:mschmulen/yachtsServer && cd yachtsServer
  2. Build swift build
  3. Insure the couchdb data store is running local or via a local docker host
  4. Seed the couchDB datastore Tools/seed_couchdb.sh --username=matt --password=123456 (verify your local credentials)
  5. Run the server with .build/debug/yachtsServer

Building and running with Xcode

  1. Clone the repo git clone git@github.com:mschmulen/yachtsServer && cd yachtsServer
  2. Generating the Xcode project swift package generate-xcodeproj
  3. Open in Xcode open yachtsServer.xcodeproj
  4. Insure the couchdb data store is running local or via a local docker host
  5. Seed the couchDB datastore Tools/seed_couchdb.sh --username=matt --password=123456 (verify your local credentials)
  6. Make sure and select the cmd line target then build and run with ⌘ + r

Verify the server is running ttp://localhost:8090 and [http://localhost:8090/yachts])http://localhost:8090/yachts

##Run the companion iOS app

Follow the instructions on the Companion iOS App https://github.com/mschmulen/yachtsApp


##Misc Docker commands

  • show images: docker images
  • show images: docker ps -a
  • run with interactive and privlileged: docker run --privileged -i -t --name yachts-swift-server mschmulen/swift-server:latest /bin/bash
  • start a docker image: docker start yachts-swift-server
  • attach to a running image: docker attach yachts-swift-server
  • Clean up dangling images docker rmi $(docker images -f "dangling=true" -q)
  • One liner to stop all of Docker containers: docker stop $(docker ps -a -q)

##Special Thanks !

Special thanks too erutherford, jessecravens, thedodd, sharplet, kristenfelch, and especially rlwimi for taking the time to review, provide thier expert advice on the development and goals this prototype project.

Thanks to insanoid for his work on SwiftyJSONAccelerator, its a great tool for creating Swift model structs from JSON stubs.

The Kitura Swift web and HTTP server framework https://github.com/IBM-Swift/Kitura project.


##Deploying to a provider WIP

####DigitalOcean (usering docker-machine) :

Prerequisites:

Getting up and running:

  1. Create the droplet: docker-machine create --driver digitalocean --digitalocean-access-token [TOKEN] yachtsServer
  2. Get the environment: docker-machine env yachtsServer
  3. Configure the environment: eval $(docker-machine env yachtsServer)
  4. Build the image: docker build -t swift-server .
  5. Run the image on the host: docker run --name webserver -p 80:8090 swift-server . The -p option is used to expose port 80 from the nginx container and make it accessible on port 8090 of the swift-server-image host

Docker machine common commands:

  • Get the ip address: docker-machine ip yachtsServer
  • Inspect: docker-machine inspect yachtsServer
  • Show docker machines: docker-machine ls
  • Connect to the docker machine: docker-machine ssh yachtsServer
  • Stop: docker-machine stop yachtsServer
  • Remove: docker-machine rm yachtServer

####Deploying via ibm bluemix:

note bluemix does not have a freemium offer and you will be charged mercilessly per minute

  1. create an account at ibm bluemix
  2. cf login -a api.ng.bluemix.net
  3. cf push "yachts"

delete your app with : cf delete "yachts"

###Misc References

Docker and Provider references:

An Ubuntu 16.04 Docker image for Swift https://github.com/swiftdocker/docker-swift. Run with docker run --privileged -i -t --name swiftfun swiftdocker/swift:latest /bin/bash

Digital ocean Docker https://docs.docker.com/machine/examples/ocean/

Digital Ocean Server Side Swift https://medium.com/@LarsJK/easy-server-side-swift-with-docker-4c297feeeeb5#.1g5i8ilmq and corresponding github repo https://github.com/serversideswift/swift-docker/blob/master/Dockerfile

IBM Swift docker examples https://github.com/IBM-MIL/Samples

Docker hub: twostraws Swift Server https://hub.docker.com/r/twostraws/server-side-swift/ docker file reference

IBM Simple swift docker configuration https://developer.ibm.com/swift/2015/12/15/running-swift-within-docker/

Couch DB Docker file reference https://github.com/apache/couchdb-docker/blob/master/2.0-dev-docs/Dockerfile

Code Sharing between server and client:

How to Implement Node + React Isomorphic JavaScript & Why it Matters , StrongLoop : https://strongloop.com/strongblog/node-js-react-isomorphic-javascript-why-it-matters/

Isomorphic JavaScript: The Future of Web Apps , airbnb : http://nerds.airbnb.com/isomorphic-javascript-future-web-apps/

https://blog.algolia.com/modern-javascript-libraries-the-isomorphic-way/

Share POJO Entity Data Classes between Android and Spring projects http://stackoverflow.com/questions/31566303/share-pojo-entity-data-classes-between-android-and-spring-projects

Swift:

OSX app and CLI to generate Swift 3 code for models from JSON. https://github.com/insanoid/SwiftyJSONAccelerator

An implementation (validator) of JSON Schema in Swift https://github.com/kylef/JSONSchema.swift

Scheme generator in ruby https://github.com/maxlinc/json-schema-generator

Scheme generator in scala https://github.com/VoxSupplyChain/json-schema-codegen

Data Modeling Framework for JSON https://github.com/jsonmodel/jsonmodel

Embedded web server for iOS UI testing https://envoy.engineering/embedded-web-server-for-ios-ui-testing-8ff3cef513df#.srl7p1x5p

Scheme management:

Swagger integrations http://swagger.io/open-source-integrations/

Swagger swift gen https://github.com/yonaskolb/SwagGen

api blueprint https://apiblueprint.org/tools.html


###Misc Notes (the Attic)

####TODO

  • push to Docker Hub repo so it can be run via docker-compose reference

####SwiftyJSONAccelerator

Install

  1. git clone https://github.com/insanoid/SwiftyJSONAccelerator.git && cd SwiftyJSONAccelerator
  2. make install
  3. cd ..
  4. rm -rf SwiftyJSONAccelerator

Run using .config.json

  1. generate: swiftyjsonaccelerator generate -p Definitions
  2. from the Definitions folder cd ~/yachtsServer/Definitions
  3. generate: swiftyjsonaccelerator generate

note inject import SwiftyJSON into Model objects


####run the docker hub twostraws/server-side-swift image

  1. mkdir docker-server
  2. cd docker-server
  3. docker run -itv $(pwd):/swiftServer --name swiftServer -w /swiftServer -p 8089:8089 -p 8090:8090 -p 5984:5984 twostraws/server-side-swift /bin/bash

run the docker container locally

  1. from the yachtsServer repo : cd yachtsServer
  2. build the image: docker build -t yachts-swift-server .
  3. verify the image was build: docker images
  4. run the docker image with: docker run -it --name webserver -p 8090:8090 -p 5984:5984 yachts-swift-server this will name the service ‘webserver’, replace the -p 80:8090 to make the image’s port 8090 available on the hosts port 80.
  5. open a web page http://localhost:8090/ to verify the server is runing pr http://localhost:8090/yachts to verify the API is working

you can verfy swift was configured correctly with docker run -it yachts-swift-server /bin/bash and swift --version from the interactive terminal

####local docker workflow

After building the yachts-swift-server image (instructions above):

  1. terminal to the container docker run -it yachts-swift-server /bin/bash
  2. pwd to confirm SwiftServer folder
  3. clean and build rm -rf .build/ && swift build
  4. run the server from the terminal with .build/debug/yachtsServer

####Configuring CouchDB

  1. Install CouchDB : http://couchdb.apache.org/#download
  2. Verify CouchDB is running open http://127.0.0.1:5984/_utils/ , http://localhost:5984/, or curl -X GET http://localhost:5984
  3. initialize with the script Tools/initDB

from within the docker container

  • start the couchdb server from within the docker container with /etc/init.d/couchdb start
  • seed couchdb Tools/seed_couchdb.sh --username=matt --password=123456
  • build ad run the yacht-server from within the docker container with swift build && .build/debug/yachtsServer

misc commands

  • run and attach: docker start -i swiftServer