Skip to content
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Scala Server Toolkit

[![Build Status](https://travis-ci.org/avast/scala-server-toolkit.svg?branch=master)](https://travis-ci.org/avast/scala-server-toolkit)
[![Maven Central](https://img.shields.io/maven-central/v/com.avast/sst-http4s-blaze-server_2.12)](https://repo1.maven.org/maven2/com/avast/sst-http4s-blaze-server_2.12/)
[![Maven Central](https://img.shields.io/maven-central/v/com.avast/sst-bundle-zio-http4s-blaze_2.12)](https://repo1.maven.org/maven2/com/avast/sst-bundle-zio-http4s-blaze_2.12/)
[![Scala Steward badge](https://img.shields.io/badge/Scala_Steward-helping-brightgreen.svg?style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAQCAMAAAARSr4IAAAAVFBMVEUAAACHjojlOy5NWlrKzcYRKjGFjIbp293YycuLa3pYY2LSqql4f3pCUFTgSjNodYRmcXUsPD/NTTbjRS+2jomhgnzNc223cGvZS0HaSD0XLjbaSjElhIr+AAAAAXRSTlMAQObYZgAAAHlJREFUCNdNyosOwyAIhWHAQS1Vt7a77/3fcxxdmv0xwmckutAR1nkm4ggbyEcg/wWmlGLDAA3oL50xi6fk5ffZ3E2E3QfZDCcCN2YtbEWZt+Drc6u6rlqv7Uk0LdKqqr5rk2UCRXOk0vmQKGfc94nOJyQjouF9H/wCc9gECEYfONoAAAAASUVORK5CYII=)](https://scala-steward.org)

This project is a culmination of years of Scala development at Avast and tries to represent the best practices of Scala server development
Expand All @@ -10,7 +10,7 @@ together well and allow you to build reliable server applications.

## [Documentation](./docs/index.md)

Or you can [deep dive into example code](example/src/main/scala/com/avast/server/toolkit/example/Main.scala) if you like that more.
Or you can [deep dive into example code](example/src/main/scala/com/avast/sst/example/Main.scala) if you like that more.

## Design

Expand Down
142 changes: 115 additions & 27 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,52 @@ lazy val commonSettings = BuildSettings.common ++ Seq(

lazy val root = project
.in(file("."))
.aggregate(example, http4sBlazeClient, http4sBlazeServer, jvmExecution, jvmSsl, jvmSystem, pureconfig)
.aggregate(
bundleMonixHttp4sBlaze,
bundleZioHttp4sBlaze,
example,
http4sClientBlaze,
http4sClientBlazePureConfig,
http4sServer,
http4sServerBlaze,
http4sServerBlazePureConfig,
http4sServerMicrometer,
jvm,
jvmMicrometer,
jvmPureConfig,
micrometerJmx,
micrometerJmxPureConfig,
pureConfig
)
.settings(
name := "scala-server-toolkit",
publish / skip := true
)

lazy val bundleMonixHttp4sBlaze = project
.in(file("bundle-monix-http4s-blaze"))
.dependsOn(http4sClientBlaze, http4sServerBlaze, http4sServerBlazePureConfig, http4sServerMicrometer, jvmMicrometer, jvmPureConfig)
.settings(commonSettings)
.settings(
name := "sst-bundle-monix-http4s-blaze",
libraryDependencies += Dependencies.monixEval
)

lazy val bundleZioHttp4sBlaze = project
.in(file("bundle-zio-http4s-blaze"))
.dependsOn(http4sClientBlaze, http4sServerBlaze, http4sServerBlazePureConfig, http4sServerMicrometer, jvmMicrometer, jvmPureConfig)
.settings(commonSettings)
.settings(
name := "sst-bundle-zio-http4s-blaze",
libraryDependencies ++= Seq(
Dependencies.zio,
Dependencies.zioInteropCats
)
)

lazy val example = project
.dependsOn(jvmExecution, http4sBlazeClient, http4sBlazeServer, jvmSsl, jvmSystem, pureconfig)
.in(file("example"))
.dependsOn(bundleZioHttp4sBlaze, micrometerJmxPureConfig)
.enablePlugins(MdocPlugin)
.settings(commonSettings)
.settings(
Expand All @@ -34,57 +72,107 @@ lazy val example = project
Global / cancelable := true,
mdocIn := baseDirectory.value / "src" / "main" / "mdoc",
mdocOut := baseDirectory.value / ".." / "docs",
libraryDependencies ++= Seq(
Dependencies.zio,
Dependencies.zioInteropCats
)
libraryDependencies += Dependencies.logbackClassic
)

lazy val http4sBlazeClient = project
.in(file("http4s-blaze-client"))
.dependsOn(jvmSsl)
lazy val http4sClientBlaze = project
.in(file("http4s-client-blaze"))
.dependsOn(jvm)
.settings(commonSettings)
.settings(
name := "sst-http4s-blaze-client",
name := "sst-http4s-client-blaze",
libraryDependencies += Dependencies.http4sBlazeClient
)

lazy val http4sBlazeServer = project
.in(file("http4s-blaze-server"))
.dependsOn(http4sBlazeClient % Test)
lazy val http4sClientBlazePureConfig = project
.in(file("http4s-client-blaze-pureconfig"))
.dependsOn(http4sClientBlaze, jvmPureConfig)
.settings(commonSettings)
.settings(name := "sst-http4s-client-blaze-pureconfig")

lazy val http4sServer = project
.in(file("http4s-server"))
.settings(commonSettings)
.settings(
name := "sst-http4s-server",
libraryDependencies ++= Seq(
Dependencies.http4sServer,
Dependencies.http4sBlazeClient % Test,
Dependencies.http4sBlazeServer % Test,
Dependencies.http4sDsl % Test
)
)

lazy val http4sServerBlaze = project
.in(file("http4s-server-blaze"))
.dependsOn(http4sServer, http4sClientBlaze % Test)
.settings(commonSettings)
.settings(
name := "sst-http4s-blaze-server",
name := "sst-http4s-server-blaze",
libraryDependencies ++= Seq(
Dependencies.http4sBlazeServer,
Dependencies.http4sDsl,
Dependencies.slf4jApi
)
)

lazy val jvmExecution = project
.in(file("jvm-execution"))
lazy val http4sServerBlazePureConfig = project
.in(file("http4s-server-blaze-pureconfig"))
.dependsOn(http4sServerBlaze, pureConfig)
.settings(commonSettings)
.settings(name := "sst-http4s-server-blaze-pureconfig")

lazy val http4sServerMicrometer = project
.in(file("http4s-server-micrometer"))
.dependsOn(http4sServer)
.settings(commonSettings)
.settings(
name := "sst-http4s-server-micrometer",
libraryDependencies += Dependencies.micrometerCore
)

lazy val jvm = project
.in(file("jvm"))
.settings(commonSettings)
.settings(
commonSettings,
name := "sst-jvm-execution",
name := "sst-jvm",
libraryDependencies += Dependencies.slf4jApi
)

lazy val jvmSsl = project
.in(file("jvm-ssl"))
lazy val jvmMicrometer = project
.in(file("jvm-micrometer"))
.dependsOn(jvm)
.settings(commonSettings)
.settings(
commonSettings,
name := "sst-jvm-ssl"
name := "sst-jvm-micrometer",
libraryDependencies += Dependencies.micrometerCore
)

lazy val jvmSystem = project
.in(file("jvm-system"))
lazy val jvmPureConfig = project
.in(file("jvm-pureconfig"))
.dependsOn(jvm, pureConfig)
.settings(commonSettings)
.settings(name := "sst-jvm-pureconfig")

lazy val micrometerJmx = project
.in(file("micrometer-jmx"))
.settings(commonSettings)
.settings(
commonSettings,
name := "sst-jvm-system"
name := "sst-micrometer-jmx",
libraryDependencies ++= Seq(
Dependencies.micrometerJmx,
Dependencies.jsr305 // required because of Scala compiler
)
)

lazy val pureconfig = project
lazy val micrometerJmxPureConfig = project
.in(file("micrometer-jmx-pureconfig"))
.dependsOn(micrometerJmx, pureConfig)
.settings(commonSettings)
.settings(name := "sst-micrometer-jmx-pureconfig")

lazy val pureConfig = project
.in(file("pureconfig"))
.settings(commonSettings)
.settings(
name := "sst-pureconfig",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.avast.sst.bundle

import cats.effect.{ExitCode, Resource}
import monix.eval.{Task, TaskApp}
import org.http4s.server.Server
import org.slf4j.LoggerFactory

/** Extend this `trait` if you want to implement server application using [[monix.eval.Task]] effect data type.
*
* Implement method `program` with initialization and business logic of your application. It will be automatically run until JVM is shut
* down in which case all the resources are cleaned up because the whole `program` is a [[cats.effect.Resource]].
*/
trait MonixServerApp extends TaskApp {

private val logger = LoggerFactory.getLogger(this.getClass)

def program: Resource[Task, Server[Task]]

override def run(args: List[String]): Task[ExitCode] = {
program
.use { server =>
for {
_ <- Task.delay(logger.info(s"Server started @ ${server.address.getHostString}:${server.address.getPort}"))
_ <- Task.never[Unit]
} yield server
}
.redeem(
ex => {
logger.error("Server initialization failed!", ex)
ExitCode.Error
},
_ => ExitCode.Success
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.avast.sst.bundle

import cats.effect.Resource
import com.github.ghik.silencer.silent
import org.http4s.server.Server
import org.slf4j.LoggerFactory
import zio.interop.catz._
import zio.{Task, UIO, ZIO}

/** Extend this `trait` if you want to implement server application using [[zio.ZIO]] effect data type.
*
* Implement method `program` with initialization and business logic of your application. It will be automatically run until JVM is shut
* down in which case all the resources are cleaned up because the whole `program` is a [[cats.effect.Resource]].
*/
trait ZioServerApp extends CatsApp {

private val logger = LoggerFactory.getLogger(this.getClass)

def program: Resource[Task, Server[Task]]

@silent("dead code")
override def run(args: List[String]): ZIO[Environment, Nothing, Int] = {
program
.use { server =>
for {
_ <- UIO.effectTotal(logger.info(s"Server started @ ${server.address.getHostString}:${server.address.getPort}"))
_ <- Task.never
} yield server
}
.fold(
ex => {
logger.error("Server initialization failed!", ex)
1
},
_ => 0
)
}

}
17 changes: 9 additions & 8 deletions docs/http4s.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Module http4s

[![Maven Central](https://img.shields.io/maven-central/v/com.avast/sst-http4s-blaze-server_2.12)](https://repo1.maven.org/maven2/com/avast/sst-http4s-blaze-server_2.12/)
[![Maven Central](https://img.shields.io/maven-central/v/com.avast/sst-http4s-server-blaze_2.12)](https://repo1.maven.org/maven2/com/avast/sst-http4s-server-blaze_2.12/)

`libraryDependencies += "com.avast" %% "sst-http4s-blaze-server" % "<VERSION>"`
`libraryDependencies += "com.avast" %% "sst-http4s-server-blaze" % "<VERSION>"`

There are `http4s-*` modules that provide easy initialization of a server and a client. Http4s is an interface with multiple possible
implementations - for now we provide only implementations based on [Blaze](https://github.com/http4s/blaze).
Expand All @@ -11,9 +11,10 @@ Both server and client are configured via configuration `case class` which conta

```scala
import cats.effect._
import com.avast.sst.execution.ExecutorModule
import com.avast.sst.http4s._
import com.avast.sst.system.console.ConsoleModule
import com.avast.sst.http4s.client._
import com.avast.sst.http4s.server._
import com.avast.sst.jvm.execution.ExecutorModule
import com.avast.sst.jvm.system.console.ConsoleModule
import org.http4s.dsl.Http4sDsl
import org.http4s.HttpRoutes
import zio.DefaultRuntime
Expand Down Expand Up @@ -59,9 +60,9 @@ runtime.unsafeRun(program)

```scala
import cats.effect._
import com.avast.sst.execution.ExecutorModule
import com.avast.sst.http4s._
import com.avast.sst.http4s.middleware.CorrelationIdMiddleware
import com.avast.sst.http4s.server._
import com.avast.sst.http4s.server.middleware.CorrelationIdMiddleware
import com.avast.sst.jvm.execution.ExecutorModule
import org.http4s.dsl.Http4sDsl
import org.http4s.HttpRoutes
import zio.DefaultRuntime
Expand Down
Loading