Swift Perfect 基础项目

  • brew install mysql@5.7 && brew link mysql@5.7 --force  (mac下开发)
  • Package.swift
  •  1 // swift-tools-version:5.0
     2 import PackageDescription //   // swift-tools-version:5.0 必填。5.0是当前主机安装的swift版本号
     3 
     4 let package = Package(
     5     name: "diarymood",
     6     products: [
     7         .executable(name: "diarymood", targets: ["diarymood"])
     8     ],
     9     dependencies: [
    10         .package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
    11         .package(url:"https://github.com/PerfectlySoft/Perfect-MySQL.git", from: "3.0.0"),
    12         .package(url: "https://github.com/PerfectlySoft/Perfect-Logger.git", from: "3.0.0"),
    13         .package(url: "https://github.com/PerfectlySoft/Perfect-Curl.git", from: "3.0.0")
    14     ],
    15     targets: [
    16         .target(name: "diarymood", dependencies: ["PerfectHTTPServer", "PerfectMySQL", "PerfectLogger", "PerfectCURL"])
    17     ]
    18 )
  • Sources⁩/⁨<ProjectName>/main.swift
 1 import PerfectLib
 2 import PerfectHTTP
 3 import PerfectHTTPServer
 4 import PerfectLogger
 5 
 6 //MARK: - Log location
 7 let logPath = "./files/log"
 8 let logDir = Dir(logPath)
 9 if !logDir.exists {
10     try Dir(logPath).create()
11 }
12 
13 LogFile.location = "\(logPath)/Server.log"
14 
15 // MARK: - Configure routes
16 var routes = BasicRoutes().routes
17 
18 // MARK: - Configure server
19 let server = HTTPServer()
20 server.addRoutes(routes)
21 server.serverPort = 8181
22 server.serverName = "localhost"
23 server.setResponseFilters([
24     (try PerfectHTTPServer.HTTPFilter.contentCompression(data: [:]), HTTPFilterPriority.high)])
25 
26 // MARK: - Start server
27 do {
28     LogFile.info("Server Start Successful")
29     try server.start()
30 } catch let error {
31     LogFile.error("Failure Start Server:\(error)")
32     print("Failure Start Server:\(error)")
33 }
  • Sources⁩/⁨<ProjectName>/ApiOperation.swift
 1 import Foundation
 2 import PerfectLib
 3 import PerfectHTTP
 4 import PerfectHTTPServer
 5 
 6 // localhost html
 7 private let LocalhostHtml: String = "<html><meta charset=\"UTF-8\"><title>Api Server</title><body>接口服务器<br>V0.0.1</body></html>"
 8 
 9 class BasicRoutes {
10     var routes: Routes {
11         get {
12             var baseRoutes = Routes()
13             
14             // localhost
15             
16             // Configure one server which:
17             //    * Serves the hello world message at <host>:<port>/
18             //    * Serves static files out of the "./webroot"
19             //        directory (which must be located in the current working directory).
20             //    * Performs content compression on outgoing data when appropriate.
21             
22             baseRoutes.add(method: .get, uri: "/", handler: localhostHandler)
23             baseRoutes.add(method: .get, uri: "/**", handler: StaticFileHandler(documentRoot: "./webroot", allowResponseFilters: true).handleRequest)
24             
25             // Interface version
26             baseRoutes.add(method: .get, uri: "/api/v1", handler: apiVersionHandle)
27             
28             return baseRoutes
29         }
30     }
31     // MARK: - localhost
32     private func localhostHandler(request: HTTPRequest, response: HTTPResponse) {
33         // Respond with a simple message.
34         response.setHeader(.contentType, value: "text/html")
35         response.appendBody(string: LocalhostHtml)
36         // Ensure that response.completed() is called when your processing is done.
37         response.completed()
38     }
39     // MARK: - Interface version
40     private func apiVersionHandle(request: HTTPRequest, response: HTTPResponse) {
41         let successArray: [String: Any] = ["status": 1, "version": "0.0.1"]
42         let jsonStr = try! successArray.jsonEncodedString()
43         
44         response.appendBody(string: jsonStr)
45         response.completed()
46     }
47 }
  • swift build
  • swift package generate-xcodeproj

Mac 配置MySql:(管理工具 Sequel Pro)

  • 启动mysql服务

  $ mysql.server start

  • 初始化mysql配置

  $ mysql_secure_installation

 1 Securing the MySQL server deployment.
 4 
 5 Connecting to MySQL using a blank password.
 6 
 7 VALIDATE PASSWORD PLUGIN can be used to test passwords
 8 and improve security. It checks the strength of password
 9 and allows the users to set only those passwords which are
10 secure enough. Would you like to setup VALIDATE PASSWORD plugin?
11 
12 Press y|Y for Yes, any other key for No: N   // 这个选yes的话密码长度就必须要设置为8位以上,但我只想要6位的
13 Please set the password for root here.
14 
15 New password:             // 设置密码
16 
17 Re-enter new password:     // 再一次确认密码
18 By default, a MySQL installation has an anonymous user,
19 allowing anyone to log into MySQL without having to have
20 a user account created for them. This is intended only for
21 testing, and to make the installation go a bit smoother.
22 You should remove them before moving into a production
23 environment.
24 
25 Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y    // 移除不用密码的那个账户
26 Success.
27 
28 
29 Normally, root should only be allowed to connect from
30 'localhost'. This ensures that someone cannot guess at
31 the root password from the network.
32 
33 Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n  //不接受root远程登录账号
34 
35  ... skipping.
36 By default, MySQL comes with a database named 'test' that
37 anyone can access. This is also intended only for testing,
38 and should be removed before moving into a production
39 environment.
40 
41 
42 Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y  //删除text数据库
43  - Dropping test database...
44 Success.
45 
46  - Removing privileges on test database...
47 Success.
48 
49 Reloading the privilege tables will ensure that all changes
50 made so far will take effect immediately.
51 
52 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
53 Success.
54 
55 All done!

 

posted @ 2018-12-19 10:14  doubletcjs  阅读(513)  评论(0编辑  收藏  举报