Skip to content

Commit 12aa79d

Browse files
authored
Merge pull request #30 from avast/feat/BundlesZioMonix
feat: Add Monix and ZIO bundles, Micrometer and PureConfig interop
2 parents d3be870 + db96dc4 commit 12aa79d

File tree

66 files changed

+1077
-158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1077
-158
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Scala Server Toolkit
22

33
[![Build Status](https://travis-ci.org/avast/scala-server-toolkit.svg?branch=master)](https://travis-ci.org/avast/scala-server-toolkit)
4-
[![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/)
4+
[![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/)
55
[![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)
66

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

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

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

1515
## Design
1616

build.sbt

Lines changed: 115 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,52 @@ lazy val commonSettings = BuildSettings.common ++ Seq(
1717

1818
lazy val root = project
1919
.in(file("."))
20-
.aggregate(example, http4sBlazeClient, http4sBlazeServer, jvmExecution, jvmSsl, jvmSystem, pureconfig)
20+
.aggregate(
21+
bundleMonixHttp4sBlaze,
22+
bundleZioHttp4sBlaze,
23+
example,
24+
http4sClientBlaze,
25+
http4sClientBlazePureConfig,
26+
http4sServer,
27+
http4sServerBlaze,
28+
http4sServerBlazePureConfig,
29+
http4sServerMicrometer,
30+
jvm,
31+
jvmMicrometer,
32+
jvmPureConfig,
33+
micrometerJmx,
34+
micrometerJmxPureConfig,
35+
pureConfig
36+
)
2137
.settings(
2238
name := "scala-server-toolkit",
2339
publish / skip := true
2440
)
2541

42+
lazy val bundleMonixHttp4sBlaze = project
43+
.in(file("bundle-monix-http4s-blaze"))
44+
.dependsOn(http4sClientBlaze, http4sServerBlaze, http4sServerBlazePureConfig, http4sServerMicrometer, jvmMicrometer, jvmPureConfig)
45+
.settings(commonSettings)
46+
.settings(
47+
name := "sst-bundle-monix-http4s-blaze",
48+
libraryDependencies += Dependencies.monixEval
49+
)
50+
51+
lazy val bundleZioHttp4sBlaze = project
52+
.in(file("bundle-zio-http4s-blaze"))
53+
.dependsOn(http4sClientBlaze, http4sServerBlaze, http4sServerBlazePureConfig, http4sServerMicrometer, jvmMicrometer, jvmPureConfig)
54+
.settings(commonSettings)
55+
.settings(
56+
name := "sst-bundle-zio-http4s-blaze",
57+
libraryDependencies ++= Seq(
58+
Dependencies.zio,
59+
Dependencies.zioInteropCats
60+
)
61+
)
62+
2663
lazy val example = project
27-
.dependsOn(jvmExecution, http4sBlazeClient, http4sBlazeServer, jvmSsl, jvmSystem, pureconfig)
64+
.in(file("example"))
65+
.dependsOn(bundleZioHttp4sBlaze, micrometerJmxPureConfig)
2866
.enablePlugins(MdocPlugin)
2967
.settings(commonSettings)
3068
.settings(
@@ -34,57 +72,107 @@ lazy val example = project
3472
Global / cancelable := true,
3573
mdocIn := baseDirectory.value / "src" / "main" / "mdoc",
3674
mdocOut := baseDirectory.value / ".." / "docs",
37-
libraryDependencies ++= Seq(
38-
Dependencies.zio,
39-
Dependencies.zioInteropCats
40-
)
75+
libraryDependencies += Dependencies.logbackClassic
4176
)
4277

43-
lazy val http4sBlazeClient = project
44-
.in(file("http4s-blaze-client"))
45-
.dependsOn(jvmSsl)
78+
lazy val http4sClientBlaze = project
79+
.in(file("http4s-client-blaze"))
80+
.dependsOn(jvm)
4681
.settings(commonSettings)
4782
.settings(
48-
name := "sst-http4s-blaze-client",
83+
name := "sst-http4s-client-blaze",
4984
libraryDependencies += Dependencies.http4sBlazeClient
5085
)
5186

52-
lazy val http4sBlazeServer = project
53-
.in(file("http4s-blaze-server"))
54-
.dependsOn(http4sBlazeClient % Test)
87+
lazy val http4sClientBlazePureConfig = project
88+
.in(file("http4s-client-blaze-pureconfig"))
89+
.dependsOn(http4sClientBlaze, jvmPureConfig)
90+
.settings(commonSettings)
91+
.settings(name := "sst-http4s-client-blaze-pureconfig")
92+
93+
lazy val http4sServer = project
94+
.in(file("http4s-server"))
95+
.settings(commonSettings)
96+
.settings(
97+
name := "sst-http4s-server",
98+
libraryDependencies ++= Seq(
99+
Dependencies.http4sServer,
100+
Dependencies.http4sBlazeClient % Test,
101+
Dependencies.http4sBlazeServer % Test,
102+
Dependencies.http4sDsl % Test
103+
)
104+
)
105+
106+
lazy val http4sServerBlaze = project
107+
.in(file("http4s-server-blaze"))
108+
.dependsOn(http4sServer, http4sClientBlaze % Test)
55109
.settings(commonSettings)
56110
.settings(
57-
name := "sst-http4s-blaze-server",
111+
name := "sst-http4s-server-blaze",
58112
libraryDependencies ++= Seq(
59113
Dependencies.http4sBlazeServer,
60114
Dependencies.http4sDsl,
61115
Dependencies.slf4jApi
62116
)
63117
)
64118

65-
lazy val jvmExecution = project
66-
.in(file("jvm-execution"))
119+
lazy val http4sServerBlazePureConfig = project
120+
.in(file("http4s-server-blaze-pureconfig"))
121+
.dependsOn(http4sServerBlaze, pureConfig)
122+
.settings(commonSettings)
123+
.settings(name := "sst-http4s-server-blaze-pureconfig")
124+
125+
lazy val http4sServerMicrometer = project
126+
.in(file("http4s-server-micrometer"))
127+
.dependsOn(http4sServer)
128+
.settings(commonSettings)
129+
.settings(
130+
name := "sst-http4s-server-micrometer",
131+
libraryDependencies += Dependencies.micrometerCore
132+
)
133+
134+
lazy val jvm = project
135+
.in(file("jvm"))
136+
.settings(commonSettings)
67137
.settings(
68-
commonSettings,
69-
name := "sst-jvm-execution",
138+
name := "sst-jvm",
70139
libraryDependencies += Dependencies.slf4jApi
71140
)
72141

73-
lazy val jvmSsl = project
74-
.in(file("jvm-ssl"))
142+
lazy val jvmMicrometer = project
143+
.in(file("jvm-micrometer"))
144+
.dependsOn(jvm)
145+
.settings(commonSettings)
75146
.settings(
76-
commonSettings,
77-
name := "sst-jvm-ssl"
147+
name := "sst-jvm-micrometer",
148+
libraryDependencies += Dependencies.micrometerCore
78149
)
79150

80-
lazy val jvmSystem = project
81-
.in(file("jvm-system"))
151+
lazy val jvmPureConfig = project
152+
.in(file("jvm-pureconfig"))
153+
.dependsOn(jvm, pureConfig)
154+
.settings(commonSettings)
155+
.settings(name := "sst-jvm-pureconfig")
156+
157+
lazy val micrometerJmx = project
158+
.in(file("micrometer-jmx"))
159+
.settings(commonSettings)
82160
.settings(
83-
commonSettings,
84-
name := "sst-jvm-system"
161+
name := "sst-micrometer-jmx",
162+
libraryDependencies ++= Seq(
163+
Dependencies.micrometerJmx,
164+
Dependencies.jsr305 // required because of Scala compiler
165+
)
85166
)
86167

87-
lazy val pureconfig = project
168+
lazy val micrometerJmxPureConfig = project
169+
.in(file("micrometer-jmx-pureconfig"))
170+
.dependsOn(micrometerJmx, pureConfig)
171+
.settings(commonSettings)
172+
.settings(name := "sst-micrometer-jmx-pureconfig")
173+
174+
lazy val pureConfig = project
175+
.in(file("pureconfig"))
88176
.settings(commonSettings)
89177
.settings(
90178
name := "sst-pureconfig",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.avast.sst.bundle
2+
3+
import cats.effect.{ExitCode, Resource}
4+
import monix.eval.{Task, TaskApp}
5+
import org.http4s.server.Server
6+
import org.slf4j.LoggerFactory
7+
8+
/** Extend this `trait` if you want to implement server application using [[monix.eval.Task]] effect data type.
9+
*
10+
* Implement method `program` with initialization and business logic of your application. It will be automatically run until JVM is shut
11+
* down in which case all the resources are cleaned up because the whole `program` is a [[cats.effect.Resource]].
12+
*/
13+
trait MonixServerApp extends TaskApp {
14+
15+
private val logger = LoggerFactory.getLogger(this.getClass)
16+
17+
def program: Resource[Task, Server[Task]]
18+
19+
override def run(args: List[String]): Task[ExitCode] = {
20+
program
21+
.use { server =>
22+
for {
23+
_ <- Task.delay(logger.info(s"Server started @ ${server.address.getHostString}:${server.address.getPort}"))
24+
_ <- Task.never[Unit]
25+
} yield server
26+
}
27+
.redeem(
28+
ex => {
29+
logger.error("Server initialization failed!", ex)
30+
ExitCode.Error
31+
},
32+
_ => ExitCode.Success
33+
)
34+
}
35+
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.avast.sst.bundle
2+
3+
import cats.effect.Resource
4+
import com.github.ghik.silencer.silent
5+
import org.http4s.server.Server
6+
import org.slf4j.LoggerFactory
7+
import zio.interop.catz._
8+
import zio.{Task, UIO, ZIO}
9+
10+
/** Extend this `trait` if you want to implement server application using [[zio.ZIO]] effect data type.
11+
*
12+
* Implement method `program` with initialization and business logic of your application. It will be automatically run until JVM is shut
13+
* down in which case all the resources are cleaned up because the whole `program` is a [[cats.effect.Resource]].
14+
*/
15+
trait ZioServerApp extends CatsApp {
16+
17+
private val logger = LoggerFactory.getLogger(this.getClass)
18+
19+
def program: Resource[Task, Server[Task]]
20+
21+
@silent("dead code")
22+
override def run(args: List[String]): ZIO[Environment, Nothing, Int] = {
23+
program
24+
.use { server =>
25+
for {
26+
_ <- UIO.effectTotal(logger.info(s"Server started @ ${server.address.getHostString}:${server.address.getPort}"))
27+
_ <- Task.never
28+
} yield server
29+
}
30+
.fold(
31+
ex => {
32+
logger.error("Server initialization failed!", ex)
33+
1
34+
},
35+
_ => 0
36+
)
37+
}
38+
39+
}

docs/http4s.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Module http4s
22

3-
[![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/)
3+
[![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/)
44

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

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

1212
```scala
1313
import cats.effect._
14-
import com.avast.sst.execution.ExecutorModule
15-
import com.avast.sst.http4s._
16-
import com.avast.sst.system.console.ConsoleModule
14+
import com.avast.sst.http4s.client._
15+
import com.avast.sst.http4s.server._
16+
import com.avast.sst.jvm.execution.ExecutorModule
17+
import com.avast.sst.jvm.system.console.ConsoleModule
1718
import org.http4s.dsl.Http4sDsl
1819
import org.http4s.HttpRoutes
1920
import zio.DefaultRuntime
@@ -59,9 +60,9 @@ runtime.unsafeRun(program)
5960

6061
```scala
6162
import cats.effect._
62-
import com.avast.sst.execution.ExecutorModule
63-
import com.avast.sst.http4s._
64-
import com.avast.sst.http4s.middleware.CorrelationIdMiddleware
63+
import com.avast.sst.http4s.server._
64+
import com.avast.sst.http4s.server.middleware.CorrelationIdMiddleware
65+
import com.avast.sst.jvm.execution.ExecutorModule
6566
import org.http4s.dsl.Http4sDsl
6667
import org.http4s.HttpRoutes
6768
import zio.DefaultRuntime

0 commit comments

Comments
 (0)