-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.swift
94 lines (70 loc) · 3.04 KB
/
configure.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
import Fluent
import FluentPostgresDriver
import FluentSQLiteDriver
import Vapor
public func configure(_ app: Application) async throws {
try configureDatabase(app)
app.databases.middleware.use(UserMiddleware())
addMigrations(app.migrations)
// Ability to parse JS dates
let jsDateFormatter = DateFormatter()
jsDateFormatter.calendar = Calendar(identifier: .iso8601)
jsDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(jsDateFormatter)
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .formatted(jsDateFormatter)
ContentConfiguration.global.use(encoder: encoder, for: .json)
ContentConfiguration.global.use(decoder: decoder, for: .json)
// Middlewares
let corsConfiguration = CORSMiddleware.Configuration(
allowedOrigin: .any(["https://localhost:8788", "https://localhost:5173", "https://localhost:4173",
"https://genealogee.app"]),
allowedMethods: [.GET, .POST, .DELETE, .PATCH, .OPTIONS],
allowedHeaders: [.accept, .authorization, .contentType, .origin, .xRequestedWith,
.userAgent, .accessControlAllowOrigin, .accessControlAllowHeaders,
.accessControlAllowCredentials, .accessControlAllowMethods],
allowCredentials: true
)
let cors = CORSMiddleware(configuration: corsConfiguration)
// Sessions
let cookieDomain = app.environment == .development ? "localhost" : "genealogee.app"
app.sessions.configuration.cookieFactory = { sessionID in
.init(string: sessionID.string,
maxAge: 60 * 60 * 24 * 7,
domain: cookieDomain,
isSecure: true,
isHTTPOnly: true,
sameSite: HTTPCookies.SameSitePolicy.lax)
}
app.middleware.use(cors, at: .beginning)
app.middleware.use(app.sessions.middleware)
app.middleware.use(User.sessionAuthenticator())
app.passwords.use(.bcrypt)
switch app.environment {
case .development:
app.http.server.configuration.tlsConfiguration = try .makeServerConfiguration(
certificateChain: NIOSSLCertificate.fromPEMFile("./certs/cert.pem").map { .certificate($0) },
privateKey: .file("./certs/key.pem")
)
fallthrough
case .testing:
app.sessions.use(.memory)
default:
app.sessions.use(.fluent)
}
app.trees.use { DatabaseTreeRepository(req: $0) }
app.people.use { DatabasePersonRepository(req: $0) }
app.families.use { DatabaseFamilyRepository(req: $0) }
app.treeSnapshots.use { DatabaseTreeSnapshotRepository(req: $0) }
try routes(app)
}
private func addMigrations(_ migrations: Migrations) {
migrations.add(CreateMigrations.all())
migrations.add(SessionRecord.migration)
migrations.add(AddTreeCreatedAt())
}
private func configureDatabase(_ app: Application) throws {
let postgresURL = Environment.get("DATABASE_URL")
try app.databases.use(.postgres(url: postgresURL!), as: .psql)
}