Skip to main content

Development

Server Side Swift with Perfect: Getting Started

Perfect is an open source server side Swift framework, that help swift developers to build applications and other REST services. If we use Perfect to create a backend for an iOS application, it’s easy to stay consistent by sticking to one language and code style. In this tutorial, I will teach you to build a simple local server by Perfect.

A. Build a simple local server by Perfect

1.Use Terminal to create a project

  1. mkdir hello-perfect
  2. cd hello-perfect/
  3. swift package init –type executable
  4. swift package generate-xcodeproj
  5. open ./hello-perfect.xcodeproj/

2.Add a dependency Library

  1. Add the below red highlighted code in the Package.swift fileServer Side Swift with Perfect: Getting Started
  2. Enter the below two command lines in Terminal
    • swift package update
    • swift package generate-xcodeproj

     

  3. import PerfectLib, PerfectHttp and PerfectHttpServer in main.swift file to check whether it added the dependency successfully.

3.Create Http Server

  1. Use Terminal to create webroot folder and create hello.txt in this folder.
    • mkdir webroot
    • touch webroot/hello.txt
    • swift package generate-xcodeproj
  2. add below code in main.swift.                                           
  3. edit schemeServer Side Swift with Perfect: Getting Started
  4. Build&Run            Server Side Swift with Perfect: Getting Started
  5. Use web browser to visit http://localhost:8080/hello.txtServer Side Swift with Perfect: Getting Started

B.Simple Request Demo

1.A simple request

  1. Add below code in Main.swift file                                             Server Side Swift with Perfect: Getting Started
  2. Use the web browser to visit  http://localhost:8080Server Side Swift with Perfect: Getting Started

2.Return the requested data in JSON format

  1. Add below code in Main.swift fileServer Side Swift with Perfect: Getting Started
  2. Use web browser to visit http://localhost:8080/helloServer Side Swift with Perfect: Getting Started

3.Simple GET request to receive parameter

  1. Add GET request code in Main.swift fileServer Side Swift with Perfect: Getting Started
  2. Use web browser to visit http://localhost:8080/books/90Server Side Swift with Perfect: Getting Started

4.Simple POST request to receive parameters

  1. Add POST request code in Main.swift fileServer Side Swift with Perfect: Getting Started
  2. Use POST MAN to checkServer Side Swift with Perfect: Getting Started

Tags

Thoughts on “Server Side Swift with Perfect: Getting Started”

  1. Hi Charlie,

    I like the tutorial, but I am having an issue when starting with B. Request Demo. When I try to navigate to the routes I keep getting i.e. “/hello, file does not exist”. Do you have any idea on how to fix this?

    Thanks

    Daniel

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories
Follow Us