-
Notifications
You must be signed in to change notification settings - Fork 495
/
main.swift
190 lines (157 loc) · 5.09 KB
/
main.swift
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
// KituraSample shows examples for creating custom routes.
import KituraSys
import KituraNet
import KituraRouter
import LoggerAPI
import HeliumLogger
#if os(Linux)
import Glibc
#endif
import Foundation
import KituraMustache
// All Web apps need a router to define routes
let router = Router()
// Using an implementation for a Logger
Log.logger = HeliumLogger()
/**
* RouterMiddleware can be used for intercepting requests and handling custom behavior
* such as authentication and other routing
*/
class BasicAuthMiddleware: RouterMiddleware {
func handle(request: RouterRequest, response: RouterResponse, next: () -> Void) {
let authString = request.headers["Authorization"]
Log.info("Authorization: \(authString)")
next()
// Check authorization string in database to approve the request if fail
// response.error = NSError(domain: "AuthFailure", code: 1, userInfo: [:])
next()
}
}
// This route executes the echo middleware
router.use("/*", middleware: BasicAuthMiddleware())
router.use("/static/*", middleware: StaticFileServer())
router.get("/hello") { _, response, next in
response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
do {
try response.status(HttpStatusCode.OK).send("Hello World, from Kitura!").end()
}
catch {}
next()
}
// This route accepts POST requests
router.post("/hello") {request, response, next in
response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
do {
try response.status(HttpStatusCode.OK).send("Got a POST request").end()
}
catch {}
next()
}
// This route accepts PUT requests
router.put("/hello") {request, response, next in
response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
do {
try response.status(HttpStatusCode.OK).send("Got a PUT request").end()
}
catch {}
next()
}
// This route accepts DELETE requests
router.delete("/hello") {request, response, next in
response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
do {
try response.status(HttpStatusCode.OK).send("Got a DELETE request").end()
}
catch {}
next()
}
// Error handling example
router.get("/error") { _, response, next in
Log.error("Example of error being set")
response.status(HttpStatusCode.INTERNAL_SERVER_ERROR)
response.error = NSError(domain: "RouterTestDomain", code: 1, userInfo: [:])
next()
}
// Redirection example
router.get("/redir") { _, response, next in
do {
try response.redirect("http://www.ibm.com")
}
catch {}
next()
}
// Reading parameters
// Accepts user as a parameter
router.get("/users/:user") { request, response, next in
response.setHeader("Content-Type", value: "text/html; charset=utf-8")
let p1 = request.params["user"] ?? "(nil)"
do {
try response.status(HttpStatusCode.OK).send(
"<!DOCTYPE html><html><body>" +
"<b>User:</b> \(p1)" +
"</body></html>\n\n").end()
}
catch {}
next()
}
// Example using templating of strings
// Support for Mustache implented for OSX only yet
router.setTemplateEngine(MustacheTemplateEngine())
router.get("/document") { _, response, next in
defer {
next()
}
do {
// the example from https://github.com/groue/GRMustache.swift/blob/master/README.md
var context: [String: Any] = [
"name": "Arthur",
"date": NSDate(),
"realDate": NSDate().dateByAddingTimeInterval(60*60*24*3),
"late": true
]
// Let template format dates with `{{format(...)}}`
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
context["format"] = dateFormatter
try response.render("document", context: context).end()
} catch {
Log.error("Failed to render template \(error)")
}
}
// Handle the GET request at the root path
router.get("/") { _, response, next in
response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
do {
try response.status(HttpStatusCode.OK).send("You're running Kitura").end()
}
catch {}
next()
}
// Handles any errors that get set
router.error { request, response, next in
response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
do {
try response.send("Caught the error: \(response.error!.localizedDescription)").end()
}
catch {}
next()
}
// Listen on port 8090
let server = HttpServer.listen(8090,
delegate: router)
Server.run()