-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.swift
More file actions
executable file
·150 lines (110 loc) · 3.85 KB
/
main.swift
File metadata and controls
executable file
·150 lines (110 loc) · 3.85 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
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
//
// main.swift
// PerfectTurnstileSQLiteDemo
//
// Created by Jonathan Guthrie on 2016-10-11.
// 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
import StORM
import PostgresStORM
import PerfectTurnstilePostgreSQL
import PerfectRequestLogger
import TurnstilePerfect
//StORMdebug = true
RequestLogFile.location = "./requests.log"
// Used later in script for the Realm and how the user authenticates.
let pturnstile = TurnstilePerfectRealm()
PostgresConnector.host = "localhost"
PostgresConnector.username = "perfect"
PostgresConnector.password = "perfect"
PostgresConnector.database = "perfect_testing"
PostgresConnector.port = 5432
// Set up the Authentication table
let auth = AuthAccount()
try? auth.setup()
// Connect the AccessTokenStore
tokenStore = AccessTokenStore()
try? tokenStore?.setup()
//let facebook = Facebook(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
//let google = Google(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
// Create HTTP server.
let server = HTTPServer()
// Register routes and handlers
let authWebRoutes = makeWebAuthRoutes()
let authJSONRoutes = makeJSONAuthRoutes("/api/v1")
// Add the routes to the server.
server.addRoutes(authWebRoutes)
server.addRoutes(authJSONRoutes)
// Adding a test route
var routes = Routes()
routes.add(method: .get, uri: "/api/v1/test", handler: AuthHandlersJSON.testHandler)
// An example route where authentication will be enforced
routes.add(method: .get, uri: "/api/v1/check", handler: {
request, response in
response.setHeader(.contentType, value: "application/json")
var resp = [String: String]()
resp["authenticated"] = "AUTHED: \(request.user.authenticated)"
let sessionIdValue: String = String(describing: request.user.authDetails?.account.uniqueID)
resp["SessionID"] = "SessionID: \(sessionIdValue)"
do {
try response.setBody(json: resp)
} catch {
print(error)
}
response.completed()
})
// An example route where auth will not be enforced
routes.add(method: .get, uri: "/api/v1/nocheck", handler: {
request, response in
response.setHeader(.contentType, value: "application/json")
var resp = [String: String]()
resp["authenticated"] = "AUTHED: \(request.user.authenticated)"
let authDetailsValue: String = String(describing: request.user.authDetails)
resp["authDetails"] = "DETAILS: \(authDetailsValue)"
do {
try response.setBody(json: resp)
} catch {
print(error)
}
response.completed()
})
// Add the routes to the server.
server.addRoutes(routes)
// Setup logging
let myLogger = RequestLogger()
// add routes to be checked for auth
var authenticationConfig = AuthenticationConfig()
authenticationConfig.include("/api/v1/check")
authenticationConfig.exclude("/api/v1/login")
authenticationConfig.exclude("/api/v1/register")
let authFilter = AuthFilter(authenticationConfig)
// Note that order matters when the filters are of the same priority level
server.setRequestFilters([pturnstile.requestFilter])
server.setResponseFilters([pturnstile.responseFilter])
server.setRequestFilters([(authFilter, .high)])
server.setRequestFilters([(myLogger, .high)])
server.setResponseFilters([(myLogger, .low)])
// Set a listen port of 8181
server.serverPort = 8181
// Where to serve static files from
server.documentRoot = "./webroot"
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
}