diff --git a/.gitignore b/.gitignore index 5d72a4a..b383d5c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ xcuserdata DerivedData/ .DS_Store file:* -Resources/config.json \ No newline at end of file +Resources/config.json +Resources/db.sqlite \ No newline at end of file diff --git a/Package.resolved b/Package.resolved index 27f968a..eb10d4e 100644 --- a/Package.resolved +++ b/Package.resolved @@ -28,6 +28,33 @@ "version": "4.2.1" } }, + { + "package": "fluent", + "repositoryURL": "https://github.com/vapor/fluent.git", + "state": { + "branch": null, + "revision": "e681c93df3201a2d8ceef15e8a9a0634578df233", + "version": "4.0.0" + } + }, + { + "package": "fluent-kit", + "repositoryURL": "https://github.com/vapor/fluent-kit.git", + "state": { + "branch": null, + "revision": "31d96b547cc1f869f2885d932a8a9a7ae2103fc6", + "version": "1.10.0" + } + }, + { + "package": "fluent-sqlite-driver", + "repositoryURL": "https://github.com/vapor/fluent-sqlite-driver.git", + "state": { + "branch": null, + "revision": "6f29f6f182c812075f09c7575c18ac5535c26824", + "version": "4.0.1" + } + }, { "package": "leaf", "repositoryURL": "https://github.com/vapor/leaf", @@ -64,6 +91,33 @@ "version": "4.2.0" } }, + { + "package": "sql-kit", + "repositoryURL": "https://github.com/vapor/sql-kit.git", + "state": { + "branch": null, + "revision": "ea9928b7f4a801b175a00b982034d9c54ecb6167", + "version": "3.7.0" + } + }, + { + "package": "sqlite-kit", + "repositoryURL": "https://github.com/vapor/sqlite-kit.git", + "state": { + "branch": null, + "revision": "2ec279b9c845cec254646834b66338551a024561", + "version": "4.0.2" + } + }, + { + "package": "sqlite-nio", + "repositoryURL": "https://github.com/vapor/sqlite-nio.git", + "state": { + "branch": null, + "revision": "6481dd0b01112d082dd7eb362782126e81964138", + "version": "1.1.0" + } + }, { "package": "swift-backtrace", "repositoryURL": "https://github.com/swift-server/swift-backtrace.git", diff --git a/Package.swift b/Package.swift index 38be553..424827e 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,9 @@ let package = Package( .package(url: "https://github.com/vapor/leaf-kit", .exact("1.0.0-tau.1.1")), // Leaf Error Middleware for custom error pages .package(name: "LeafErrorMiddleware", url: "https://github.com/brokenhandsio/leaf-error-middleware.git", from: "2.0.0-beta"), - .package(url: "https://github.com/apple/swift-crypto.git", from: "1.1.2") + .package(url: "https://github.com/apple/swift-crypto.git", from: "1.1.2"), + .package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"), + .package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0") ], targets: [ .target( @@ -23,6 +25,8 @@ let package = Package( .product(name: "Vapor", package: "vapor"), .product(name: "Leaf", package: "leaf"), .product(name: "Crypto", package: "swift-crypto"), + .product(name: "Fluent", package: "fluent"), + .product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"), "LeafErrorMiddleware" ], swiftSettings: [ diff --git a/Resources/db.sqlite b/Resources/db.sqlite new file mode 100644 index 0000000..072afd9 Binary files /dev/null and b/Resources/db.sqlite differ diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 769617b..06182e9 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -1,6 +1,8 @@ +import Fluent import Vapor import Leaf import LeafErrorMiddleware +import FluentSQLiteDriver // configures your application public func configure(_ app: Application) throws { @@ -8,8 +10,15 @@ public func configure(_ app: Application) throws { // Serve files from /Public app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) + app.sessions.use(.fluent) + app.sessions.configuration.cookieName = "statusboard-session" + app.sessions.configuration.cookieFactory = { sessionID in + .init(string: sessionID.string, expires: Date(timeIntervalSinceNow: 60*60*24*365), isSecure: true, isHTTPOnly: true, sameSite: HTTPCookies.SameSitePolicy.none) + } app.middleware.use(app.sessions.middleware) - app.sessions.configuration.cookieName = "statusboard_session" + + app.databases.use(.sqlite(.file("\(app.directory.resourcesDirectory)/db.sqlite")), as: .sqlite) + app.migrations.add(SessionRecord.migration) // Configure Leaf LeafOption.caching = app.environment.isRelease ? .default : .bypass