Skip to content

Instantly share code, notes, and snippets.

@memory-lovers
Last active June 12, 2017 15:11
Show Gist options
  • Save memory-lovers/352aa0c33070c2acd842fe9aa596e1b7 to your computer and use it in GitHub Desktop.
Save memory-lovers/352aa0c33070c2acd842fe9aa596e1b7 to your computer and use it in GitHub Desktop.
package com.example
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import java.net.URI
import java.net.URISyntaxException
import javax.sql.DataSource
@Profile("heroku")
@Configuration
open class HerokuConfig(@Autowired val properties: DataSourceProperties) {
@Bean
@Throws(URISyntaxException::class)
open fun dataSource(): DataSource {
val dbUri = URI(System.getenv("DATABASE_URL"))
val url = "jdbc:postgresql://" + dbUri.host + ":" + dbUri.port + dbUri.path
val username = dbUri.userInfo.split(":")[0]
val password = dbUri.userInfo.split(":")[1]
return DataSourceBuilder
.create(this.properties.classLoader)
.url(url)
.username(username)
.password(password).build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment