From fc60ea8c4d52c5015edce1e4c6086d80e7a7d830 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 28 Feb 2021 08:15:55 +0000 Subject: Flags per directory, override vhost settings --- server/src/net/usebox/gemini/server/Server.scala | 2 +- .../src/net/usebox/gemini/server/ServiceConf.scala | 46 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) (limited to 'server/src/net/usebox') diff --git a/server/src/net/usebox/gemini/server/Server.scala b/server/src/net/usebox/gemini/server/Server.scala index ed9252d..2e9bb6b 100644 --- a/server/src/net/usebox/gemini/server/Server.scala +++ b/server/src/net/usebox/gemini/server/Server.scala @@ -142,7 +142,7 @@ case class Server(conf: ServiceConf) { bodySize = dirFile.length(), bodyPath = Some(dirFilePath) ) - } else if (vhost.directoryListing) { + } else if (vhost.getDirectoryListing(resource)) { logger.debug("directory listing") DirListing( req, diff --git a/server/src/net/usebox/gemini/server/ServiceConf.scala b/server/src/net/usebox/gemini/server/ServiceConf.scala index 9c0f8e8..f0751d2 100644 --- a/server/src/net/usebox/gemini/server/ServiceConf.scala +++ b/server/src/net/usebox/gemini/server/ServiceConf.scala @@ -1,21 +1,39 @@ package net.usebox.gemini.server +import java.nio.file.{Path, FileSystems} + +import scala.concurrent.duration.FiniteDuration + import pureconfig._ import pureconfig.generic.semiauto._ -import scala.concurrent.duration.FiniteDuration +import org.log4s._ case class KeyStore(path: String, alias: String, password: String) +case class Directory(path: String, directoryListing: Option[Boolean]) + case class VirtualHost( host: String, root: String, keyStore: Option[KeyStore] = None, indexFile: String = "index.gmi", directoryListing: Boolean = true, - geminiParams: Option[String] = None + geminiParams: Option[String] = None, + directories: List[Directory] ) +object VirtualHost { + implicit class VirtualHostOps(vhost: VirtualHost) { + def getDirectoryListing(path: Path): Boolean = + vhost.directories + .find(_.path == path.toString()) + .fold(vhost.directoryListing)(loc => + loc.directoryListing.getOrElse(vhost.directoryListing) + ) + } +} + case class ServiceConf( address: String, port: Int, @@ -30,9 +48,31 @@ case class ServiceConf( object ServiceConf { + private[this] val logger = getLogger + implicit val keyStoreReader = deriveReader[KeyStore] + implicit val directoryHostReader = deriveReader[Directory] implicit val virtualHostReader = deriveReader[VirtualHost] implicit val serviceConfReader = deriveReader[ServiceConf] - def load(confFile: String) = ConfigSource.file(confFile).load[ServiceConf] + def load(confFile: String) = + ConfigSource.file(confFile).load[ServiceConf].map { conf => + conf.copy(virtualHosts = conf.virtualHosts.map { vhost => + vhost.copy(directories = vhost.directories.map { dir => + val path = + FileSystems + .getDefault() + .getPath(vhost.root, dir.path) + .normalize() + + if (!path.toFile().isDirectory()) + logger.warn( + s"In virtual host '${vhost.host}': directory entry '${dir.path}' is not a directory" + ) + + dir + .copy(path = path.toString()) + }) + }) + } } -- cgit v1.2.3