Make all settings configurable by system property or environment variable

This commit is contained in:
Naoki Takezoe
2017-06-11 01:48:35 +09:00
parent 5164d57787
commit 282d5fe239
3 changed files with 106 additions and 34 deletions

View File

@@ -134,16 +134,16 @@ public class JettyLauncher {
return new File(System.getProperty("user.home"), ".gitbucket"); return new File(System.getProperty("user.home"), ".gitbucket");
} }
private static void deleteDirectory(File dir){ // private static void deleteDirectory(File dir){
for(File file: dir.listFiles()){ // for(File file: dir.listFiles()){
if(file.isFile()){ // if(file.isFile()){
file.delete(); // file.delete();
} else if(file.isDirectory()){ // } else if(file.isDirectory()){
deleteDirectory(file); // deleteDirectory(file);
} // }
} // }
dir.delete(); // dir.delete();
} // }
private static Handler addStatisticsHandler(Handler handler) { private static Handler addStatisticsHandler(Handler handler) {
// The graceful shutdown is implemented via the statistics handler. // The graceful shutdown is implemented via the statistics handler.

View File

@@ -1,9 +1,9 @@
package gitbucket.core.service package gitbucket.core.service
import gitbucket.core.util.{Directory, SyntaxSugars}
import gitbucket.core.util.Implicits._ import gitbucket.core.util.Implicits._
import Directory._ import gitbucket.core.util.ConfigUtil._
import SyntaxSugars._ import gitbucket.core.util.Directory._
import gitbucket.core.util.SyntaxSugars._
import SystemSettingsService._ import SystemSettingsService._
import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletRequest
@@ -220,23 +220,53 @@ object SystemSettingsService {
private val LdapSsl = "ldap.ssl" private val LdapSsl = "ldap.ssl"
private val LdapKeystore = "ldap.keystore" private val LdapKeystore = "ldap.keystore"
private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A = // private def getEnvironmentVariable[A](key: String): Option[A] = {
defining(props.getProperty(key)){ value => // val value = System.getenv("GITBUCKET_" + key.toUpperCase.replace('.', '_'))
if(value == null || value.isEmpty) default // if(value != null && value.nonEmpty){
else convertType(value).asInstanceOf[A] // Some(convertType(value)).asInstanceOf[Option[A]]
} // } else {
// None
// }
// }
//
// private def getSystemProperty[A](key: String): Option[A] = {
// val value = System.getProperty("gitbucket." + key)
// if(value != null && value.nonEmpty){
// Some(convertType(value)).asInstanceOf[Option[A]]
// } else {
// None
// }
// }
private def getOptionValue[A: ClassTag](props: java.util.Properties, key: String, default: Option[A]): Option[A] = private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A = {
defining(props.getProperty(key)){ value => getSystemProperty(key).getOrElse(getEnvironmentVariable(key).getOrElse {
if(value == null || value.isEmpty) default defining(props.getProperty(key)){ value =>
else Some(convertType(value)).asInstanceOf[Option[A]] if(value == null || value.isEmpty){
} default
} else {
convertType(value).asInstanceOf[A]
}
}
})
}
private def convertType[A: ClassTag](value: String) = private def getOptionValue[A: ClassTag](props: java.util.Properties, key: String, default: Option[A]): Option[A] = {
defining(implicitly[ClassTag[A]].runtimeClass){ c => getSystemProperty(key).orElse(getEnvironmentVariable(key).orElse {
if(c == classOf[Boolean]) value.toBoolean defining(props.getProperty(key)){ value =>
else if(c == classOf[Int]) value.toInt if(value == null || value.isEmpty){
else value default
} } else {
Some(convertType(value)).asInstanceOf[Option[A]]
}
}
})
}
// private def convertType[A: ClassTag](value: String) =
// defining(implicitly[ClassTag[A]].runtimeClass){ c =>
// if(c == classOf[Boolean]) value.toBoolean
// else if(c == classOf[Int]) value.toInt
// else value
// }
} }

View File

@@ -4,11 +4,15 @@ import com.typesafe.config.ConfigFactory
import java.io.File import java.io.File
import Directory._ import Directory._
import com.github.takezoe.slick.blocking.{BlockingH2Driver, BlockingMySQLDriver, BlockingJdbcProfile} import ConfigUtil._
import com.github.takezoe.slick.blocking.{BlockingH2Driver, BlockingJdbcProfile, BlockingMySQLDriver}
import gitbucket.core.util.SyntaxSugars.defining
import liquibase.database.AbstractJdbcDatabase import liquibase.database.AbstractJdbcDatabase
import liquibase.database.core.{H2Database, MySQLDatabase, PostgresDatabase} import liquibase.database.core.{H2Database, MySQLDatabase, PostgresDatabase}
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
import scala.reflect.ClassTag
object DatabaseConfig { object DatabaseConfig {
private lazy val config = { private lazy val config = {
@@ -30,14 +34,14 @@ object DatabaseConfig {
ConfigFactory.parseFile(file) ConfigFactory.parseFile(file)
} }
private lazy val dbUrl = config.getString("db.url") private lazy val dbUrl = getValue("db.url", config.getString) //config.getString("db.url")
def url(directory: Option[String]): String = def url(directory: Option[String]): String =
dbUrl.replace("${DatabaseHome}", directory.getOrElse(DatabaseHome)) dbUrl.replace("${DatabaseHome}", directory.getOrElse(DatabaseHome))
lazy val url : String = url(None) lazy val url : String = url(None)
lazy val user : String = config.getString("db.user") lazy val user : String = getValue("db.user", config.getString)
lazy val password : String = config.getString("db.password") lazy val password : String = getValue("db.password", config.getString)
lazy val jdbcDriver : String = DatabaseType(url).jdbcDriver lazy val jdbcDriver : String = DatabaseType(url).jdbcDriver
lazy val slickDriver : BlockingJdbcProfile = DatabaseType(url).slickDriver lazy val slickDriver : BlockingJdbcProfile = DatabaseType(url).slickDriver
lazy val liquiDriver : AbstractJdbcDatabase = DatabaseType(url).liquiDriver lazy val liquiDriver : AbstractJdbcDatabase = DatabaseType(url).liquiDriver
@@ -47,8 +51,16 @@ object DatabaseConfig {
lazy val minimumIdle : Option[Int] = getOptionValue("db.minimumIdle" , config.getInt) lazy val minimumIdle : Option[Int] = getOptionValue("db.minimumIdle" , config.getInt)
lazy val maximumPoolSize : Option[Int] = getOptionValue("db.maximumPoolSize" , config.getInt) lazy val maximumPoolSize : Option[Int] = getOptionValue("db.maximumPoolSize" , config.getInt)
private def getValue[T](path: String, f: String => T): T = {
getSystemProperty(path).getOrElse(getEnvironmentVariable(path).getOrElse{
f(path)
})
}
private def getOptionValue[T](path: String, f: String => T): Option[T] = { private def getOptionValue[T](path: String, f: String => T): Option[T] = {
if(config.hasPath(path)) Some(f(path)) else None getSystemProperty(path).orElse(getEnvironmentVariable(path).orElse {
if(config.hasPath(path)) Some(f(path)) else None
})
} }
} }
@@ -99,3 +111,33 @@ object DatabaseType {
} }
} }
} }
object ConfigUtil {
def getEnvironmentVariable[A](key: String): Option[A] = {
val value = System.getenv("GITBUCKET_" + key.toUpperCase.replace('.', '_'))
if(value != null && value.nonEmpty){
Some(convertType(value)).asInstanceOf[Option[A]]
} else {
None
}
}
def getSystemProperty[A](key: String): Option[A] = {
val value = System.getProperty("gitbucket." + key)
if(value != null && value.nonEmpty){
Some(convertType(value)).asInstanceOf[Option[A]]
} else {
None
}
}
def convertType[A: ClassTag](value: String) =
defining(implicitly[ClassTag[A]].runtimeClass){ c =>
if(c == classOf[Boolean]) value.toBoolean
else if(c == classOf[Long]) value.toLong
else if(c == classOf[Int]) value.toInt
else value
}
}