|
| 1 | +# PerfectTemplate [English](https://github.com/PerfectlySoft/PerfectTemplate) |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="http://perfect.org/get-involved.html" target="_blank"> |
| 5 | + <img src="http://perfect.org/assets/github/perfect_github_2_0_0.jpg" alt="Get Involed with Perfect!" width="854" /> |
| 6 | + </a> |
| 7 | +</p> |
| 8 | + |
| 9 | +<p align="center"> |
| 10 | + <a href="https://github.com/PerfectlySoft/Perfect" target="_blank"> |
| 11 | + <img src="http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg" alt="Star Perfect On Github" /> |
| 12 | + </a> |
| 13 | + <a href="http://stackoverflow.com/questions/tagged/perfect" target="_blank"> |
| 14 | + <img src="http://www.perfect.org/github/perfect_gh_button_2_SO.jpg" alt="Stack Overflow" /> |
| 15 | + </a> |
| 16 | + <a href="https://twitter.com/perfectlysoft" target="_blank"> |
| 17 | + <img src="http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg" alt="Follow Perfect on Twitter" /> |
| 18 | + </a> |
| 19 | + <a href="http://perfect.ly" target="_blank"> |
| 20 | + <img src="http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg" alt="Join the Perfect Slack" /> |
| 21 | + </a> |
| 22 | +</p> |
| 23 | + |
| 24 | +<p align="center"> |
| 25 | + <a href="https://developer.apple.com/swift/" target="_blank"> |
| 26 | + <img src="https://img.shields.io/badge/Swift-3.0-orange.svg?style=flat" alt="Swift 3.0"> |
| 27 | + </a> |
| 28 | + <a href="https://developer.apple.com/swift/" target="_blank"> |
| 29 | + <img src="https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat" alt="Platforms OS X | Linux"> |
| 30 | + </a> |
| 31 | + <a href="http://perfect.org/licensing.html" target="_blank"> |
| 32 | + <img src="https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat" alt="License Apache"> |
| 33 | + </a> |
| 34 | + <a href="http://twitter.com/PerfectlySoft" target="_blank"> |
| 35 | + <img src="https://img.shields.io/badge/Twitter-@PerfectlySoft-blue.svg?style=flat" alt="PerfectlySoft Twitter"> |
| 36 | + </a> |
| 37 | + <a href="http://perfect.ly" target="_blank"> |
| 38 | + <img src="http://perfect.ly/badge.svg" alt="Slack Status"> |
| 39 | + </a> |
| 40 | +</p> |
| 41 | + |
| 42 | +Perfect Web服务器项目模板 |
| 43 | + |
| 44 | +本代码用于软件工程师在此基础之上开发Web服务器及其应用。您可以直接克隆本项目进行后续开发。该项目通过SPM软件包管理器编译,并能够生成一个可以独立运行的HTTP服务器。 |
| 45 | + |
| 46 | +###Swift兼容性 |
| 47 | + |
| 48 | +本项目必须使用Swift 3.0工具链及Xcode 8.0+,或者通过Linux安装[Swift.org](http://swift.org/)。 |
| 49 | + |
| 50 | +## Swift 版本注意事项 |
| 51 | + |
| 52 | +因为Xcode 8发行后出现了一些问题,如果您直接在Xcode下使用,我们建议安装swiftenv,以及 Swift 3.0.1 工具集预览版。 |
| 53 | + |
| 54 | +``` |
| 55 | +# after installing swiftenv from https://swiftenv.fuller.li/en/latest/ |
| 56 | +swiftenv install https://swift.org/builds/swift-3.0.1-preview-1/xcode/swift-3.0.1-PREVIEW-1/swift-3.0.1-PREVIEW-1-osx.pkg |
| 57 | +``` |
| 58 | + |
| 59 | +还有一种方式,就是在您Xcode中增加一个配置,即在项目设置“Project Settings”里面,查找条目“Library Search Paths”,然后将这个条目配置为“$(PROJECT_DIR)”,并且⚠️配置为⚠️递归形式“recursive”。这样就会通知编译器根据项目所在文件夹进行递归式检索项目所需要的函数库和参考引用。 |
| 60 | + |
| 61 | +## 编译运行 |
| 62 | + |
| 63 | +为了创建项目并且试验运行,请在终端命令行中输入以下内容。完成后就可以实现一个在本地网络8181端口工作的Web服务器。 |
| 64 | + |
| 65 | +``` |
| 66 | +git clone https://github.com/PerfectlySoft/PerfectTemplate.git |
| 67 | +cd PerfectTemplate |
| 68 | +swift build |
| 69 | +.build/debug/PerfectTemplate |
| 70 | +``` |
| 71 | + |
| 72 | +如果没有问题,输出应该看起来像是这样: |
| 73 | + |
| 74 | +``` |
| 75 | +Starting HTTP server on 0.0.0.0:8181 with document root ./webroot |
| 76 | +``` |
| 77 | + |
| 78 | +这表明服务器已经准备好并且等待连接了。请访问[http://localhost:8181/](http://127.0.0.1:8181/) 来查看欢迎信息。在终端命令行上输入control-c组合键即可停止Web服务。 |
| 79 | + |
| 80 | +## 快速上手 |
| 81 | + |
| 82 | +以下的源代码展示了一个最简单的“你好,世界!”样例。 |
| 83 | + |
| 84 | +```swift |
| 85 | +import PerfectLib |
| 86 | +import PerfectHTTP |
| 87 | +import PerfectHTTPServer |
| 88 | + |
| 89 | +// 创建HTTP服务器 |
| 90 | +let server = HTTPServer() |
| 91 | + |
| 92 | +// 注册自定义路由和页面句柄 |
| 93 | +var routes = Routes() |
| 94 | +routes.add(method: .get, uri: "/", handler: { |
| 95 | + request, response in |
| 96 | + response.appendBody(string: "<html><head><meta http-equiv='content-type' content='text/html;charset=utf-8'><title>你好,世界!</title></head><body>你好,世界!</body></html>") |
| 97 | + response.completed() |
| 98 | + } |
| 99 | +) |
| 100 | + |
| 101 | +// 将路由注册到服务器 |
| 102 | +server.addRoutes(routes) |
| 103 | + |
| 104 | +// 监听8181端口 |
| 105 | +server.serverPort = 8181 |
| 106 | + |
| 107 | +// 设置文档根目录。 |
| 108 | +// 这个操作是可选的,如果没有静态页面内容则可以忽略这一步。 |
| 109 | +// 设置文档根目录后,对于其他所有未经过滤器或已注册路由来说的其他路径“/**”,都会指向这个根目录下的文件。 |
| 110 | +server.documentRoot = "./webroot" |
| 111 | + |
| 112 | +// 逐个检查命令行参数和服务器配置 |
| 113 | +// 如果用命令行执行带 --help 参数的服务器可执行程序,就可以看到所有可以选择的参数。 |
| 114 | +// 如果调用时在命令行参数,而且该参数在配置文件中也有说明,则命令行参数的值会取代配置文件。 |
| 115 | +configureServer(server) |
| 116 | + |
| 117 | +do { |
| 118 | + // 启动HTTP服务器 |
| 119 | + try server.start() |
| 120 | +} catch PerfectError.networkError(let err, let msg) { |
| 121 | + print("网络异常: \(err) \(msg)") |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +## 问题报告 |
| 128 | + |
| 129 | +目前我们已经把所有错误报告合并转移到了JIRA上,因此github原有的错误汇报功能不能用于本项目。 |
| 130 | + |
| 131 | +您的任何宝贵建意见或建议,或者发现我们的程序有问题,欢迎您在这里告诉我们。[http://jira.perfect.org:8080/servicedesk/customer/portal/1](http://jira.perfect.org:8080/servicedesk/customer/portal/1)。 |
| 132 | + |
| 133 | +目前问题清单请参考以下链接: [http://jira.perfect.org:8080/projects/ISS/issues](http://jira.perfect.org:8080/projects/ISS/issues) |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +## 更多内容 |
| 138 | +关于Perfect更多内容,请参考[perfect.org](http://perfect.org)官网。 |
0 commit comments