Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error handling to avoid database-inconsistency. #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 31 additions & 37 deletions Sources/PerfectLocalAuthentication/Schema/InitializeSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,40 @@ import PerfectSessionPostgreSQL

public func initializeSchema(_ fname: String = "./config/ApplicationConfiguration.json") -> [String:Any] {
var opts = [String:Any]()
if let config = JSONConfig(name: fname) {
let dict = config.getValues()!
opts["httpPort"] = dict["httpport"] as! Int
// StORM Connector Config
PostgresConnector.host = dict["postgreshost"] as? String ?? ""
PostgresConnector.username = dict["postgresuser"] as? String ?? ""
PostgresConnector.password = dict["postgrespwd"] as? String ?? ""
PostgresConnector.database = dict["postgresdbname"] as? String ?? ""
PostgresConnector.port = dict["postgresport"] as? Int ?? 0

// Outbound email config
SMTPConfig.mailserver = dict["mailserver"] as? String ?? ""
SMTPConfig.mailuser = dict["mailuser"] as? String ?? ""
SMTPConfig.mailpass = dict["mailpass"] as? String ?? ""
SMTPConfig.mailfromaddress = dict["mailfromaddress"] as? String ?? ""
SMTPConfig.mailfromname = dict["mailfromname"] as? String ?? ""

opts["baseURL"] = dict["baseURL"] as? String ?? ""
AuthenticationVariables.baseURL = dict["baseURL"] as? String ?? ""

// session driver config
PostgresSessionConnector.host = PostgresConnector.host
PostgresSessionConnector.port = PostgresConnector.port
PostgresSessionConnector.username = PostgresConnector.username
PostgresSessionConnector.password = PostgresConnector.password
PostgresSessionConnector.database = PostgresConnector.database
PostgresSessionConnector.table = "sessions"

} else {
print("Unable to get Configuration")

PostgresConnector.host = "localhost"
PostgresConnector.username = "perfect"
PostgresConnector.password = "perfect"
PostgresConnector.database = "perfect_testing"
PostgresConnector.port = 5432

guard let config = JSONConfig(name: fname) else {
fatalError("Unable to open configuration file: \(fname)")
}
guard let dict = config.getValues() else {
fatalError("Unable to parse configuration file: \(fname)")
}

opts["httpPort"] = dict["httpport"] as! Int
// StORM Connector Config
PostgresConnector.host = dict["postgreshost"] as? String ?? ""
PostgresConnector.username = dict["postgresuser"] as? String ?? ""
PostgresConnector.password = dict["postgrespwd"] as? String ?? ""
PostgresConnector.database = dict["postgresdbname"] as? String ?? ""
PostgresConnector.port = dict["postgresport"] as? Int ?? 0

// Outbound email config
SMTPConfig.mailserver = dict["mailserver"] as? String ?? ""
SMTPConfig.mailuser = dict["mailuser"] as? String ?? ""
SMTPConfig.mailpass = dict["mailpass"] as? String ?? ""
SMTPConfig.mailfromaddress = dict["mailfromaddress"] as? String ?? ""
SMTPConfig.mailfromname = dict["mailfromname"] as? String ?? ""

opts["baseURL"] = dict["baseURL"] as? String ?? ""
AuthenticationVariables.baseURL = dict["baseURL"] as? String ?? ""

// session driver config
PostgresSessionConnector.host = PostgresConnector.host
PostgresSessionConnector.port = PostgresConnector.port
PostgresSessionConnector.username = PostgresConnector.username
PostgresSessionConnector.password = PostgresConnector.password
PostgresSessionConnector.database = PostgresConnector.database
PostgresSessionConnector.table = "sessions"

// StORMdebug = true

// Account
Expand All @@ -62,4 +57,3 @@ public func initializeSchema(_ fname: String = "./config/ApplicationConfiguratio

return opts
}