Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.nullptr7
package configurations

import types.{DatabaseConfig, ServerConfig}

final case class ApplicationResources(
databaseConfig: DatabaseConfig,
serverConfig: BlazeServerConfig
serverConfig: ServerConfig
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,31 @@ package types {
final case class Port(value: Int) extends AnyVal

final case class Namespace(value: String) extends AnyVal
final case class ConfigLocation(value: String) extends AnyVal

sealed trait ConfigType {
trait ConfigType {
val namespace: Namespace
val location: Option[ConfigLocation]
}

final object DbDev extends ConfigType {
val namespace: Namespace = Namespace("db")
object ConfigType {

val location: Option[ConfigLocation] = None
}
implicit object Blaze extends ConfigType {
val namespace: Namespace = Namespace("server")
}

final object ServerDev extends ConfigType {
val location: Option[ConfigLocation] = None
implicit object Postgres extends ConfigType {
val namespace: Namespace = Namespace("db")
}

val namespace: Namespace = Namespace("server")
}

final case class ServerConfig(host: Hostname, port: Port)

final case class DatabaseConfig(
val host: Hostname,
val port: Port,
val user: String,
val database: String,
val password: Sensitive
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import pureconfig.{ConfigReader, ConfigSource}

import configurations.types._

trait ConfigLoader[F[_]] {
sealed trait ConfigLoader[F[_]] {

def load[Conf: ConfigReader: ClassTag](configType: ConfigType): Resource[F, Conf]
final protected lazy val source = ConfigSource.default

def load[Conf: ConfigReader: ClassTag, CT <: ConfigType](implicit ct: CT): Resource[F, Conf]
}

object ConfigLoader {
Expand All @@ -21,13 +23,8 @@ object ConfigLoader {

implicit def forSync[F[_]: Sync]: ConfigLoader[F] =
new ConfigLoader[F] {

def load[Conf: ConfigReader: ClassTag](configType: ConfigType): Resource[F, Conf] = Resource.eval {
configType.location match {
case None => ConfigSource.default.at(configType.namespace.value).loadF[F, Conf]()
case Some(path) => ConfigSource.file(path.value).at(configType.namespace.value).loadF[F, Conf]()
}
}
def load[Conf: ConfigReader: ClassTag, CT <: ConfigType](implicit ct: CT): Resource[F, Conf] =
Resource.eval(source.at(ct.namespace.value).loadF[F, Conf]())

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import skunk._
import fs2.io.net.Network
import natchez.Trace

import configurations.DatabaseConfig
import configurations.types.DatabaseConfig

trait DatabaseSession {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ import configurations._
import configurations.types._
import helpers.ConfigLoader

import types.ConfigType._

object Startup extends IOApp.Simple {

private lazy val loadResource: Resource[IO, ApplicationResources] =
(
ConfigLoader[IO].load[DatabaseConfig](DbDev),
ConfigLoader[IO].load[BlazeServerConfig](ServerDev)
ConfigLoader[IO].load[DatabaseConfig, Postgres.type],
ConfigLoader[IO].load[ServerConfig, Blaze.type]
).parMapN(ApplicationResources)

private def withServer(routes: HttpRoutes[IO], serverConfig: BlazeServerConfig): Resource[IO, Server] =
private def withServer(routes: HttpRoutes[IO], serverConfig: ServerConfig): Resource[IO, Server] =
BlazeServerBuilder[IO]
.bindHttp(serverConfig.port.value, serverConfig.host.value)
.withHttpApp(routes.orNotFound)
Expand Down