-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHandlers.swift
More file actions
executable file
·93 lines (74 loc) · 3.14 KB
/
Handlers.swift
File metadata and controls
executable file
·93 lines (74 loc) · 3.14 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
//
// Handlers.swift
// Perfect-Blog-Mustache
//
// Created by Jonathan Guthrie on 2016-09-28.
//
//
import PerfectLib
import PerfectSQLite
import PerfectHTTP
import PerfectMustache
import SwiftString
/*
These are the main Mustache handlers.
They are called as the handlers from the routes in main.swift
*/
struct ListHandler: MustachePageHandler { // all template handlers must inherit from PageHandler
// This is the function which all handlers must impliment.
// It is called by the system to allow the handler to return the set of values which will be used when populating the template.
// - parameter context: The MustacheWebEvaluationContext which provides access to the HTTPRequest containing all the information pertaining to the request
// - parameter collector: The MustacheEvaluationOutputCollector which can be used to adjust the template output. For example a `defaultEncodingFunc` could be installed to change how outgoing values are encoded.
func extendValuesForResponse(context contxt: MustacheWebEvaluationContext, collector: MustacheEvaluationOutputCollector) {
var values = MustacheEvaluationContext.MapType()
var ary = [Any]()
let dbHandler = DB()
let data = dbHandler.getList()
for i in 0..<data.count {
var thisPost = [String:String]()
thisPost["title"] = data[i]["title"]
thisPost["synopsis"] = data[i]["synopsis"]
thisPost["titlesanitized"] = data[i]["title"]!.slugify()
ary.append(thisPost)
}
values["posts"] = ary
contxt.extendValues(with: values)
do {
try contxt.requestCompleted(withCollector: collector)
} catch {
let response = contxt.webResponse
response.status = .internalServerError
response.appendBody(string: "\(error)")
response.completed()
}
}
}
struct StoryHandler: MustachePageHandler { // all template handlers must inherit from PageHandler
// This is the function which all handlers must impliment.
// It is called by the system to allow the handler to return the set of values which will be used when populating the template.
// - parameter context: The MustacheWebEvaluationContext which provides access to the HTTPRequest containing all the information pertaining to the request
// - parameter collector: The MustacheEvaluationOutputCollector which can be used to adjust the template output. For example a `defaultEncodingFunc` could be installed to change how outgoing values are encoded.
func extendValuesForResponse(context contxt: MustacheWebEvaluationContext, collector: MustacheEvaluationOutputCollector) {
var values = MustacheEvaluationContext.MapType()
let request = contxt.webRequest
let titleSanitized = request.urlVariables["titleSanitized"] ?? ""
let dbHandler = DB()
let data = dbHandler.getStory(titleSanitized)
if data["title"] == nil {
values["title"] = "Error"
values["body"] = "<p>No story found</p>"
} else {
values["title"] = data["title"]
values["body"] = data["body"]
}
contxt.extendValues(with: values)
do {
try contxt.requestCompleted(withCollector: collector)
} catch {
let response = contxt.webResponse
response.status = .internalServerError
response.appendBody(string: "\(error)")
response.completed()
}
}
}