-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.scala
116 lines (101 loc) · 4.74 KB
/
Server.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package za.co.absa.atum.server.api.http
import cats.syntax.semigroupk._
import org.http4s.HttpRoutes
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.Router
import sttp.monad.MonadError
import sttp.tapir.generic.auto.schemaForCaseClass
import sttp.tapir.json.play.jsonBody
import sttp.tapir.server.http4s.Http4sServerOptions
import sttp.tapir.server.http4s.ztapir.ZHttp4sServerInterpreter
import sttp.tapir.server.interceptor.DecodeFailureContext
import sttp.tapir.server.interceptor.decodefailure.DecodeFailureHandler
import sttp.tapir.server.interceptor.decodefailure.DefaultDecodeFailureHandler.respond
import sttp.tapir.server.model.ValuedEndpointOutput
import sttp.tapir.swagger.bundle.SwaggerInterpreter
import sttp.tapir.ztapir._
import sttp.tapir.{DecodeResult, PublicEndpoint, headers, statusCode}
import za.co.absa.atum.server.Constants.{SwaggerApiName, SwaggerApiVersion}
import za.co.absa.atum.server.api.controller._
import za.co.absa.atum.server.config.SslConfig
import za.co.absa.atum.server.model.BadRequestResponse
import zio.interop.catz._
import zio._
import javax.net.ssl.SSLContext
trait Server extends Endpoints {
type Env = PartitioningController with CheckpointController
type F[A] = RIO[Env, A]
private val decodeFailureHandler: DecodeFailureHandler[F] = new DecodeFailureHandler[F] {
override def apply(ctx: DecodeFailureContext)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]] = {
monad.unit(
respond(ctx).map { case (sc, hs) =>
val message = ctx.failure match {
case DecodeResult.Missing => s"Decoding error - missing value."
case DecodeResult.Multiple(vs) => s"Decoding error - $vs."
case DecodeResult.Error(original, _) => s"Decoding error for an input value '$original'."
case DecodeResult.Mismatch(_, actual) => s"Unexpected value '$actual'."
case DecodeResult.InvalidValue(errors) => s"Validation error - $errors."
}
val errorResponse = BadRequestResponse(message)
ValuedEndpointOutput(statusCode.and(headers).and(jsonBody[BadRequestResponse]), (sc, hs, errorResponse))
}
)
}
}
private val http4sServerOptions: Http4sServerOptions[F] = Http4sServerOptions
.customiseInterceptors[F]
.decodeFailureHandler(decodeFailureHandler)
.options
private def createServerEndpoint[I, E, O](
endpoint: PublicEndpoint[I, E, O, Any],
logic: I => ZIO[Env, E, O]
): ZServerEndpoint[Env, Any] = {
endpoint.zServerLogic(logic).widen[Env]
}
private def createAllServerRoutes: HttpRoutes[F] = {
val endpoints = List(
createServerEndpoint(createCheckpointEndpoint, CheckpointController.createCheckpoint),
createServerEndpoint(createPartitioningEndpoint, PartitioningController.createPartitioningIfNotExists),
createServerEndpoint(healthEndpoint, (_: Unit) => ZIO.unit)
)
ZHttp4sServerInterpreter[Env](http4sServerOptions).from(endpoints).toRoutes
}
private def createSwaggerRoutes: HttpRoutes[F] = {
val endpoints = List(createCheckpointEndpoint, createPartitioningEndpoint)
ZHttp4sServerInterpreter[Env](http4sServerOptions)
.from(SwaggerInterpreter().fromEndpoints[F](endpoints, SwaggerApiName, SwaggerApiVersion))
.toRoutes
}
private def createServer(port: Int, sslContext: Option[SSLContext] = None): ZIO[Env, Throwable, Unit] =
ZIO.executor.flatMap { executor =>
val builder = BlazeServerBuilder[F]
.bindHttp(port, "0.0.0.0")
.withExecutionContext(executor.asExecutionContext)
.withHttpApp(Router("/" -> (createAllServerRoutes <+> createSwaggerRoutes)).orNotFound)
val builderWithSsl = sslContext.fold(builder)(ctx => builder.withSslContext(ctx))
builderWithSsl.serve.compile.drain
}
private val httpServer: ZIO[Env, Throwable, Unit] = createServer(8080)
private val httpsServer: ZIO[Env, Throwable, Unit] = SSL.context.flatMap { context =>
createServer(8443, Some(context))
}
protected val server: ZIO[Env, Throwable, Unit] = for {
sslConfig <- ZIO.config[SslConfig](SslConfig.config)
server <- if (sslConfig.enabled) httpsServer else httpServer
} yield server
}