9

Perfect 是一个新的 Swift 框架,用于在 swift 中创建 web/http 服务器。文档还没有,我发现从头开始构建新项目有困难。我不知道需要导入哪些框架,哪个是应用程序的入口点。main.swift 等...

我想创建一个新的 xcworkspace,其中包含我的项目“你好世界服务器”。

我试图解决的问题:

  • 必须包含哪些框架?
  • 我应该如何创建一个完美的服务器,应用程序的入口点是什么?
  • 如何创建一个响应“Hello World 消息”的“hello”根?
  • 我应该如何为服务器制作目标并最终运行服务器?
4

5 回答 5

5

我设法写了一个关于这个的“Hello World”指南。http://code-me-dirty.blogspot.co.uk/2016/02/creating-perfect-swift-server.html

简而言之,您需要像这样进行:

  1. 克隆原始项目

  2. 创建一个新的工作区

  3. 创建一个新项目

  4. 导入 PerfectLib.xcodeproject & Import PerfectServer.xcodeproject 但不要复制

  5. 设置您的项目方案以启动 PerfectServer HTTP 应用程序

  6. 在“链接的框架和库”部分链接 PerfectLib

  7. 为您的框架目标设置构建设置*

  8. 创建 PerfectHandlers.swift 并粘贴(最好写感觉)以下代码

    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()
        }
    }
    

然后你就可以运行了。在我的博客上,我有一个很长、更详细的版本,如有必要,我会在这里更新。

构建设置

  • 部署地点:是
  • 安装构建产品位置:$(CONFIGURATION_BUILD_DIR)
  • 安装目录:/PerfectLibraries
  • 跳过安装:否
于 2016-02-10T12:18:08.207 回答
4

我刚刚写了一个教程,我想作为另一个解决方案分享,其中概述了如何使用 Perfect 创建 Web 服务以及与之交互的应用程序。

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

概括

  1. 您必须将项目放在工作区中。此工作区还应包括 PerfectServer 和 PerfectLib 项目。

工作区截图

  1. 在您的项目中,创建一个新的 OSX 框架目标。这将是您的服务器目标

创建新目标

  1. 将 PerfectLib 与您的服务器目标和应用程序目标链接(如果您正在与服务器一起构建应用程序)

  2. 编辑服务器的运行方案以使用 PerfectServer HTTP 应用程序启动。

编辑方案 具有可执行文件的方案

  1. 在您的服务器目标的构建设置中,设置以下标志:

    • 跳过安装 = 否
    • 部署位置 = 是
    • 安装目录 = /PerfectLibraries
    • 安装构建产品位置 = $(CONFIGURATION_BUILD_DIR)
  2. 在服务器的文件夹中创建一个新文件。此文件将处理传入的请求。包括 [大部分] 以下内容:

    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()
        }
    }
    

在构建服务的不同方面时,可以在命令行中使用 cURL 或Postman等其他 REST 测试工具对其进行测试

如果您想深入了解如何与 SQLite 数据库集成或创建与新服务器通信的应用程序,请查看本文顶部的教程。

于 2016-02-16T00:39:26.370 回答
3

正如其他人建议的那样,我建议远离模板,自己创建一个干净的项目。

创建此文件夹结构:

MyAPI
├── Package.swift
└── Sources
    └── main.swift

然后,在Package.swift文件中

import PackageDescription

let package = Package(
    name: "MyAPI",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 2)
    ]
)

还有 main.swift 文件:

import PerfectHTTP
import PerfectHTTPServer

do {

    let route = Route(method: .get, uri: "/hello", handler: { (request: HTTPRequest, response: HTTPResponse) in
        response.appendBody(string: "world!")
        response.completed()
    })

    try HTTPServer.launch(.server(name: "localhost", port: 8080, routes: [route]))

} catch {
    fatalError("\(error)")
}

转到命令行并运行:

swift package generate-xcodeproj

打开生成的项目文件:

MyAPI.xcodeproj

更改活动方案,然后构建并运行:

在此处输入图像描述

在 Safari 中打开:

http://localhost:8080/hello
于 2017-05-05T21:46:15.587 回答
0

我不确定您是否找到了解决方案,但这就是我所做的:

“Tap Tracker”应用程序是由 Perfect 库编写的应用程序,因此即使文档尚未准备好,您仍然可以剖析该应用程序。我重命名了应用程序和类/方法。有一个 index.html,我将其移至“www”根目录,然后使用 TTHandler 重新路由视图以尝试制作标准布局。由于框架太年轻,所以效果不太好,但可以做到。我会更具体,但我暂时回到了轨道,因为我想等到它更成熟一点。

玩起来很有趣,我可能会在完美的 onc 功能创新的基础上编写自己的库,我可以用它来稳定一些东西。

于 2016-02-08T20:14:32.903 回答
0

只需将示例项目之一中的所有文件复制到您的 git 存储库: https ://github.com/PerfectExamples

将所有文件(您已复制)中的 example_project_name 重命名为您的项目名称。

在终端运行

swift package generate-xcodeproj

您将获得一个具有所需名称的完美项目。

于 2016-12-03T06:02:24.740 回答