Created
April 26, 2019 11:50
-
-
Save brianmarete/576ebc3b8e78cdf8a5481fa103876bac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.sql2o.*; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class DB { | |
private static URI dbUri; | |
public static Sql2o sql2o; | |
Logger logger = LoggerFactory.getLogger(DB.class); | |
static { | |
try { | |
if (System.getenv("DATABASE_URL") == null) { | |
dbUri = new URI("postgres://localhost:5432/to_do"); | |
} else { | |
dbUri = new URI(System.getenv("DATABASE_URL")); | |
} | |
int port = dbUri.getPort(); | |
String host = dbUri.getHost(); | |
String path = dbUri.getPath(); | |
String username = (dbUri.getUserInfo() == null) ? null : dbUri.getUserInfo().split(":")[0]; | |
String password = (dbUri.getUserInfo() == null) ? null : dbUri.getUserInfo().split(":")[1]; | |
sql2o = new Sql2o("jdbc:postgresql://" + host + ":" + port + path, username, password); | |
} catch (URISyntaxException e ) { | |
logger.error("Unable to connect to database."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment