-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.swift
More file actions
102 lines (83 loc) · 2.98 KB
/
example.swift
File metadata and controls
102 lines (83 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// main.swift
// Perfect JSON API Example
//
// Created by Jonathan Guthrie on 2016-09-26.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
// Create HTTP server.
let server = HTTPServer()
// Create the container variable for routes to be added to.
var routes = Routes()
// Register your own routes and handlers
// This is an example "Hello, world!" HTML route
routes.add(method: .get, uri: "/", handler: {
request, response in
// Setting the response content type explicitly to text/html
response.setHeader(.contentType, value: "text/html")
// Adding some HTML to the response body object
response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
// Signalling that the request is completed
response.completed()
}
)
// Adding a route to handle the GET people list URL
routes.add(method: .get, uri: "/api/v1/people", handler: {
request, response in
let people = People()
// Setting the response content type explicitly to application/json
response.setHeader(.contentType, value: "application/json")
// Setting the body response to the JSON list generated
response.appendBody(string: people.list())
// Signalling that the request is completed
response.completed()
}
)
// Adding a route to handle the POST people add URL, with post body params
routes.add(method: .post, uri: "/api/v1/people", handler: {
request, response in
let people = People()
// Setting the response content type explicitly to application/json
response.setHeader(.contentType, value: "application/json")
// Adding a new "person", passing the complete HTTPRequest object to the function.
response.appendBody(string: people.add(request))
// Signalling that the request is completed
response.completed()
}
)
// Adding a route to handle the POST people add via JSON
routes.add(method: .post, uri: "/api/v1/people/json", handler: {
request, response in
let people = People()
// Setting the response content type explicitly to application/json
response.setHeader(.contentType, value: "application/json")
// Adding a new "person", passing the just the request's post body as a raw string to the function.
response.appendBody(string: people.add(request.postBodyString!))
// Signalling that the request is completed
response.completed()
}
)
// Add the routes to the server.
server.addRoutes(routes)
// Set a listen port of 8181
server.serverPort = 8181
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
}