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

Implementing SSL/TLS. #5

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
build/
DerivedData
db/
.vscode/
## Various settings
*.pbxuser
!default.pbxuser
Expand Down
12 changes: 8 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ let package = Package(
.package(url: "https://github.com/PerfectlySoft/PerfectLib.git", from: "3.0.0"),
.package(url: "https://github.com/PerfectlySoft/Perfect-PostgreSQL.git", from: "3.0.0"),
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer", from: "3.0.0"),
.package(url: "https://github.com/PerfectlySoft/Perfect-Notifications.git", from: "3.0.0"),
.package(url: "https://github.com/PerfectlySoft/Perfect-CloudFormation.git", from: "0.0.0"),
.package(url: "https://github.com/PerfectlySoft/Perfect-Redis.git", from: "3.2.0"),
.package(url: "https://github.com/ubiqweus/qBiqSwiftCodables.git", .branch("master"))
.package(url: "https://github.com/ubiqweus/qBiqSwiftCodables.git", .branch("master")),
.package(url: "https://github.com/kjessup/SAuthCodables.git", .branch("master")),
],
targets: [
.target(name: "BiqNetLib", dependencies:[]),
.target(name: "BiqCollectorLib",
dependencies: [
"PerfectNet",
"PerfectThread",
"PerfectLib",
"PerfectPostgreSQL",
"PerfectRedis",
"SwiftCodables"
"PerfectNotifications",
"SwiftCodables",
"SAuthCodables",
"BiqNetLib"
]
),
.target(name: "BiqCollectorExe",
Expand Down
49 changes: 27 additions & 22 deletions Sources/BiqCollectorExe/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ import PerfectHTTPServer
import PerfectNet
import PerfectCrypto
import PerfectCloudFormation
import PerfectRedis
import PerfectCRUD
import SwiftCodables

extension String {
func env(_ defaultValue: String = "" ) -> String {
guard let pval = getenv(self) else {
print("loading env ", self, " = ", defaultValue)
return defaultValue
}
let val = String.init(cString: pval)
print("loading env ", self, " = ", val)
return val
}
}

let biqDatabaseInfo: CloudFormation.RDSInstance = {
if let pgsql = CloudFormation.listRDSInstances(type: .postgres)
.sorted(by: { $0.resourceName < $1.resourceName }).first {
Expand All @@ -38,39 +49,33 @@ let biqDatabaseInfo: CloudFormation.RDSInstance = {
return .init(resourceType: .postgres,
resourceId: "",
resourceName: "",
userName: "postgres",
password: "",
hostName: "localhost",
hostPort: 5432)
userName: "BIQ_PG_USER".env("postgres"),
password: "BIQ_PG_PASS".env(""),
hostName: "BIQ_PG_HOST".env("localhost"),
hostPort: Int("BIQ_PG_PORT".env("5432")) ?? 5432 )
}()

let biqRedisInfo: CloudFormation.ElastiCacheInstance = {
if let redis = CloudFormation.listElastiCacheInstances(type: .redis)
.sorted(by: { $0.resourceName < $1.resourceName }).first {
return redis
}
return .init(resourceType: .redis,
resourceId: "",
resourceName: "",
hostName: "localhost",
hostPort: 6379)
}()
CRUDLogging.queryLogDestinations = []
BiqObs.reportSink = biqRedisInfo
CRUDLogging.errorLogDestinations = [.console, .file("/var/log/qbiq_error.log")]

BiqObs.databaseInfo = biqDatabaseInfo

#if os(Linux)
let staticFilePort = 80
let testPort = 8080
#if os(Linux)
let webroot = "./webroot"
#else
let staticFilePort = 80
let testPort = 8080
let webroot = "/Users/kjessup/development/TreeFrog/qBiq/qBiqCollector/webroot"
//let webroot = "/Users/kjessup/development/TreeFrog/qBiq/qBiqCollector/webroot"
let webroot = "/Users/rockywei/qbiq/release"
#endif

let notificationConfigPath = "BIQ_NT_PATH".env("/root/conf.prod.json")
let notificationKeyPath = "BIQ_NT_KEY".env("/root/secret.key")

// static file server for updates
do {
CRUDLogging.log(.info, "Setup notifications on \(notificationConfigPath) with \(notificationKeyPath)")
try BiqCollectorLib.Config.setup(configurationFilePath: notificationConfigPath, keyPath: notificationKeyPath)
CRUDLogging.log(.info, "Binding static file server on port \(staticFilePort)")
func fileServe(_ request: HTTPRequest, _ response: HTTPResponse) {
let path = request.path
Expand Down Expand Up @@ -132,7 +137,7 @@ do {
var r = Routes()
r.add(method: .get, uri: "/**", handler: fileServe)
r.add(method: .head, uri: "/**", handler: fileServe)
try HTTPServer.launch(wait: false, name: "qbiq static files", port: staticFilePort, routes: r)
try HTTPServer.launch(wait: false, name: "qbiq static files", port: staticFilePort, routes: r)
} catch {
CRUDLogging.log(.error, "Unable to start static file server: \(error)")
}
Expand Down
Loading