手记

从头开始创建一个基于Swift的Perfect的服务端

搭建一个Perfect工程大致步骤如下:

  1. 创建工作空间引入Perfect库

  2. 设置工程

  3. 编码运行

准备工作:
下载Perfect库
推荐从git上下载源码,因为里面包含了可能用到的所有组件:


解压Perfect库的压缩包,安装xcode的Perfect模板


打开这个工程直接run就可以


1. 创建工作空间引入Perfect库

1.1 创建工作空间

打开Xcode—>File—>New—>WorkPlace
创建一个PerfectDemo的工作空间,穿件完成后会生成一个PerfectDemo.xcworkspace文件该文件就是一个Xcode的工作空间。



1.2 引入PerfectLib与Perfect Server



注意添加选项的选择



添加完成后可以看到我们创建的PerfectDemo工作空间已经引入Perfect的依赖


接下来,创建一个PerfectDemo工程,该工程为正式的服务器工程:
推荐创建的时候以cocoaframwork的形式添加


注意添加工程的时候选择我们的工作空间为添加选项:


工程添加完成之后工作空间结构如下:



2. 设置工程

工程添加完成之后,开始设置我们的工程

setting.png


具体设置项目如下:
Skip Install = No
Deployment Location = Yes
Installation Directory = /PerfectLibraries
Installation Build Products Location = $(CONFIGURATION_BUILD_DIR)

3. 编码运行

所有的设置成功之后,开始编写服务端代码:
创建PerfectMain.swift文件:
先运行一下官方的示例代码:

  import Foundation  import PerfectLib  // This is the function which all Perfect Server modules must expose.
  // The system will load the module and call this function.// In here, register any handlers or perform any one-time tasks.
  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() 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)")}

  class IndexHandler: RequestHandler { 
    func handleRequest(request: WebRequest, response: WebResponse) { 
      response.appendBodyString("Index handler: You accessed path \(request.requestURI())") response.requestCompletedCallback() 
    }
  }

代码编写完成之后,设置运行的Schema
选中PerfectDemo工程不要选到Perfect的库上去了
设置Excutable为我们之前添加的Perfect模板

editschame.png


设置完成之后,Run工程会启动一个PerfectServer Http程序,
表示我们的Perfect服务器已经启动,改程序面板可以设置服务器的端口号,ip,已经webroot目录等等


success.png



作者:Tywin_Zhang
链接:https://www.jianshu.com/p/a4741a89f679


0人推荐
随时随地看视频
慕课网APP