Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a new Perfect Project from scratch (Swift server) in xcode?

Perfect is a new Swift Framework for creating a web/http server in swift. The documentation is not there yet and I find trouble with building a new project from scratch. I don't know which frameworks are necessary to import and which one is the entry point of the app. main.swift etc...

I'd like to make a new xcworkspace that will have my project, "a hello world server".

Problems I'm trying to tackle:

  • Which frameworks must be included?
  • How should I create a Perfect server, what's the entry point of the app?
  • How to create a "hello" root which responds with a "Hello World message"?
  • How should I make the target for the server and eventually run the server?
like image 932
Alexandros Spyropoulos Avatar asked Jan 30 '16 17:01

Alexandros Spyropoulos


People also ask

How do I create a swift project in Xcode?

To create a new Swift package, open Xcode and select File > New > Swift Package. Choose a name and select a file location. Select “Create Git repository on my Mac” to put your package under version control. On completion, the Swift package opens in Xcode and looks similar to a standard Xcode project.

How do I open a project in Xcode?

Importing into XcodeOpen Xcode and select Open Another Project or File, Open. Open the folder you unzipped from your Dropsource download and locate the file with “. xcodeproj” extension in the root directory. Select the file and click Open.

What are frameworks in Xcode?

A framework is a hierarchical directory that encapsulates shared resources, such as a dynamic shared library, nib files, image files, localized strings, header files, and reference documentation in a single package. Multiple applications can use all of these resources simultaneously.


2 Answers

I managed to write a "Hello World" guide about this. http://code-me-dirty.blogspot.co.uk/2016/02/creating-perfect-swift-server.html

In a nutshell you need to proceed like this:

  1. clone the original project

  2. Create a new Workspace

  3. Create a new Project

  4. Import PerfectLib.xcodeproject & Import PerfectServer.xcodeproject but do not copy

  5. Setup your project scheme to launch the PerfectServer HTTP App

  6. Link the PerfectLib onn the "Linked Frameworks and Libraries" section

  7. setup Build settings for your framework target*

  8. Create PerfectHandlers.swift and paste(better write to get the feeling) the following code

    import PerfectLib
    //public method that is being called by the server framework to initialise your module.
    public func PerfectServerModuleInit() {
    
        // Install the built-in routing handler.
        // Using this system is optional and you could install your own system if desired.
        Routing.Handler.registerGlobally()
    
        // Create Routes
        Routing.Routes["GET", ["/", "index.html"] ] = { (_:WebResponse) in return IndexHandler() }
    
        // Check the console to see the logical structure of what was installed.
        print("\(Routing.Routes.description)")
    }
    
    //Create a handler for index Route
    class IndexHandler: RequestHandler {
    
        func handleRequest(request: WebRequest, response: WebResponse) {
            response.appendBodyString("Hello World")
            response.requestCompletedCallback()
        }
    }
    

Then you are ready to run. On my blog I have a long, more detailed version of this and I will update here if necessary.

Build Settings

  • Deployment Location: Yes
  • Installation Build Products Location : $(CONFIGURATION_BUILD_DIR)
  • Installation Directory : /PerfectLibraries
  • Skip Install : NO
like image 131
Alexandros Spyropoulos Avatar answered Oct 26 '22 04:10

Alexandros Spyropoulos


I just wrote up a tutorial I want to share as another solution that outlines how to create a web service with Perfect and an app to interact with it.

http://chrismanahan.com/creating-a-web-service-swift-perfect

Summary

  1. You must have your project in a workspace. This workspace should also include the PerfectServer and PerfectLib projects.

workspace screenshot

  1. In your project, create a new OSX Framework target. This will be your server target

creating a new target

  1. Link PerfectLib with both your server target and your app's target (if you're building an app alongside the server)

  2. Edit your server's Run scheme to launch with PerfectServer HTTP App.

edit scheme scheme with executable

  1. In your Server target's Build Settings, set the following flags:

    • Skip Install = No
    • Deployment Location = Yes
    • Installation Directory = /PerfectLibraries
    • Installation Build Products Location = $(CONFIGURATION_BUILD_DIR)
  2. Create a new file in the server's folder. This file will handle requests that come in. Include [most of] the following:

    import PerfectLib
    
    // This function is required. The Perfect framework expects to find this function
    // to do initialization
    public func PerfectServerModuleInit() {
    
        // Install the built-in routing handler. 
        // This is required by Perfect to initialize everything
        Routing.Handler.registerGlobally()
    
        // These two routes are specific to the tutorial in the link above. 
        // This is where you would register your own endpoints. 
        // Take a look at the docs for the Routes API to better understand
        // everything you can do here
    
        // register a route for gettings posts
        Routing.Routes["GET", "/posts"] = { _ in
            return GetPostHandler()
        }
    
        // register a route for creating a new post
        Routing.Routes["POST", "/posts"] = { _ in
            return PostHandler()
        }
    }
    
    class GetPostHandler: RequestHandler {
        func handleRequest(request: WebRequest, response: WebResponse) {
            response.appendBodyString("get posts")
            response.requestCompletedCallback()
        }
    }
    
    class PostHandler: RequestHandler {
        func handleRequest(request: WebRequest, response: WebResponse) {
            response.appendBodyString("creating post")
            response.requestCompletedCallback()
        }
    }
    

As you're building out different aspects of your service, you can test it by using cURL in the command line, or other REST testing tools like Postman

If you wanna dive deeper and learn how to integrate with a SQLite database or create an app that talks with your new server, check out the tutorial at the top of this post.

like image 33
Chris Avatar answered Oct 26 '22 03:10

Chris