diff --git a/src/main/java/JettyLauncher.java b/src/main/java/JettyLauncher.java index 93f27ef..44c1acd 100644 --- a/src/main/java/JettyLauncher.java +++ b/src/main/java/JettyLauncher.java @@ -16,12 +16,17 @@ System.setProperty("java.awt.headless", "true"); String host = null; - int port = 8080; + String port = null; InetSocketAddress address = null; String contextPath = "/"; String tmpDirPath=""; boolean forceHttps = false; + host = getEnvironmentVariable("gitbucket.host"); + port = getEnvironmentVariable("gitbucket.port"); + contextPath = getEnvironmentVariable("gitbucket.prefix"); + tmpDirPath = getEnvironmentVariable("gitbucket.tempDir"); + for(String arg: args) { if(arg.startsWith("--") && arg.contains("=")) { String[] dim = arg.split("="); @@ -31,13 +36,10 @@ host = dim[1]; break; case "--port": - port = Integer.parseInt(dim[1]); + port = dim[1]; break; case "--prefix": contextPath = dim[1]; - if (!contextPath.startsWith("/")) { - contextPath = "/" + contextPath; - } break; case "--max_file_size": System.setProperty("gitbucket.maxFileSize", dim[1]); @@ -62,10 +64,14 @@ } } + if (contextPath != null && !contextPath.startsWith("/")) { + contextPath = "/" + contextPath; + } + if(host != null) { - address = new InetSocketAddress(host, port); + address = new InetSocketAddress(host, getPort(port)); } else { - address = new InetSocketAddress(port); + address = new InetSocketAddress(getPort(port)); } Server server = new Server(address); @@ -143,6 +149,23 @@ return new File(System.getProperty("user.home"), ".gitbucket"); } + private static String getEnvironmentVariable(String key){ + String value = System.getenv(key.toUpperCase().replace('.', '_')); + if (value != null && value.length() == 0){ + return null; + } else { + return value; + } + } + + private static int getPort(String port){ + if(port == null) { + return 8080; + } else { + return Integer.parseInt(port); + } + } + private static Handler addStatisticsHandler(Handler handler) { // The graceful shutdown is implemented via the statistics handler. // See the following: https://bugs.eclipse.org/bugs/show_bug.cgi?id=420142 diff --git a/src/main/scala/gitbucket/core/plugin/PluginRegistry.scala b/src/main/scala/gitbucket/core/plugin/PluginRegistry.scala index b633691..b018a29 100644 --- a/src/main/scala/gitbucket/core/plugin/PluginRegistry.scala +++ b/src/main/scala/gitbucket/core/plugin/PluginRegistry.scala @@ -15,7 +15,7 @@ import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.service.SystemSettingsService import gitbucket.core.service.SystemSettingsService.SystemSettings -import gitbucket.core.util.DatabaseConfig +import gitbucket.core.util.{ConfigUtil, DatabaseConfig} import gitbucket.core.util.Directory._ import io.github.gitbucket.solidbase.Solidbase import io.github.gitbucket.solidbase.manager.JDBCVersionManager @@ -235,7 +235,7 @@ .reverse } - lazy val extraPluginDir: Option[String] = Option(System.getProperty("gitbucket.pluginDir")) + lazy val extraPluginDir: Option[String] = ConfigUtil.getConfigValue[String]("gitbucket.pluginDir") def getGitBucketVersion(pluginJarFileName: String): Option[String] = { val regex = ".+-gitbucket\\_(\\d+\\.\\d+\\.\\d+(-SNAPSHOT)?)-.+".r diff --git a/src/main/scala/gitbucket/core/service/SystemSettingsService.scala b/src/main/scala/gitbucket/core/service/SystemSettingsService.scala index 8f4e50e..34d8fd1 100644 --- a/src/main/scala/gitbucket/core/service/SystemSettingsService.scala +++ b/src/main/scala/gitbucket/core/service/SystemSettingsService.scala @@ -305,7 +305,7 @@ private val PluginProxyPassword = "plugin.proxy.password" private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A = { - getSystemProperty(key).getOrElse(getEnvironmentVariable(key).getOrElse { + getConfigValue(key).getOrElse { defining(props.getProperty(key)) { value => if (value == null || value.isEmpty) { default @@ -313,11 +313,11 @@ convertType(value).asInstanceOf[A] } } - }) + } } private def getOptionValue[A: ClassTag](props: java.util.Properties, key: String, default: Option[A]): Option[A] = { - getSystemProperty(key).orElse(getEnvironmentVariable(key).orElse { + getConfigValue(key).orElse { defining(props.getProperty(key)) { value => if (value == null || value.isEmpty) { default @@ -325,7 +325,7 @@ Some(convertType(value)).asInstanceOf[Option[A]] } } - }) + } } } diff --git a/src/main/scala/gitbucket/core/util/ConfigUtil.scala b/src/main/scala/gitbucket/core/util/ConfigUtil.scala new file mode 100644 index 0000000..af5f942 --- /dev/null +++ b/src/main/scala/gitbucket/core/util/ConfigUtil.scala @@ -0,0 +1,41 @@ +package gitbucket.core.util + +import gitbucket.core.util.SyntaxSugars.defining + +import scala.reflect.ClassTag + +object ConfigUtil { + + def getConfigValue[A: ClassTag](key: String): Option[A] = { + getSystemProperty(key).orElse(getEnvironmentVariable(key)) + } + + def getEnvironmentVariable[A: ClassTag](key: String): Option[A] = { + val name = (if (key.startsWith("gitbucket.")) "" else "GITBUCKET_") + key.toUpperCase.replace('.', '_') + val value = System.getenv(name) + if (value != null && value.nonEmpty) { + Some(convertType(value)) + } else { + None + } + } + + def getSystemProperty[A: ClassTag](key: String): Option[A] = { + val name = if (key.startsWith("gitbucket.")) key else "gitbucket." + key + val value = System.getProperty(name) + if (value != null && value.nonEmpty) { + Some(convertType(value)) + } else { + None + } + } + + def convertType[A: ClassTag](value: String): A = + 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 + }.asInstanceOf[A] + +} diff --git a/src/main/scala/gitbucket/core/util/DatabaseConfig.scala b/src/main/scala/gitbucket/core/util/DatabaseConfig.scala index a2f6682..1401b8e 100644 --- a/src/main/scala/gitbucket/core/util/DatabaseConfig.scala +++ b/src/main/scala/gitbucket/core/util/DatabaseConfig.scala @@ -6,7 +6,6 @@ import Directory._ import ConfigUtil._ import com.github.takezoe.slick.blocking.{BlockingH2Driver, BlockingJdbcProfile, BlockingMySQLDriver} -import gitbucket.core.util.SyntaxSugars.defining import liquibase.database.AbstractJdbcDatabase import liquibase.database.core.{H2Database, MySQLDatabase, PostgresDatabase} import org.apache.commons.io.FileUtils @@ -54,16 +53,14 @@ lazy val minimumIdle: Option[Int] = getOptionValue("db.minimumIdle", 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 getValue[T: ClassTag](path: String, f: String => T): T = { + getConfigValue(path).getOrElse(f(path)) } - private def getOptionValue[T](path: String, f: String => T): Option[T] = { - getSystemProperty(path).orElse(getEnvironmentVariable(path).orElse { + private def getOptionValue[T: ClassTag](path: String, f: String => T): Option[T] = { + getConfigValue(path).orElse { if (config.hasPath(path)) Some(f(path)) else None - }) + } } } @@ -114,33 +111,3 @@ } } } - -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 - } - -} diff --git a/src/main/scala/gitbucket/core/util/FileUtil.scala b/src/main/scala/gitbucket/core/util/FileUtil.scala index a54b75a..1fc336a 100644 --- a/src/main/scala/gitbucket/core/util/FileUtil.scala +++ b/src/main/scala/gitbucket/core/util/FileUtil.scala @@ -92,16 +92,12 @@ name } - lazy val MaxFileSize = - if (System.getProperty("gitbucket.maxFileSize") != null) - System.getProperty("gitbucket.maxFileSize").toLong - else - 3 * 1024 * 1024 + lazy val MaxFileSize: Long = { + ConfigUtil.getConfigValue[Long]("gitbucket.maxFileSize").getOrElse(3 * 1024 * 1024) + } - lazy val UploadTimeout = - if (System.getProperty("gitbucket.UploadTimeout") != null) - System.getProperty("gitbucket.UploadTimeout").toLong - else - 3 * 10000 + lazy val UploadTimeout: Long = { + ConfigUtil.getConfigValue[Long]("gitbucket.UploadTimeout").getOrElse(3 * 10000) + } } diff --git a/src/main/scala/gitbucket/core/util/Validations.scala b/src/main/scala/gitbucket/core/util/Validations.scala index 0402c72..65082de 100644 --- a/src/main/scala/gitbucket/core/util/Validations.scala +++ b/src/main/scala/gitbucket/core/util/Validations.scala @@ -8,7 +8,7 @@ /** * Constraint for the identifier such as user name or page name. */ - def identifier: Constraint = new Constraint() { + val identifier: Constraint = new Constraint() { override def validate(name: String, value: String, messages: Messages): Option[String] = if (!value.matches("[a-zA-Z0-9\\-_.]+")) { Some(s"${name} contains invalid character.") @@ -22,19 +22,22 @@ /** * Constraint for the password. */ - def password: Constraint = new Constraint() { - override def validate(name: String, value: String, messages: Messages): Option[String] = - if (System.getProperty("gitbucket.validate.password") != "false" && !value.matches("[a-zA-Z0-9\\-_.]+")) { + val password: Constraint = new Constraint() { + lazy val validatePassword = ConfigUtil.getConfigValue[Boolean]("gitbucket.validate.password").getOrElse(true) + + override def validate(name: String, value: String, messages: Messages): Option[String] = { + if (validatePassword == true && !value.matches("[a-zA-Z0-9\\-_.]+")) { Some(s"${name} contains invalid character.") } else { None } + } } /** * Constraint for the repository identifier. */ - def repository: Constraint = new Constraint() { + val repository: Constraint = new Constraint() { override def validate(name: String, value: String, messages: Messages): Option[String] = if (!value.matches("[a-zA-Z0-9\\-\\+_.]+")) { Some(s"${name} contains invalid character.") @@ -48,7 +51,7 @@ /** * Constraint for the color pattern. */ - def color = pattern("#[0-9a-fA-F]{6}") + val color = pattern("#[0-9a-fA-F]{6}") /** * ValueType for the java.util.Date property.