Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Balduin Landolt <33053745+BalduinLandolt@users.noreply.github.com>
- Loading branch information
1 parent
3bf3fe6
commit 7ae6d24
Showing
47 changed files
with
1,311 additions
and
186 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
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,34 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE configuration> | ||
|
||
<configuration > | ||
<import class="ch.qos.logback.core.ConsoleAppender"/> | ||
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/> | ||
<import class="ch.qos.logback.contrib.json.classic.JsonLayout"/> | ||
<import class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter"/> | ||
|
||
<appender name="STDOUT" class="ConsoleAppender"> | ||
<encoder class="PatternLayoutEncoder"> | ||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg [%mdc]%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<appender name="JSON" class="ConsoleAppender"> | ||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | ||
<layout class="JsonLayout"> | ||
<jsonFormatter class="JacksonJsonFormatter"> | ||
<prettyPrint>true</prettyPrint> | ||
</jsonFormatter> | ||
<timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat> | ||
</layout> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
<!-- <appender-ref ref="JSON"/> --> | ||
</root> | ||
|
||
<!-- for logging during tests, please see/use logback-test.xml under test/resources --> | ||
|
||
</configuration> |
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,35 @@ | ||
package dsp.api.main | ||
|
||
import zio._ | ||
import dsp.user.route.UserRoutes | ||
import dsp.user.handler.UserHandler | ||
import dsp.user.repo.impl.UserRepoLive | ||
import zio.logging.removeDefaultLoggers | ||
import zio.logging.backend.SLF4J | ||
import dsp.config.AppConfig | ||
import dsp.util.UuidGeneratorLive | ||
|
||
object DspMain extends ZIOAppDefault { | ||
|
||
override val run: Task[Unit] = | ||
ZIO | ||
.serviceWithZIO[DspServer](_.start) | ||
.provide( | ||
// ZLayer.Debug.mermaid, | ||
// server | ||
DspServer.layer, | ||
// configuration | ||
AppConfig.live, | ||
// routes | ||
UserRoutes.layer, | ||
// handlers | ||
UserHandler.layer, | ||
// repositories | ||
UserRepoLive.layer, | ||
// slf4j facade, we use it with logback.xml | ||
removeDefaultLoggers, | ||
SLF4J.slf4j, | ||
UuidGeneratorLive.layer | ||
) | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
dsp-api-main/src/main/scala/dsp/api/main/DspMiddleware.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,24 @@ | ||
package dsp.api.main | ||
|
||
import zhttp.http.middleware.HttpMiddleware | ||
import zhttp.http._ | ||
import zio._ | ||
|
||
object DspMiddleware { | ||
// adds a requestId to all logs that were triggered by the same request | ||
val logging: HttpMiddleware[Any, Nothing] = | ||
new HttpMiddleware[Any, Nothing] { | ||
override def apply[R1 <: Any, E1 >: Nothing]( | ||
http: HttpApp[R1, E1] | ||
): HttpApp[R1, E1] = | ||
Http.fromOptionFunction[Request] { request => | ||
Random.nextUUID.flatMap { requestId => | ||
ZIO.logAnnotate("RequestId", requestId.toString) { | ||
for { | ||
result <- http(request) | ||
} yield result | ||
} | ||
} | ||
} | ||
} | ||
} |
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,32 @@ | ||
package dsp.api.main | ||
|
||
import dsp.user.route.UserRoutes | ||
import zio._ | ||
import zhttp.http._ | ||
import zio.ZLayer | ||
import zhttp.service.Server | ||
import dsp.api.main.DspMiddleware | ||
import dsp.config.AppConfig | ||
import dsp.util.UuidGeneratorLive | ||
|
||
final case class DspServer( | ||
appConfig: AppConfig, | ||
userRoutes: UserRoutes | ||
) { | ||
|
||
// adds up the routes of all slices | ||
val dspRoutes: HttpApp[AppConfig & UuidGeneratorLive, Throwable] = | ||
userRoutes.routes // ++ projectRoutes.routes | ||
|
||
// starts the server with the provided settings from the appConfig | ||
def start = { | ||
val port = appConfig.dspApi.externalPort | ||
Server.start(port, dspRoutes @@ DspMiddleware.logging) | ||
} | ||
|
||
} | ||
|
||
object DspServer { | ||
val layer: ZLayer[AppConfig & UserRoutes, Nothing, DspServer] = | ||
ZLayer.fromFunction(DspServer.apply _) | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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,19 @@ | ||
app { | ||
dsp-api { | ||
// relevant for direct communication inside the dsp stack | ||
internal-host = "0.0.0.0" | ||
internal-host = ${?DSP_API_INTERNAL_HOST} | ||
internal-port = 4444 | ||
internal-port = ${?DSP_API_INTERNAL_PORT} | ||
|
||
// relevant for the client, i.e. browser | ||
external-protocol = "http" // optional ssl termination needs to be done by the proxy | ||
external-protocol = ${?DSP_API_EXTERNAL_PROTOCOL} | ||
external-host = "0.0.0.0" | ||
external-host = ${?DSP_API_EXTERNAL_HOST} | ||
external-port = 4444 | ||
external-port = ${?DSP_API_EXTERNAL_PORT} | ||
} | ||
|
||
bcrypt-password-strength = 12 | ||
} |
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,46 @@ | ||
package dsp.config | ||
|
||
import com.typesafe.config.ConfigFactory | ||
import zio._ | ||
import zio.config._ | ||
import zio.config.magnolia.descriptor | ||
import zio.config.typesafe.TypesafeConfigSource | ||
|
||
import dsp.valueobjects.User._ | ||
|
||
/** | ||
* Configuration | ||
*/ | ||
final case class AppConfig( | ||
dspApi: DspApi, | ||
bcryptPasswordStrength: PasswordStrength | ||
) | ||
|
||
final case class DspApi( | ||
internalHost: String, | ||
internalPort: Int, | ||
externalHost: String, | ||
externalPort: Int | ||
) | ||
|
||
object AppConfig { | ||
|
||
/** | ||
* Reads in the applicaton configuration using ZIO-Config. ZIO-Config is capable of loading | ||
* the Typesafe-Config format. Reads the 'app' configuration from 'application.conf'. | ||
*/ | ||
private val source: ConfigSource = | ||
TypesafeConfigSource.fromTypesafeConfig(ZIO.attempt(ConfigFactory.load().getConfig("app").resolve)) | ||
|
||
/** | ||
* Instantiates the config class hierarchy using the data from the 'app' configuration from 'application.conf'. | ||
*/ | ||
private val configFromSource: IO[ReadError[String], AppConfig] = read( | ||
descriptor[AppConfig].mapKey(toKebabCase) from source | ||
) | ||
|
||
/** | ||
* Application configuration from application.conf | ||
*/ | ||
val live: ULayer[AppConfig] = ZLayer(configFromSource.orDie) | ||
} |
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,34 @@ | ||
/* | ||
* 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 dsp.util | ||
|
||
import zio._ | ||
|
||
import java.util.UUID | ||
|
||
/** | ||
* Handles UUID creation | ||
*/ | ||
trait UuidGenerator { | ||
def createRandomUuid: UIO[UUID] | ||
} | ||
|
||
/** | ||
* Live instance of the UuidGenerator | ||
*/ | ||
final case class UuidGeneratorLive() extends UuidGenerator { | ||
|
||
/** | ||
* Creates a random UUID | ||
* | ||
* @return a random UUID | ||
*/ | ||
override def createRandomUuid: UIO[UUID] = ZIO.succeed(UUID.randomUUID()) | ||
} | ||
object UuidGeneratorLive { | ||
val layer: ULayer[UuidGeneratorLive] = | ||
ZLayer.succeed(UuidGeneratorLive()) | ||
} |
Oops, something went wrong.