Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

55 rader
2.0 KiB

  1. import NIOSSL
  2. import Fluent
  3. import FluentPostgresDriver
  4. import Vapor
  5. import JWT
  6. extension String {
  7. var bytes: [UInt8] { .init(self.utf8) }
  8. }
  9. extension JWKIdentifier {
  10. static let `public` = JWKIdentifier(string: "public")
  11. static let `private` = JWKIdentifier(string: "private")
  12. }
  13. // configures your application
  14. public func configure(_ app: Application) async throws {
  15. let privateKey = try String(contentsOfFile: app.directory.workingDirectory + "jwtRS256.key")
  16. let privateSigner = try JWTSigner.rs256(key: .private(pem: privateKey.bytes))
  17. let publicKey = try String(contentsOfFile: app.directory.workingDirectory + "jwtRS256.key.pub")
  18. let publicSigner = try JWTSigner.rs256(key: .public(pem: publicKey.bytes))
  19. app.jwt.signers.use(privateSigner, kid: .private)
  20. app.jwt.signers.use(publicSigner, kid: .public, isDefault: true)
  21. app.middleware.use(CORSMiddleware())
  22. let corsConfiguration = CORSMiddleware.Configuration(
  23. allowedOrigin: .all,
  24. allowedMethods: [.GET, .POST, .PUT, .OPTIONS, .DELETE, .PATCH],
  25. allowedHeaders: [.accept, .authorization, .contentType, .origin, .xRequestedWith, .userAgent, .accessControlAllowOrigin]
  26. )
  27. let cors = CORSMiddleware(configuration: corsConfiguration)
  28. app.middleware.use(cors, at: .beginning)
  29. app.http.server.configuration.port = 3004
  30. app.databases.use(DatabaseConfigurationFactory.postgres(configuration: .init(
  31. hostname: Environment.get("DATABASE_HOST") ?? "localhost",
  32. port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? SQLPostgresConfiguration.ianaPortNumber,
  33. username: Environment.get("DATABASE_USERNAME") ?? "mcarman",
  34. password: Environment.get("DATABASE_PASSWORD") ?? "@ng31F@rm0823262",
  35. database: Environment.get("DATABASE_NAME") ?? "trainingdb",
  36. tls: .prefer(try .init(configuration: .clientDefault)))
  37. ), as: .psql)
  38. app.logger.logLevel = .debug
  39. try await doMigrations(app)
  40. // register routes
  41. try routes(app)
  42. }