Created
December 8, 2018 09:23
-
-
Save vzsg/32eeaeedcc59111575ef62eaffa5d60f to your computer and use it in GitHub Desktop.
Vapor 3 Sessions with PostgreSQL example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import FluentPostgreSQL | |
import Vapor | |
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws { | |
// (1) Standard PostgreSQL setup | |
try services.register(FluentPostgreSQLProvider()) | |
let pgURL = Environment.get("DATABASE_URL") ?? "postgres://vapor:vapor@127.0.0.1:5432/vapor" | |
let pgConfig = PostgreSQLDatabaseConfig(url: pgURL)! | |
var databases = DatabasesConfig() | |
databases.add(database: PostgreSQLDatabase(config: pgConfig), as: .psql) | |
services.register(databases) | |
// --- (1) END | |
var middlewares = MiddlewareConfig() | |
middlewares.use(ErrorMiddleware.self) | |
middlewares.use(SessionsMiddleware.self) // (2) Enabling sessions | |
services.register(middlewares) | |
var migrations = MigrationConfig() | |
migrations.prepareCache(for: .psql) // (3) Adding CacheEntry to the migrations | |
services.register(migrations) | |
// (4) Configuring FluentPostgreSQL as KeyedCache service... | |
// ...like scratching your ear from behind your back | |
services.register(KeyedCache.self) { container in | |
try container.keyedCache(for: .psql) | |
} | |
config.prefer(DatabaseKeyedCache<ConfiguredDatabase<PostgreSQLDatabase>>.self, for: KeyedCache.self) | |
// --- (4) END | |
let router = EngineRouter.default() | |
try routes(router) | |
services.register(router, as: Router.self) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
public func routes(_ router: Router) throws { | |
router.get("hello") { req -> String in | |
let name: String = try req.session()["name"] ?? "World" | |
return "Hello, \(name)!\n" | |
} | |
router.post("hello") { req -> Response in | |
guard let name: String = try req.content.syncGet(at: "name") else { | |
throw Abort(.badRequest) | |
} | |
let session = try req.session() | |
session["name"] = name | |
return req.redirect(to: "/hello") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cleaning up... | |
rm cookiejar | |
# 1. creating a new session | |
curl localhost:8080/hello -c cookiejar -b cookiejar | |
# prints "Hello, World!" | |
# 2. updating name in the session | |
curl -d name=Zsolt localhost:8080/hello -L -c cookiejar -b cookiejar | |
# prints "Hello, Zsolt!" | |
# 3. restarting the server | |
# ... this one is up to the reader of the gist ... | |
# 4. verifying session still exists | |
curl localhost:8080/hello -c cookiejar -b cookiejar | |
# prints "Hello, Zsolt!" again :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment