Last active
July 13, 2022 15:23
-
-
Save purejgleason/615e4f242b9174b2fa38fe7f4a3c8a54 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
class MySQLService extends HttpFunction { | |
val source = TestDataource(); | |
override def service(httpRequest: HttpRequest, httpResponse: HttpResponse): Unit = | |
httpResponse.getWriter.write(source.getSalutations) | |
} |
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
class MySQLService extends HttpFunction { | |
// TODO: Refactor into mixin or something | |
Class.forName("org.postgresql.Driver") | |
val dbName: String = System.getenv("DB_NAME") | |
val dbUser: String = System.getenv("DB_USER") | |
val dbPassword = Option( | |
System.getenv("DB_PASS") | |
).getOrElse("ignored") | |
val dbUseIAM = Option( | |
System.getenv("DB_IAM") | |
).getOrElse("true") | |
val ssoMode = Option( | |
System.getenv("DB_SSL") | |
).getOrElse("disable") // TODO: Should this be enabled by default? | |
val instanceConnectionName: String = System.getenv("INSTANCE_CONNECTION_NAME") | |
val jdbcURL: String = String.format("jdbc:postgresql:///%s", dbName) | |
val connProps = new Properties | |
connProps.setProperty("user", dbUser) | |
// Note: a non-empty string value for the password property must be set. While this property will be ignored when connecting with the Cloud SQL Connector using IAM auth, leaving it empty will cause driver-level validations to fail. | |
if( dbUseIAM.equals("true") ){ | |
println("Using IAM password is ignored") | |
connProps.setProperty("password", "ignored") | |
} else { | |
println("Using manual, password must be provided") | |
connProps.setProperty("password", dbPassword) | |
} | |
connProps.setProperty("sslmode", ssoMode) | |
connProps.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory") | |
connProps.setProperty("cloudSqlInstance", instanceConnectionName) | |
connProps.setProperty("enableIamAuth", dbUseIAM) | |
// Initialize connection pool | |
val config = new HikariConfig | |
config.setJdbcUrl(jdbcURL) | |
config.setDataSourceProperties(connProps) | |
config.setConnectionTimeout(10000) // 10s | |
config.addDataSourceProperty("ipTypes", "PUBLIC,PRIVATE") // TODO: Make configureable | |
var pool : DataSource = new HikariDataSource(config) | |
override def service(httpRequest: HttpRequest, httpResponse: HttpResponse): Unit = { | |
val salutations = new StringBuilder | |
val conn = pool.getConnection | |
try { | |
val selectStmt = conn.prepareStatement("SELECT * FROM test.hello") | |
try { | |
selectStmt.setQueryTimeout(10) // 10s | |
val rs = selectStmt.executeQuery | |
while ( rs.next ){ | |
if (salutations.toString.length != 0) | |
{ | |
salutations ++= "\n" | |
} | |
salutations ++= rs.getString("salutation") | |
} | |
} finally if (selectStmt != null) selectStmt.close() | |
} finally if (conn != null) conn.close() | |
httpResponse.getWriter.write(salutations.toString) | |
} | |
} |
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
package com.purepm | |
import java.util.Properties | |
import javax.sql.DataSource | |
import com.zaxxer.hikari.HikariConfig | |
import com.zaxxer.hikari.HikariDataSource | |
import java.sql.Connection | |
class TestDataource { | |
var pool : Option[DataSource] = None | |
def getSalutations: String = { | |
val salutations = new StringBuilder | |
val ds : DataSource = pool.getOrElse( | |
throw new RuntimeException("Could not get datasource") | |
) | |
val conn : Connection = ds.getConnection | |
try { | |
val selectStmt = conn.prepareStatement("SELECT * FROM test.hello") | |
try { | |
selectStmt.setQueryTimeout(10) // 10s | |
val rs = selectStmt.executeQuery | |
while ( rs.next ){ | |
if (salutations.toString.length != 0) | |
{ | |
salutations ++= "\n" | |
} | |
salutations ++= rs.getString("salutation") | |
} | |
} finally if (selectStmt != null) selectStmt.close() | |
} finally if (conn != null) conn.close() | |
salutations.toString | |
} | |
} | |
object TestDataource { | |
def apply(): TestDataource = { | |
// TODO: Refactor into mixin or something | |
Class.forName("org.postgresql.Driver") | |
val dbName: String = System.getenv("DB_NAME") | |
val dbUser: String = System.getenv("DB_USER") | |
val dbPassword = Option( | |
System.getenv("DB_PASS") | |
).getOrElse("ignored") | |
val dbUseIAM = Option( | |
System.getenv("DB_IAM") | |
).getOrElse("true") | |
val ssoMode = Option( | |
System.getenv("DB_SSL") | |
).getOrElse("disable") // TODO: Should this be enabled by default? | |
val instanceConnectionName: String = System.getenv("INSTANCE_CONNECTION_NAME") | |
val jdbcURL: String = String.format("jdbc:postgresql:///%s", dbName) | |
val connProps = new Properties | |
connProps.setProperty("user", dbUser) | |
// Note: a non-empty string value for the password property must be set. While this property will be ignored when connecting with the Cloud SQL Connector using IAM auth, leaving it empty will cause driver-level validations to fail. | |
if( dbUseIAM.equals("true") ){ | |
println("Using IAM password is ignored") | |
connProps.setProperty("password", "ignored") | |
} else { | |
println("Using manual, password must be provided") | |
connProps.setProperty("password", dbPassword) | |
} | |
connProps.setProperty("sslmode", ssoMode) | |
connProps.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory") | |
connProps.setProperty("cloudSqlInstance", instanceConnectionName) | |
connProps.setProperty("enableIamAuth", dbUseIAM) | |
// Initialize connection pool | |
val config = new HikariConfig | |
config.setJdbcUrl(jdbcURL) | |
config.setDataSourceProperties(connProps) | |
config.setConnectionTimeout(10000) // 10s | |
config.addDataSourceProperty("ipTypes", "PUBLIC,PRIVATE") // TODO: Make configureable | |
val pool : DataSource = new HikariDataSource(config) | |
var source = new TestDataource | |
source.pool = Option(pool) | |
source | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment