Advertisement
Guest User

Untitled

a guest
Oct 31st, 2016
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. // main.swift ************* ************* ************* ************* *************
  2. import PerfectLib
  3. import PerfectHTTP
  4. import PerfectHTTPServer
  5.  
  6. // Create HTTP server.
  7. let server = HTTPServer()
  8.  
  9. // Register your own routes and handlers
  10. var routes = Routes()
  11. routes.add(method: .get, uri: "/", handler: {
  12. request, response in
  13. response.setHeader(.contentType, value: "text/html")
  14. response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
  15. response.completed()
  16. }
  17. )
  18.  
  19. //This route will be used to fetch data from the mysql database
  20. routes.add(method: .get, uri: "/use", handler: useMysql)
  21.  
  22. // Add the routes to the server.
  23. server.addRoutes(routes)
  24.  
  25. // Set a listen port of 8181
  26. server.serverPort = 8181
  27.  
  28. // Set a document root.
  29. // This is optional. If you do not want to serve static content then do not set this.
  30. // Setting the document root will automatically add a static file handler for the route /**
  31. server.documentRoot = "./webroot"
  32.  
  33. // Gather command line options and further configure the server.
  34. // Run the server with --help to see the list of supported arguments.
  35. // Command line arguments will supplant any of the values set above.
  36. configureServer(server)
  37.  
  38. do {
  39. // Launch the HTTP server.
  40. try server.start()
  41. } catch PerfectError.networkError(let err, let msg) {
  42. print("Network error thrown: \(err) \(msg)")
  43. }
  44.  
  45. // useMysql.swift ************* ************* ************* ************* *************
  46. import PerfectLib
  47. import MySQL
  48. import PerfectHTTP
  49.  
  50. let testHost = "127.0.0.1"
  51. let testUser = "root@localhost"
  52. // PLEASE change to whatever your actual password is before running these tests
  53. let testPassword = ""
  54. let testSchema = "MyAwesomeProjectDatabase"
  55.  
  56. let dataMysql = MySQL()
  57.  
  58. public func useMysql(_ request: HTTPRequest, response: HTTPResponse) {
  59. // need to make sure something is available.
  60. guard dataMysql.connect(host: testHost, user: testUser, password: testPassword ) else {
  61. Log.info(message: "Failure connecting to data server \(testHost)")
  62. return
  63. }
  64.  
  65. defer {
  66. dataMysql.close() // defer ensures we close our db connection at the end of this request
  67. }
  68.  
  69. //set database to be used, this example assumes presence of a users table and run a raw query, return failure message on a error
  70. guard dataMysql.selectDatabase(named: testSchema) && dataMysql.query(statement: "select * from users limit 1") else {
  71. Log.info(message: "Failure: \(dataMysql.errorCode()) \(dataMysql.errorMessage())")
  72.  
  73. return
  74. }
  75.  
  76. //store complete result set
  77. let results = dataMysql.storeResults()
  78.  
  79. //setup an array to store results
  80. var resultArray = [[String?]]()
  81.  
  82. while let row = results?.next() {
  83. resultArray.append(row)
  84.  
  85. }
  86.  
  87. //return array to http response
  88. response.appendBody(string: "<html><title>Mysql Test</title><body>\(resultArray.debugDescription)</body></html>")
  89. response.completed()
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement