-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
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
49 changes: 49 additions & 0 deletions
49
http4s-scala-fx/src/main/scala/http4s/fx/BlazeServerFXBackend.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,49 @@ | ||
package http4s | ||
package fx | ||
|
||
import _root_.fx.handle | ||
import _root_.fx.instances.StructuredF | ||
import _root_.fx.instances.FxAsync.asyncInstance | ||
import _root_.fx.{Control, Errors, Nullable, Resource, Resources, Structured} | ||
import org.http4s.HttpApp | ||
import org.http4s.blaze.server.BlazeServerBuilder | ||
import org.http4s.server.{Server, defaults} | ||
|
||
class BlazeServerFXBackend private ( | ||
app: HttpApp[StructuredF] | ||
)( | ||
using structured: Structured, | ||
control: Control[Throwable], | ||
config: HttpServerConfig | ||
) { | ||
private var serverFinalizers: Nullable[(Server, StructuredF[Unit])] = Nullable.none | ||
|
||
private val builder: BlazeServerBuilder[StructuredF] = | ||
BlazeServerBuilder[StructuredF] | ||
.bindHttp(config.port.getOrElse(defaults.HttpPort), config.host.getOrElse(defaults.IPv4Host)) | ||
.withIdleTimeout(config.idleTimeout.getOrElse(defaults.IdleTimeout)) | ||
.withSelectorThreadFactory(structured.threadFactory) | ||
.withHttpApp(app) | ||
|
||
def start(): BlazeServerFXBackend = | ||
serverFinalizers = handle( | ||
Nullable(builder.resource.allocated) | ||
)((e: Throwable) => e.shift) | ||
this | ||
|
||
def close(): Unit = | ||
serverFinalizers.map{ case (_, finalizers) => finalizers }.getOrElse(()) | ||
|
||
def server(): Server = | ||
handle( | ||
serverFinalizers.map{ case (server, _) => server }.value | ||
)((e: Throwable) => e.shift) | ||
} | ||
|
||
object BlazeServerFXBackend: | ||
def apply(app: HttpApp[StructuredF])( | ||
using structured: Structured, | ||
control: Control[Throwable], | ||
config: HttpServerConfig | ||
): Resource[BlazeServerFXBackend] = | ||
Resource(new BlazeServerFXBackend(app).start(), (server, _) => server.close()) |
20 changes: 20 additions & 0 deletions
20
http4s-scala-fx/src/main/scala/http4s/fx/HttpServerConfig.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,20 @@ | ||
package http4s | ||
package fx | ||
|
||
import _root_.fx.Nullable | ||
|
||
import java.net.InetSocketAddress | ||
import scala.concurrent.duration.Duration | ||
|
||
/** | ||
* Defines the server configuration options for an http server. Not open for extension. | ||
*/ | ||
final class HttpServerConfig( | ||
val port: Nullable[Int], | ||
val host: Nullable[String], | ||
val idleTimeout: Nullable[Duration] | ||
) | ||
|
||
object HttpServerConfig: | ||
given HttpServerConfig = | ||
HttpServerConfig(Nullable.none, Nullable.none, Nullable.none) |
39 changes: 39 additions & 0 deletions
39
http4s-scala-fx/src/test/scala/http4s/fx/BlazeServerFXBackendSuite.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,39 @@ | ||
package http4s | ||
package fx | ||
|
||
import _root_.fx.{defaultRetryPolicy, parallel, structured, GET, POST, Structured} | ||
import munit.fx.ScalaFXSuite | ||
|
||
import java.net.URI | ||
|
||
class BlazeServerFXBackendSuite extends ScalaFXSuite, HttpServerFixtures: | ||
|
||
httpServerHttp4s(pingPongApp) | ||
.testFX("GET requests should be returned in non-blocking fibers") { serverResource => | ||
serverResource.use { baseServerAddress => | ||
println(baseServerAddress) | ||
|
||
val pongResponse: Structured ?=> (String, String, String) = | ||
parallel( | ||
() => URI.create(s"$baseServerAddress/ping/1").GET[String]().body, | ||
() => URI.create(s"$baseServerAddress/ping/2").GET[String]().body, | ||
() => URI.create(s"$baseServerAddress/ping/3").GET[String]().body | ||
) | ||
|
||
assertEqualsFX( | ||
structured(pongResponse), | ||
("pong", "pong", "pong") | ||
) | ||
} | ||
} | ||
|
||
httpServerHttp4s(echoApp) | ||
.testFX("POST requests should be returned".ignore) { serverResource => | ||
serverResource.use { baseServerAddress => | ||
|
||
val response = structured(URI.create(s"$baseServerAddress/echo").POST[String, String]("hello")) | ||
|
||
assertEqualsFX(response.body, "hello") | ||
assertEqualsFX(response.statusCode, 200) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
http4s-scala-fx/src/test/scala/http4s/fx/HttpServerFixtures.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,38 @@ | ||
package http4s | ||
package fx | ||
|
||
import _root_.fx.{Control, Resource, handle, structured, toEither} | ||
import _root_.fx.instances.FxAsync.asyncInstance | ||
import _root_.fx.instances.StructuredF | ||
import _root_.fx.HttpExecutionException | ||
import fx.BlazeServerFXBackend | ||
import munit.FunSuite | ||
import org.http4s.Method.{GET, POST} | ||
import org.http4s.{HttpApp, HttpRoutes, Response} | ||
|
||
trait HttpServerFixtures: | ||
self: FunSuite => | ||
|
||
val pingPongApp: HttpApp[StructuredF] = | ||
HttpApp.apply { | ||
case r if r.method == GET && r.uri.path.renderString.contains("ping") => | ||
Response[StructuredF]().withEntity[String]("pong") | ||
} | ||
|
||
val echoApp: HttpApp[StructuredF] = | ||
HttpApp.apply { | ||
case r if r.method == POST && r.uri.path.renderString.contains("echo") => | ||
Response[StructuredF]().withBodyStream(r.body) | ||
} | ||
|
||
def httpServerHttp4s(app: HttpApp[StructuredF]): FunFixture[Resource[String]] = | ||
FunFixture( | ||
setup = _ => { | ||
for { | ||
server <- toEither( | ||
structured(BlazeServerFXBackend(app)) | ||
).fold(e => throw e, identity) | ||
} yield s"http:/${server.server().address}" | ||
}, | ||
teardown = _ => () | ||
) |
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