Skip to content

Commit

Permalink
Cloud_config support added. (#12)
Browse files Browse the repository at this point in the history
* Added cloud_config support.

Removed develop branch of buildpack from manifest file.

* Added more stuff to make cloud_config work.

It should check for a cloud_config file now. Got a real file
to actually test with. So this should work.

* Syntax changes.
  • Loading branch information
jesspittman authored Sep 21, 2016
1 parent 571a8ec commit 5aecfd7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
66 changes: 66 additions & 0 deletions Sources/Deploy/Configuration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation
import CouchDB
import SwiftyJSON
import LoggerAPI
import CloudFoundryEnv

public struct Configuration {

private static var configurationFile = "cloud_config.json"

public static func getConfiguration() -> Service? {
var appEnv: AppEnv
do {
let path = Configuration.getAbsolutePath(relativePath: "/\(configurationFile)", useFallback: false)
if path != nil {
let data = try Data(contentsOf: URL(fileURLWithPath: path!))
let configJson = JSON(data: data)
appEnv = try CloudFoundryEnv.getAppEnv(options: configJson)
Log.info("Using configuration values from '\(configurationFile)'.")
} else {
Log.warning("Could not find '\(configurationFile)'.")
appEnv = try CloudFoundryEnv.getAppEnv()
}
return appEnv.getService(spec: "TodoList-CouchDB")
} catch {
Log.warning("An error occurred while trying to read configurations.")
return nil
}
}

private static func getAbsolutePath(relativePath: String, useFallback: Bool) -> String? {
let initialPath = #file
let components = initialPath.characters.split(separator: "/").map(String.init)
let notLastThree = components[0..<components.count - 3]
var filePath = "/" + notLastThree.joined(separator: "/") + relativePath
let fileManager = FileManager.default

if fileManager.fileExists(atPath: filePath) {
return filePath
} else if useFallback {
// Get path in alternate way, if first way fails
let currentPath = fileManager.currentDirectoryPath
filePath = currentPath + relativePath
return fileManager.fileExists(atPath: filePath) ? filePath : nil
} else {
return nil
}
}

}
5 changes: 1 addition & 4 deletions Sources/Deploy/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ import TodoListWeb
import CloudFoundryEnv
import TodoListAPI
import TodoListWeb

import TodoList

Log.logger = HeliumLogger()



extension DatabaseConfiguration {

init(withService: Service) {
Expand All @@ -52,7 +49,7 @@ let databaseConfiguration: DatabaseConfiguration
let todos: TodoList

do {
if let service = try CloudFoundryEnv.getAppEnv().getService(spec: "TodoList-CouchDB") {
if let service = Configuration.getConfiguration() {
Log.verbose("Found TodoList-CouchDB on CloudFoundry")
databaseConfiguration = DatabaseConfiguration(withService: service)
todos = TodoList(databaseConfiguration)
Expand Down
1 change: 0 additions & 1 deletion manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ applications:
random-route: true
services:
- TodoList-CouchDB
buildpack: https://github.com/IBM-Swift/swift-buildpack.git#develop

0 comments on commit 5aecfd7

Please sign in to comment.