Skip to content
This repository was archived by the owner on Aug 19, 2023. It is now read-only.

Commit eec1119

Browse files
committed
More build/etc
1 parent 3c8f73a commit eec1119

File tree

8 files changed

+91
-0
lines changed

8 files changed

+91
-0
lines changed

examples/fs2-echo/build.sbt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ThisBuild / resolvers += Resolver.sonatypeRepo("snapshots")
2+
3+
scalaVersion := "2.13.6"
4+
5+
libraryDependencies ++= Seq(
6+
"co.fs2" %% "fs2-io" % "3.0.1",
7+
"com.scalawilliam" %% "letsencrypt-ce3" % "0.0.5-SNAPSHOT"
8+
)
9+
10+
reStartArgs += "--insecure"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.5.5

examples/fs2-echo/project/plugins.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
2+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.8.1")
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.scalawilliam
2+
3+
import cats.effect.kernel.{Async, Sync}
4+
import cats.effect.{ExitCode, IO, IOApp, Resource}
5+
import com.comcast.ip4s.Port
6+
import com.scalawilliam.letsencrypt.LetsEncryptScala
7+
import fs2.io.net.tls.TLSContext
8+
import fs2.io.net.{Network, Socket}
9+
import fs2.{INothing, Stream, text}
10+
import cats.effect.Concurrent
11+
12+
object FS2LetsEncryptEchoApp extends IOApp {
13+
14+
type SocketHandler[F[_]] = Socket[F] => Stream[F, INothing]
15+
16+
/** Respond to a client socket with echoes - if they send a line, we return a line */
17+
private def handleEchoes[F[_]: Concurrent]: SocketHandler[F] =
18+
client =>
19+
client.reads
20+
.through(text.utf8Decode)
21+
.through(text.lines)
22+
.interleave(Stream.constant("\n"))
23+
.through(text.utf8Encode)
24+
.through(client.writes)
25+
/** This is VERY important; if you don't handle errors, the whole server will go down;
26+
* One example is if a plaintext client connects to an SSL server */
27+
.handleErrorWith(_ => Stream.empty)
28+
29+
private def runTcpServer[F[_]: Concurrent: Network](
30+
forHandler: SocketHandler[F]
31+
): F[Unit] =
32+
Network[F]
33+
.server(port = Port.fromInt(5555))
34+
.map(forHandler)
35+
.parJoin(100)
36+
.compile
37+
.drain
38+
39+
/** Utility function to pipe a socket through SSL.
40+
* This is a prime example of composition in functional programming:
41+
* you merely 'secure' a socket handler, without having to
42+
* modify any of the plaintext server code.
43+
*
44+
* We have to use a 'Resource' here because the SSL Context
45+
* is something we should let go of once not needed any more.
46+
* */
47+
private def secureHandler[F[_]: Concurrent: Sync: Async](
48+
originalHandler: SocketHandler[F]
49+
): Resource[F, SocketHandler[F]] =
50+
LetsEncryptScala
51+
.fromEnvironment[F]
52+
.flatMap(_.sslContextResource)
53+
.map(TLSContext.Builder.forAsync[F].fromSSLContext)
54+
.map { tlsContext => (clientSocket: Socket[F]) =>
55+
fs2.Stream
56+
.resource(tlsContext.server(clientSocket))
57+
.flatMap(originalHandler)
58+
}
59+
60+
private def secureConditionally[F[_]: Concurrent: Sync: Async](
61+
enableSecurity: Boolean,
62+
handler: SocketHandler[F]
63+
): Resource[F, SocketHandler[F]] =
64+
if (enableSecurity) secureHandler(handler)
65+
else Resource.pure(handler)
66+
67+
override def run(args: List[String]): IO[ExitCode] =
68+
secureConditionally[IO](
69+
enableSecurity = !args.contains("--insecure"),
70+
handler = handleEchoes
71+
).use(handler => runTcpServer(handler).as(ExitCode.Success))
72+
73+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.5.5

examples/http4s/project/plugins.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
2+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.8.1")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.5.5

examples/play/project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.8")

0 commit comments

Comments
 (0)