Skip to content

Commit

Permalink
build: Replace io.d11:zhttp lib with dev.zio:zio-http (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Apr 26, 2023
1 parent 9d587ff commit be7fe96
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 92 deletions.
17 changes: 8 additions & 9 deletions build.sbt
Expand Up @@ -41,7 +41,7 @@ lazy val V = new {
val zioConfig = "3.0.2"
val zioLogging = "2.0.0"
val zioJson = "0.3.0"
val zioHttp = "2.0.0-RC11"
val zioHttp = "0.0.3"
val zioCatsInterop = "3.3.0"
val zioMetrics = "2.0.6"

Expand Down Expand Up @@ -81,14 +81,13 @@ lazy val D = new {
val zioLog: ModuleID = "dev.zio" %% "zio-logging" % V.zioLogging
val zioSLF4J: ModuleID = "dev.zio" %% "zio-logging-slf4j" % V.zioLogging
val zioJson: ModuleID = "dev.zio" %% "zio-json" % V.zioJson
val zioHttp: ModuleID = "dev.zio" %% "zio-http" % "0.0.3" // TODO version
val zioHttpD11 = "io.d11" %% "zhttp" % V.zioHttp // FIXME!!!!!!!
val zioCatsInterop = "dev.zio" %% "zio-interop-cats" % V.zioCatsInterop
val zioMetrics = "dev.zio" %% "zio-metrics-connectors" % V.zioMetrics
val zioHttp: ModuleID = "dev.zio" %% "zio-http" % V.zioHttp
val zioCatsInterop: ModuleID = "dev.zio" %% "zio-interop-cats" % V.zioCatsInterop
val zioMetrics: ModuleID = "dev.zio" %% "zio-metrics-connectors" % V.zioMetrics

val zioConfig = "dev.zio" %% "zio-config" % V.zioConfig
val zioConfigMagnolia = "dev.zio" %% "zio-config-magnolia" % V.zioConfig
val zioConfigTypesafe = "dev.zio" %% "zio-config-typesafe" % V.zioConfig
val zioConfig: ModuleID = "dev.zio" %% "zio-config" % V.zioConfig
val zioConfigMagnolia: ModuleID = "dev.zio" %% "zio-config-magnolia" % V.zioConfig
val zioConfigTypesafe: ModuleID = "dev.zio" %% "zio-config-typesafe" % V.zioConfig

val circeCore: ModuleID = "io.circe" %% "circe-core" % V.circe
val circeGeneric: ModuleID = "io.circe" %% "circe-generic" % V.circe
Expand Down Expand Up @@ -318,7 +317,7 @@ lazy val D_PrismAgent = new {
D.zioConfigTypesafe,
D.zioJson,
logback,
D.zioHttpD11,
D.zioHttp,
D.zioMetrics,
)
val akkaHttpDependencies: Seq[ModuleID] =
Expand Down
8 changes: 2 additions & 6 deletions infrastructure/shared/apisix/conf/apisix.yaml
Expand Up @@ -3,7 +3,7 @@ plugins:

routes:
- uri: /prism-agent/_system/*
upstream_id: 6
upstream_id: 3
plugins:
proxy-rewrite:
regex_uri: ["^/prism-agent/_system/(.*)", "/$1"]
Expand Down Expand Up @@ -75,7 +75,7 @@ upstreams:
type: roundrobin
- id: 3
nodes:
"prism-agent:8090": 1 # didcom
"prism-agent:8090": 1 # didcom and system
type: roundrobin
- id: 4
nodes:
Expand All @@ -85,8 +85,4 @@ upstreams:
nodes:
"swagger-ui:8080": 1
type: roundrobin
- id: 6
nodes:
"prism-agent:8082": 1 # _system
type: roundrobin
#END
28 changes: 28 additions & 0 deletions infrastructure/shared/docker-compose-db.yml
@@ -0,0 +1,28 @@
---
version: "3.8"

services:
##########################
# Database
##########################
db:
image: postgres:13
environment:
POSTGRES_MULTIPLE_DATABASES: "castor,pollux,connect,iris,agent,node_db"
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- pg_data_db:/var/lib/postgresql/data
- ./postgres/init-script.sh:/docker-entrypoint-initdb.d/init-script.sh
- ./postgres/max_conns.sql:/docker-entrypoint-initdb.d/max_conns.sql
ports:
- "5433:5432"

healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "iris"]
interval: 10s
timeout: 5s
retries: 5

volumes:
pg_data_db:
Expand Up @@ -77,7 +77,8 @@ agent {
port =${?AGENT_HTTP_PORT}
}
}
didCommServiceEndpointUrl = ${DIDCOMM_SERVICE_URL}
didCommServiceEndpointUrl = "http://localhost:8090"
didCommServiceEndpointUrl = ${?DIDCOMM_SERVICE_URL}
database {
host = "localhost"
host = ${?AGENT_DB_HOST}
Expand Down
Expand Up @@ -16,8 +16,9 @@ import org.flywaydb.core.extensibility.AppliedMigration
import io.iohk.atala.pollux.core.service.CredentialSchemaServiceImpl
import io.iohk.atala.pollux.sql.repository.JdbcCredentialSchemaRepository
import io.iohk.atala.agent.walletapi.sql.JdbcDIDSecretStorage
import zhttp.http.*
import zhttp.service.Server
import zio.http.*
import zio.http.model.*
import zio.http.ZClient.ClientLive
import zio.metrics.connectors.prometheus.PrometheusPublisher
import zio.metrics.connectors.{MetricsConfig, prometheus}
import zio.metrics.jvm.DefaultJvmMetrics
Expand All @@ -31,41 +32,7 @@ import io.iohk.atala.connect.controller.ConnectionControllerImpl
import io.iohk.atala.castor.controller.DIDControllerImpl

import java.security.Security

object SystemInfoApp extends ZIOAppDefault {
private val metricsConfig = ZLayer.succeed(MetricsConfig(5.seconds))

def run =
for {
systemServicePort <- System.env("SYSTEM_SERVICE_PORT").map {
case Some(s) if s.toIntOption.isDefined => s.toInt
case _ => 8082
}
_ <- Server
.start(
port = systemServicePort,
http = Http.collectZIO[Request] {
case Method.GET -> !! / "metrics" =>
ZIO.serviceWithZIO[PrometheusPublisher](_.get.map(Response.text))
case Method.GET -> !! / "health" =>
ZIO
.succeed(
Response.json(
HealthInfo(
version = BuildInfo.version
).asJson.toString
)
)
}
)
.provide(
metricsConfig,
prometheus.publisherLayer,
prometheus.prometheusLayer
)
} yield ()

}
import io.iohk.atala.agent.server.http.HttpRoutes

object AgentApp extends ZIOAppDefault {

Expand All @@ -86,14 +53,31 @@ object AgentApp extends ZIOAppDefault {
_ <- ZIO.serviceWithZIO[AgentMigrations](_.migrate)
} yield ()

def serverProgram(didCommServicePort: Int) = {
val server = {
val config = ServerConfig(address = new java.net.InetSocketAddress(didCommServicePort))
ServerConfig.live(config)(using Trace.empty) >>> Server.live
}
for {
_ <- ZIO.logInfo(s"Server Started on port $didCommServicePort")
myServer <- {
Server
.serve(Modules.didCommServiceEndpoint ++ SystemInfoApp.app)
.provideSomeLayer(server)
.debug *> ZIO.logWarning(s"Server STOP (on port $didCommServicePort)")
}.fork
} yield (myServer)
}

def appComponents(didCommServicePort: Int, restServicePort: Int) = for {
_ <- Modules.issueCredentialDidCommExchangesJob.debug.fork
_ <- Modules.presentProofExchangeJob.debug.fork
_ <- Modules.connectDidCommExchangesJob.debug.fork
_ <- Modules.didCommServiceEndpoint(didCommServicePort).debug.fork
server <- serverProgram(didCommServicePort)
_ <- Modules.syncDIDPublicationStateFromDltJob.fork
_ <- Modules.app(restServicePort).fork
_ <- Modules.zioApp.fork
_ <- server.join *> ZIO.log(s"Server End")
_ <- ZIO.never
} yield ()

Expand Down Expand Up @@ -129,7 +113,7 @@ object AgentApp extends ZIOAppDefault {

didCommServiceUrl <- System.env("DIDCOMM_SERVICE_URL").map {
case Some(s) => s
case _ => "http://localhost"
case _ => "http://localhost:8090"
}
_ <- ZIO.logInfo(s"DIDComm Service URL => $didCommServiceUrl")

Expand Down Expand Up @@ -171,4 +155,4 @@ object AgentApp extends ZIOAppDefault {

}

object MainApp extends ZIOApp.Proxy(SystemInfoApp <> DefaultJvmMetrics.app <> AgentApp)
object MainApp extends ZIOApp.Proxy(DefaultJvmMetrics.app <> AgentApp)
Expand Up @@ -56,9 +56,9 @@ import io.iohk.atala.resolvers.{DIDResolver, UniversalDidResolver}
import org.didcommx.didcomm.DIDComm
import org.didcommx.didcomm.model.UnpackParams
import org.didcommx.didcomm.secret.{Secret, SecretResolver}
import zhttp.http.*
import zhttp.service.Server
import zio.*
import zio.http.*
import zio.http.model.*
import zio.config.typesafe.TypesafeConfigSource
import zio.config.{ReadError, read}
import zio.interop.catz.*
Expand Down Expand Up @@ -101,40 +101,36 @@ object Modules {
zioHttpServerApp.unit
}

def didCommServiceEndpoint(port: Int) = {
val header = "content-type" -> MediaTypes.contentTypeEncrypted
val app: HttpApp[
DidOps & DidAgent & CredentialService & PresentationService & ConnectionService & ManagedDIDService & HttpClient &
DidAgent & DIDResolver,
Throwable
] =
Http.collectZIO[Request] {
// // TODO add DIDComm messages parsing logic here!
// Response.text("Hello World!").setStatus(Status.Accepted)
// case Method.POST -> !! / "did-comm-v2" =>
case Method.GET -> !! / "did" =>
for {
didCommService <- ZIO.service[DidAgent]
str = didCommService.id.value
} yield (Response.text(str))

case req @ Method.POST -> !!
if req.headersAsList.exists(h => h._1.equalsIgnoreCase(header._1) && h._2.equalsIgnoreCase(header._2)) =>
val result = for {
data <- req.body.asString
_ <- webServerProgram(data)
} yield Response.ok

result
.tapError { error =>
ZIO.logErrorCause("Fail to POST form webServerProgram", Cause.fail(error))
}
.mapError {
case ex: DIDSecretStorageError => ex
case ex: MercuryThrowable => mercuryErrorAsThrowable(ex)
}
}
Server.start(port, app)
def didCommServiceEndpoint: HttpApp[
DidOps & DidAgent & CredentialService & PresentationService & ConnectionService & ManagedDIDService & HttpClient &
DidAgent & DIDResolver,
Throwable
] = Http.collectZIO[Request] {
case Method.GET -> !! / "did" =>
for {
didCommService <- ZIO.service[DidAgent]
str = didCommService.id.value
} yield (Response.text(str))

case req @ Method.POST -> !!
if req.headersAsList
.exists(h =>
h.key.toString.equalsIgnoreCase("content-type") &&
h.value.toString.equalsIgnoreCase(MediaTypes.contentTypeEncrypted)
) =>
val result = for {
data <- req.body.asString
_ <- webServerProgram(data)
} yield Response.ok

result
.tapError { error =>
ZIO.logErrorCause("Fail to POST form webServerProgram", Cause.fail(error))
}
.mapError {
case ex: DIDSecretStorageError => ex
case ex: MercuryThrowable => mercuryErrorAsThrowable(ex)
}
}

val issueCredentialDidCommExchangesJob: RIO[
Expand Down
@@ -0,0 +1,28 @@
package io.iohk.atala.agent.server

import zio.*
import zio.http.*
import zio.http.model.*
import zio.metrics.jvm.DefaultJvmMetrics
import io.iohk.atala.agent.server.buildinfo.BuildInfo
import io.iohk.atala.agent.server.health.HealthInfo
import zio.metrics.connectors.MetricsConfig
import zio.metrics.connectors.prometheus

import io.circe.generic.auto.*
import io.circe.syntax.*

object SystemInfoApp {
val metricsConfig = ZLayer.succeed(MetricsConfig(5.seconds))

lazy val prometheusLayer = (metricsConfig ++ prometheus.publisherLayer) >>> prometheus.prometheusLayer

def app = Http
.collectZIO[Request] {
case Method.GET -> !! / "metrics" =>
ZIO.serviceWithZIO[prometheus.PrometheusPublisher](_.get.map(Response.text))
case Method.GET -> !! / "health" =>
ZIO.succeed(Response.json(HealthInfo(version = BuildInfo.version).asJson.toString))
}
.provideLayer(SystemInfoApp.metricsConfig ++ prometheus.publisherLayer ++ prometheusLayer)
}

0 comments on commit be7fe96

Please sign in to comment.