-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #766 from softwaremill/use-autowire
Use autowire
- Loading branch information
Showing
22 changed files
with
210 additions
and
320 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
backend/src/main/scala/com/softwaremill/bootzooka/Dependencies.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.softwaremill.bootzooka | ||
|
||
import cats.data.NonEmptyList | ||
import cats.effect.{IO, Resource} | ||
import com.softwaremill.bootzooka.config.Config | ||
import com.softwaremill.bootzooka.email.EmailService | ||
import com.softwaremill.bootzooka.email.sender.EmailSender | ||
import com.softwaremill.bootzooka.http.{Http, HttpApi, HttpConfig} | ||
import com.softwaremill.bootzooka.metrics.{MetricsApi, VersionApi} | ||
import com.softwaremill.bootzooka.passwordreset.{PasswordResetApi, PasswordResetAuthToken} | ||
import com.softwaremill.bootzooka.security.ApiKeyAuthToken | ||
import com.softwaremill.bootzooka.user.UserApi | ||
import com.softwaremill.bootzooka.util.{Clock, DefaultIdGenerator} | ||
import com.softwaremill.macwire.autocats.autowire | ||
import doobie.util.transactor.Transactor | ||
import io.prometheus.client.CollectorRegistry | ||
import sttp.client3.SttpBackend | ||
|
||
case class Dependencies(api: HttpApi, emailService: EmailService) | ||
|
||
object Dependencies { | ||
def wire(config: Config, sttpBackend: Resource[IO, SttpBackend[IO, Any]], xa: Resource[IO, Transactor[IO]], clock: Clock): Resource[IO, Dependencies] = { | ||
def buildHttpApi(http: Http, userApi: UserApi, passwordResetApi: PasswordResetApi, metricsApi: MetricsApi, versionApi: VersionApi, collectorRegistry: CollectorRegistry, cfg: HttpConfig) = | ||
new HttpApi( | ||
http, | ||
userApi.endpoints concatNel passwordResetApi.endpoints, | ||
NonEmptyList.of(metricsApi.metricsEndpoint, versionApi.versionEndpoint), | ||
collectorRegistry, | ||
cfg) | ||
|
||
autowire[Dependencies]( | ||
config.api, | ||
config.user, | ||
config.passwordReset, | ||
config.email, | ||
DefaultIdGenerator, | ||
clock, | ||
CollectorRegistry.defaultRegistry, | ||
sttpBackend, | ||
xa, | ||
buildHttpApi _, | ||
new ApiKeyAuthToken(_), | ||
new EmailService(_, _, _, _, _), | ||
EmailSender.create _, | ||
new PasswordResetAuthToken(_), | ||
) | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
backend/src/main/scala/com/softwaremill/bootzooka/InitModule.scala
This file was deleted.
Oops, something went wrong.
35 changes: 21 additions & 14 deletions
35
backend/src/main/scala/com/softwaremill/bootzooka/Main.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
package com.softwaremill.bootzooka | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import cats.effect.{IO, Resource} | ||
import com.softwaremill.bootzooka.config.Config | ||
import com.softwaremill.bootzooka.infrastructure.DB | ||
import com.softwaremill.bootzooka.metrics.Metrics | ||
import com.softwaremill.bootzooka.util.DefaultClock | ||
import com.typesafe.scalalogging.StrictLogging | ||
import doobie.util.transactor | ||
import sttp.capabilities.WebSockets | ||
import sttp.capabilities.fs2.Fs2Streams | ||
import sttp.client3.SttpBackend | ||
import sttp.client3.asynchttpclient.fs2.AsyncHttpClientFs2Backend | ||
import sttp.client3.logging.slf4j.Slf4jLoggingBackend | ||
import sttp.client3.prometheus.PrometheusBackend | ||
|
||
object Main extends StrictLogging { | ||
def main(args: Array[String]): Unit = { | ||
Metrics.init() | ||
Thread.setDefaultUncaughtExceptionHandler((t, e) => logger.error("Uncaught exception in thread: " + t, e)) | ||
|
||
val initModule = new InitModule {} | ||
initModule.logConfig() | ||
val config = Config.read | ||
Config.log(config) | ||
|
||
val mainTask = initModule.db.transactorResource.use { _xa => | ||
initModule.baseSttpBackend.use { _baseSttpBackend => | ||
val modules = new MainModule { | ||
override def xa: transactor.Transactor[IO] = _xa | ||
override def baseSttpBackend: SttpBackend[IO, Any] = _baseSttpBackend | ||
override def config: Config = initModule.config | ||
} | ||
lazy val sttpBackend: Resource[IO, SttpBackend[IO, Fs2Streams[IO] with WebSockets]] = | ||
AsyncHttpClientFs2Backend | ||
.resource[IO]() | ||
.map(baseSttpBackend => Slf4jLoggingBackend(PrometheusBackend(baseSttpBackend), includeTiming = true)) | ||
|
||
val xa = new DB(config.db).transactorResource | ||
|
||
Dependencies | ||
.wire(config, sttpBackend, xa, DefaultClock) | ||
.use { case Dependencies(httpApi, emailService) => | ||
/* | ||
Sequencing two tasks using the >> operator: | ||
- the first starts the background processes (such as an email sender) | ||
- the second allocates the http api resource, and never releases it (so that the http server is available | ||
as long as our application runs) | ||
*/ | ||
modules.startBackgroundProcesses >> modules.httpApi.resource.use(_ => IO.never) | ||
emailService.startProcesses().void >> httpApi.resource.use(_ => IO.never) | ||
} | ||
} | ||
mainTask.unsafeRunSync() | ||
.unsafeRunSync() | ||
} | ||
} |
35 changes: 0 additions & 35 deletions
35
backend/src/main/scala/com/softwaremill/bootzooka/MainModule.scala
This file was deleted.
Oops, something went wrong.
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
36 changes: 0 additions & 36 deletions
36
backend/src/main/scala/com/softwaremill/bootzooka/config/ConfigModule.scala
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
backend/src/main/scala/com/softwaremill/bootzooka/email/EmailModule.scala
This file was deleted.
Oops, something went wrong.
13 changes: 12 additions & 1 deletion
13
backend/src/main/scala/com/softwaremill/bootzooka/email/sender/EmailSender.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
package com.softwaremill.bootzooka.email.sender | ||
|
||
import cats.effect.IO | ||
import com.softwaremill.bootzooka.email.EmailData | ||
import com.softwaremill.bootzooka.email.{EmailConfig, EmailData} | ||
import sttp.client3.SttpBackend | ||
|
||
trait EmailSender { | ||
def apply(email: EmailData): IO[Unit] | ||
} | ||
|
||
object EmailSender { | ||
def create(sttpBackend: SttpBackend[IO, Any], config: EmailConfig): EmailSender = if (config.mailgun.enabled) { | ||
new MailgunEmailSender(config.mailgun, sttpBackend) | ||
} else if (config.smtp.enabled) { | ||
new SmtpEmailSender(config.smtp) | ||
} else { | ||
DummyEmailSender | ||
} | ||
} |
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
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
Oops, something went wrong.