{"id":13990727,"url":"https://github.com/jayfk/server-side-swift-starter","last_synced_at":"2025-07-22T13:31:13.097Z","repository":{"id":73239072,"uuid":"57131043","full_name":"jayfk/server-side-swift-starter","owner":"jayfk","description":"Server side swift starter template","archived":true,"fork":false,"pushed_at":"2016-04-28T07:47:32.000Z","size":187,"stargazers_count":33,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-09T13:17:41.356Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jayfk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-04-26T13:37:13.000Z","updated_at":"2023-01-28T15:51:26.000Z","dependencies_parsed_at":"2024-01-18T05:10:23.238Z","dependency_job_id":"5fd4de53-9ca7-4fcf-a6d6-b187427767bd","html_url":"https://github.com/jayfk/server-side-swift-starter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jayfk%2Fserver-side-swift-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jayfk%2Fserver-side-swift-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jayfk%2Fserver-side-swift-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jayfk%2Fserver-side-swift-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jayfk","download_url":"https://codeload.github.com/jayfk/server-side-swift-starter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227098971,"owners_count":17730687,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-09T13:03:09.521Z","updated_at":"2024-11-29T10:31:35.742Z","avatar_url":"https://github.com/jayfk.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"## Server Side Swift Starter\nA starter template to develop and deploy swift 3 applications with docker. \n\n### Features\n- Swift 3.0 (SNAPSHOT-2016-03-24-a)\n- Uses two environments for development and production.\n- Code autoreload during development\n- Kitura swift framework preinstalled (but can be replaced)\n- NGINX handles static files and acts as a reverse proxy for the swift application\n- Pre configured MongoDB database, comes with an admin interface to create/backup databases\n- Uses environment variables to store secrets\n\n### Screenshots\n\n![Hi from swift](https://dockify.io/content/images/2016/04/hi_from_swift.png)\n\n![Swift JSON](https://dockify.io/content/images/2016/04/json.png)\n\n![Admin Interface](https://dockify.io/content/images/2016/04/admin.png)\n\n![Mongo Express](https://dockify.io/content/images/2016/04/mongo-express.png)\n\n## Getting started\n### Prerequisite\n\nYou'll need to have \n\n- docker\n- docker-compose \u003e 1.6.0\n- docker-machine (optional)\n\ninstalled on your mac. \n\nThe easiest way to get them all is to download and install [docker-toolbox](https://www.docker.com/products/docker-toolbox). \nIf you are completely new to docker, check out the [getting started with docker](https://docs.docker.com/mac/) guide.\n\nMake sure you can run docker commands like `docker run hello-world` on your terminal, or use the docker quickstart terminal.\n\n### Installation\n\nClone the template in a new directory (make sure to replace `NewProject` with your own project name) \n    \n    git clone https://github.com/jayfk/server-side-swift-starter.git NewProject\n    cd NewProject/\n    \nRemove all upstream git files, we don't need them.\n\n    rm -rf .git/\n\nChange the defaults with sed (make sure to replace the values in brackets, e.g `\u003cuser\u003e` with `jayfk`)\n\n    sed -i.tmp s/#ADMIN_USER#/\u003cuser\u003e/g env/admin.env\n    sed -i.tmp s/#ADMIN_PASSWORD#/\u003cpassword\u003e/g env/admin.env\n    sed -i.tmp s/#PROJECT_NAME#/\u003cNewProject\u003e/g dockerfiles/swift/autoreload.sh dockerfiles/swift/Dockerfile Package.swift\n    find . -type f -name \\*.tmp -exec rm -f {} \\;\n\n## Developing locally\n\nWe are going to use docker to run our swift code during development and (later) in production. This\nhas a couple of huge benefits and a few drawbacks, but the most important thing is that we have consistent\nenvironments.\n\nFirst, build and start the stack with\n\n    docker-compose -f dev.yml up\n    \nThis will take *forever* on the first start. Docker has to pull all base images and has\nto build our own images in `dev.yml`. This includes a full swift runtime linked to Foundation and the MongoDB\ndriver to name a few. Subsequent builds will be super fast, thanks to dockers caching.\n\nWhile you wait, you can check out the [docker-compose getting started](https://docs.docker.com/compose/gettingstarted/) \nsection in dockers docs. Our development environment is defined in `dev.yml` and uses the \ndockerfiles `dockerfiles/swift/Dockerfile-dev` and `dockerfiles/nginx/Dockerfiles`. This will give \nyou a quick overview of what's taking so long.\n\nAs soon as your terminal stops spitting out text, you should see an output similiar to this\n\n```\nswift_1          | Compiling Swift Module 'LoggerAPI' (1 sources)\nswift_1          | Compiling Swift Module 'KituraTemplateEngine' (1 sources)\nswift_1          | Compiling Swift Module 'Socket' (3 sources)\nswift_1          | Compiling Swift Module 'SwiftyJSON' (2 sources)\nswift_1          | Compiling Swift Module 'BinaryJSON' (9 sources)\nswift_1          | Compiling Swift Module 'KituraSys' (4 sources)\nswift_1          | Compiling Swift Module 'MongoDB' (7 sources)\nswift_1          | Compiling Swift Module 'KituraNet' (12 sources)\nswift_1          | Compiling Swift Module 'Kitura' (13 sources)\nswift_1          | Compiling Swift Module 'NewProject' (1 sources)\nswift_1          | Linking .build/debug/NewProject\nswift_1          | Setting up watches.  Beware: since -r was given, this may take a while!\nswift_1          | Watches established.\n```\n\nOpen up your browser with\n\n    open \"http://$(docker-machine ip dev)\"\n\nThe code powering the application is in `Sources/main.swift`. Change the html string and hit save, you'll see\nthat the application is rebuilt automatically.\n\n```\nrebuild code here\n```\n\nRefresh your browser and you should see your changes.\n\n*Pro tip: Add the IP `docker-machine ip dev` is putting out to `/etc/hosts` under the \n`docker.local` domain. For example: add a new line with `192.168.99.100 docker.local`. You will now\nbe able to connect to your app at http://docker.local/*\n\n\n### Where to go from here\n\n - Take a look at the admin interface to create and backup databases at `http://your-ip/admin/`\n\n - The code in `Sources/main.swift` uses the Kitura framework. For more examples, take a look at the \n[Kitura example project](https://github.com/IBM-Swift/Kitura-Sample/blob/master/Sources/KituraSample/main.swift).\n\n - You can use whatever framework you like (or code your own!). Just make sure your code starts some \nkind of server and speaks HTTP on port 8090.\n\n - Dependencies are pulled in automatically using the [swift package manager](https://github.com/apple/swift-package-manager). \n If you want to add a new dependency, add it to `Package.swift`.\n \n - [Create an Xcode Project](#creating-an-xcode-project)\n    \n    \n## Deploying to production\n\nNote: In this example we are going to use docker-machine to create a remote server on DigitalOcean\nand install docker on it. The cool thing about docker-machine is that it works with a lot of \n[cloud providers](https://docs.docker.com/machine/drivers/) out of the box. I've found DigitalOcean\nto be the easiest to get started, but if you are a fan of AWS/Microsoft Azure/Google Compute \nEngine or whatnot, just use them.\n\n### Creating a Droplet with Docker Machine\n\nYou need an API access token to create a new server. Let's get one:\n\n- Go to the Digitalocean [Console](https://cloud.digitalocean.com/login) and log in.\n- Click on **API** in the header.\n- Click on **Generate new token**\n- Give the token a unique name, e.g *docker-machine*. Make sure that the write checkbox is checked.\n Otherwise docker-machine won't be able to create a new droplet.\n- Click on **Generate Token**\n- Copy the newly generated token and store it somewhere safe.\n\n![DigitalOcean admin console](https://dockify.io/content/images/2016/04/digitalocean-api.png)\n\nNow, let's create the machine:\n  \n    docker-machine create swiftapp --driver=digitalocean --digitalocean-region=nyc3 --digitalocean-size=512mb --digitalocean-access-token=YOUR_TOKEN\n\nThis will create a new 512MB Droplet in New York City 3 with the name *swiftapp*. If you want to \ncreate a Droplet of a different size or in a different datacenter, make sure to adjust \n`--digitalocean-size` and `--digitalocean-region` to your needs.\n\nThe command takes a couple of minutes to finish, the output should be similiar to this: \n\n\n```\nRunning pre-create checks...\nCreating machine...\n(swiftapp) Creating SSH key...\n(swiftapp) Creating Digital Ocean droplet...\n(swiftapp) Waiting for IP address to be assigned to the Droplet...\nWaiting for machine to be running, this may take a few minutes...\nDetecting operating system of created instance...\nWaiting for SSH to be available...\nDetecting the provisioner...\nProvisioning with ubuntu(systemd)...\nInstalling Docker...\nCopying certs to the local machine directory...\nCopying certs to the remote machine...\nSetting Docker configuration on the remote daemon...\nChecking connection to Docker...\nDocker is up and running!\nTo see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env swiftapp\n```\n\n    \n*If you get any errors, remove the machine by running `docker-machine rm -f swiftapp`. If this \nalso produces errors, you can always delete the Droplet using the Digitalocean web console as a \nfallback.*\n\n### First Production Deployment\n\nSelect your production server with\n\n    eval $(docker-machine env swiftapp)\n\nThis will set a couple of environment variables telling the docker client on our machine to talk\ndirectly to the docker deamon on our server.\n\nNow, we can start our stack on the server with\n    \n    docker-compose up -d\n\nDon't worry, this will take a couple of minutes the first time. The docker deamon has to fetch all\n the images and install all the low level dependencies first. Subsequent builds will be a lot \n faster since the image build process will be cached on the server. \n \nIf you are bored, you can connect to the server and and launch `htop` to see what's happening \nunder the hood.\n\n    docker-machine ssh swiftapp\n    apt-get install htop \u0026\u0026 htop \n\nOnce ready, check out your shiny swift powered web app by running\n\n    open \"http://$(docker-machine ip swiftapp)\"\n    \n\n### Pushing new code\n\nIf you want to push new code to the server, you'll need to rebuild the image, create a new container and restart the running container. \n\n    docker-compose build swift\n    docker-compose create swift\n    docker-compose stop swift\n    docker-compose start swift\n\n## Useful things\n\n### Creating an Xcode Project\n\nTo Create an Xcode project and open it (make sure to replace `NewProject` with your own project name)\n    \n    docker-compose -f dev.yml run swift swift build --generate-xcodeproj .\n    open NewProject.xcodeproj\n    \nThe generated project is mediocre at best. You'll probably see a lot of compile errors. To fix \n(most of) them, download and install the [March 24, 2016](https://swift.org/builds/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2016-03-24-a/swift-DEVELOPMENT-SNAPSHOT-2016-03-24-a-osx.pkg)\ndevelopment snapshot and change the toolchain in Xcode at `Preferences` \u003e `Components` \u003e `Toolchains`.\n\n![Xcode Toolchains](https://dockify.io/content/images/2016/04/xcode_toolchains.png)\n\nIn order to get code highlighting and resolution to work, you need to select the modules manually.\nIn Xcode, locate your `main.swift` file at `Sources/main.swift` and select every imported module under\n*Target Membership*. You'll still see *No such module* warnings, but the highlighting/resolution will \nwork.\n\n![Xcode dependencies](https://dockify.io/content/images/2016/04/xcode_dependencies.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjayfk%2Fserver-side-swift-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjayfk%2Fserver-side-swift-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjayfk%2Fserver-side-swift-starter/lists"}