/
HttpServer.scala
56 lines (49 loc) · 1.67 KB
/
HttpServer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.core
import akka.http.scaladsl.Http
import zio._
import org.knora.webapi.config.AppConfig
import org.knora.webapi.core
import org.knora.webapi.routing.ApiRoutes
/**
* The Akka based HTTP server
*/
trait HttpServer {
val serverBinding: Http.ServerBinding
}
object HttpServer {
val layer: ZLayer[ActorSystem with AppConfig with ApiRoutes, Nothing, HttpServer] =
ZLayer.scoped {
for {
as <- ZIO.service[core.ActorSystem]
config <- ZIO.service[AppConfig]
apiRoutes <- ZIO.service[ApiRoutes]
binding <- {
implicit val system: akka.actor.ActorSystem = as.system
ZIO.acquireRelease {
ZIO
.fromFuture(_ =>
Http().newServerAt(config.knoraApi.internalHost, config.knoraApi.internalPort).bind(apiRoutes.routes)
)
.zipLeft(ZIO.logInfo(">>> Acquire HTTP Server <<<"))
.orDie
} { serverBinding =>
ZIO
.fromFuture(_ =>
serverBinding.terminate(
new scala.concurrent.duration.FiniteDuration(1, scala.concurrent.duration.MILLISECONDS)
)
)
.zipLeft(ZIO.logInfo(">>> Release HTTP Server <<<"))
.orDie
}
}
} yield HttpServerImpl(binding)
}
private final case class HttpServerImpl(binding: Http.ServerBinding) extends HttpServer { self =>
val serverBinding: Http.ServerBinding = self.binding
}
}