Skip to content

Commit 23e074a

Browse files
committed
Add redirect example
1 parent b85964d commit 23e074a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Examples/Demo/TelegraphDemo.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extension TelegraphDemo {
7070
// Note: we're ignoring possible strong retain cycles in the demo
7171
server.route(.GET, "hello/:name", serverHandleHello)
7272
server.route(.GET, "hello(/)", serverHandleHello)
73+
server.route(.GET, "redirect", serverHandleRedirect)
7374
server.route(.GET, "secret/*") { .forbidden }
7475
server.route(.GET, "status") { (.ok, "Server is running") }
7576

@@ -123,13 +124,20 @@ extension TelegraphDemo {
123124
// MARK: - Server route handlers
124125

125126
extension TelegraphDemo {
126-
/// Raised when the /hello enpoint is called
127+
/// Raised when the /hello endpoint is called.
127128
private func serverHandleHello(request: HTTPRequest) -> HTTPResponse {
128129
let name = request.params["name"] ?? "stranger"
129130
return HTTPResponse(content: "Hello \(name.capitalized)")
130131
}
131132

132-
/// Raised when the /data enpoint is called
133+
/// Raised when the /redirect endpoint is called.
134+
private func serverHandleRedirect(request: HTTPRequest) -> HTTPResponse {
135+
let response = HTTPResponse(.temporaryRedirect)
136+
response.headers.location = "https://www.google.com"
137+
return response
138+
}
139+
140+
/// Raised when the /data endpoint is called.
133141
private func serverHandleData(request: HTTPRequest) -> HTTPResponse {
134142
// Decode the request body using the JSON decoder, fallback to "stranger" if the data is invalid
135143
let requestDict = try? JSONDecoder().decode([String: String].self, from: request.body)
@@ -145,14 +153,14 @@ extension TelegraphDemo {
145153
// MARK: - Client request handlers
146154

147155
extension TelegraphDemo {
148-
/// Raised when the client processes the /hello endpoint response
156+
/// Raised when the client processes the /hello endpoint response.
149157
private func clientHandleHello(data: Data?, response: URLResponse) {
150158
if let textData = data, let text = String(data: textData, encoding: .utf8) {
151159
print("[CLIENT]", "Request on /hello succeeded - text:", text)
152160
}
153161
}
154162

155-
/// Raised when the client processes the /data endpoint response
163+
/// Raised when the client processes the /data endpoint response.
156164
private func clientHandleData(data: Data?, response: URLResponse) {
157165
if let jsonData = data, let json = try? JSONDecoder().decode([String: String].self, from: jsonData) {
158166
print("[CLIENT]", "Request on /data succeded - json:", json)

0 commit comments

Comments
 (0)