Skip to content

Commit

Permalink
Tapir updated (ergoplatform#161)
Browse files Browse the repository at this point in the history
* Migration to Tapir 0.18.x.
  • Loading branch information
oskin1 authored Nov 11, 2021
1 parent 2d87390 commit 47b3cbb
Show file tree
Hide file tree
Showing 45 changed files with 163 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.metals
.bloop
target
data
/modules/*/target/
/target/
/project/target/
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lazy val commonSettings = Seq(
scalacOptions ++= commonScalacOptions,
scalaVersion := "2.12.15",
organization := "org.ergoplatform",
version := "9.6.0",
version := "9.6.3",
resolvers += Resolver.sonatypeRepo("public"),
resolvers += Resolver.sonatypeRepo("snapshots"),
libraryDependencies ++= dependencies.Testing ++ dependencies.CompilerPlugins,
Expand Down
20 changes: 20 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '3.4'

services:
postgres:
image: postgres:11-alpine
shm_size: 4g
environment:
POSTGRES_PASSWORD: foo
ports:
- "5432:5432"
volumes:
- ./data/postgres:/var/lib/postgresql/data:rw
redis:
image: redis:latest
restart: always
command: ["redis-server"]
ports:
- "127.0.0.1:6379:6379"
volumes:
- ./data/redis:/usr/local/etc/redis
2 changes: 1 addition & 1 deletion modules/explorer-api/src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ utx-cache.transaction-ttl = 48h

requests.max-entities-per-request = 500
requests.max-entities-per-heavy-request = 100
requests.max-epochs-per-request = 1536
requests.max-blocks-per-request = 1536
service.chunk-size = 100

protocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import cats.effect.{ConcurrentEffect, ContextShift, Resource, Timer}
import cats.syntax.semigroupk._
import dev.profunktor.redis4cats.algebra.RedisCommands
import org.ergoplatform.ErgoAddressEncoder
import org.ergoplatform.explorer.CRaise
import org.ergoplatform.explorer.Err.{RefinementFailed, RequestProcessingErr}
import org.ergoplatform.explorer.db.Trans
import org.ergoplatform.explorer.db.algebra.LiftConnectionIO
import org.ergoplatform.explorer.http.api.streaming.CompileStream
import org.ergoplatform.explorer.http.api.v0.routes.RoutesV0Bundle
import org.ergoplatform.explorer.http.api.v1.routes.RoutesV1Bundle
import org.ergoplatform.explorer.settings.ApiSettings
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.middleware._
import org.http4s.server.{Router, Server}
import org.http4s.syntax.kleisli._
Expand All @@ -36,7 +34,7 @@ object HttpApi {
ec: ExecutionContext,
encoder: ErgoAddressEncoder,
opts: Http4sServerOptions[F, F]
): Resource[F, Server[F]] =
): Resource[F, Server] =
for {
v0 <- Resource.eval(RoutesV0Bundle(settings.protocol, settings.utxCache, redis)(trans))
v1 <- Resource.eval(RoutesV1Bundle(settings.service, settings.requests, settings.utxCache, redis)(trans))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object commonDirectives {
defaultFieldOpt: Option[String] = None
): EndpointInput[Sorting] =
(query[Option[String]]("sortBy").validate(
Validator.`enum`(none :: allowedFields.keys.toNonEmptyList.toList.map(_.some))
Validator.enumeration(none :: allowedFields.keys.toNonEmptyList.toList.map(_.some))
) and query[Option[Sorting.SortOrder]]("sortDirection"))
.map { input =>
val (fieldOpt, orderOpt) = input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ final case class Items[A](items: List[A], total: Int)

object Items {

implicit def schema[A: Schema]: Schema[Items[A]] = Schema.derived[Items[A]]
implicit def schema[A: Schema]: Schema[Items[A]] =
Schema.derived[Items[A]]
.modify(_.items)(_.description("Items in selection"))
.modify(_.total)(_.description("Total qty of items"))

implicit def validator[A: Schema]: Validator[Items[A]] = schema.validator
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ object BlockExtensionInfo {
implicit private def registersSchema: Schema[Json] =
Schema(
SchemaType.SOpenProduct(
SchemaType.SObjectInfo("fields"),
Schema(SchemaType.SString[Json]())
)(_ => Map.empty)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ object OutputInfo {
implicit private def registersSchema: Schema[Json] =
Schema(
SchemaType.SOpenProduct(
SchemaType.SObjectInfo("AdditionalRegisters"),
Schema(SchemaType.SString[Json]())
)(_ => Map.empty)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ object SpendingProofInfo {
implicit private def extensionSchema: Schema[Json] =
Schema(
SchemaType.SOpenProduct(
SchemaType.SObjectInfo("ProofExtension"),
Schema(SchemaType.SString[Json]())
)(_ => Map.empty)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ object UOutputInfo {
implicit private def registersSchema: Schema[Json] =
Schema(
SchemaType.SOpenProduct(
SchemaType.SObjectInfo("AdditionalRegisters"),
Schema(SchemaType.SString[Json]())
)(_ => Map.empty)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ final class AddressesRoutes[

import org.ergoplatform.explorer.http.api.v0.defs.AddressesEndpointDefs._

private def interpreter = Http4sServerInterpreter(opts)

val routes: HttpRoutes[F] =
getAddressR <+> getTxsByAddressR <+> getAssetHoldersR <+> getBalancesR

def getAddressR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getAddressDef) {
interpreter.toRoutes(getAddressDef) {
case (address, minConfirmations) =>
addressesService.getAddressInfo(address, minConfirmations).adaptThrowable.value
}

def getTxsByAddressR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getTxsByAddressDef) {
interpreter.toRoutes(getTxsByAddressDef) {
case (address, paging) =>
transactionsService
.getTxsInfoByAddress(address, paging)
Expand All @@ -43,7 +45,7 @@ final class AddressesRoutes[
}

def getAssetHoldersR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getAssetHoldersDef) {
interpreter.toRoutes(getAssetHoldersDef) {
case (tokenId, paging) =>
addressesService
.getAssetHoldersAddresses(tokenId, paging)
Expand All @@ -54,7 +56,7 @@ final class AddressesRoutes[
}

def getBalancesR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getBalancesDef) { paging =>
interpreter.toRoutes(getBalancesDef) { paging =>
addressesService.balances(paging).adaptThrowable.value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ final class AssetsRoutes[
val routes: HttpRoutes[F] =
getAllIssuingBoxesR <+> getIssuingBoxR

private def interpreter = Http4sServerInterpreter(opts)

private def getAllIssuingBoxesR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getAllIssuingBoxesDef) { paging =>
interpreter.toRoutes(getAllIssuingBoxesDef) { paging =>
service
.getAllIssuingBoxes(paging)
.adaptThrowable
.value
}

private def getIssuingBoxR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getIssuingBoxDef) { tokenId =>
interpreter.toRoutes(getIssuingBoxDef) { tokenId =>
service
.getIssuingBoxes(NonEmptyList.one(tokenId))
.compile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ final class BlocksRoutes[
val routes: HttpRoutes[F] =
getBlocksR <+> getBlockSummaryByIdR <+> getBlockIdsAtHeightR

private def interpreter = Http4sServerInterpreter(opts)

private def getBlocksR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getBlocksDef) {
interpreter.toRoutes(getBlocksDef) {
case (paging, sorting) =>
service
.getBlocks(paging, sorting)
Expand All @@ -33,7 +35,7 @@ final class BlocksRoutes[
}

private def getBlockSummaryByIdR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getBlockSummaryByIdDef) { id =>
interpreter.toRoutes(getBlockSummaryByIdDef) { id =>
service
.getBlockSummaryById(id)
.adaptThrowable
Expand All @@ -42,7 +44,7 @@ final class BlocksRoutes[
}

private def getBlockIdsAtHeightR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getBlockIdsAtHeightDef) { height =>
interpreter.toRoutes(getBlockIdsAtHeightDef) { height =>
service
.getBlockIdsAtHeight(height)
.adaptThrowable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ final class BoxesRoutes[
getOutputByIdR <+> getOutputsByErgoTreeR <+> getUnspentOutputsByErgoTreeR <+>
getOutputsByAddressR <+> getUnspentOutputsByAddressR

private def interpreter = Http4sServerInterpreter(opts)

private def getOutputByIdR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getOutputByIdDef) { id =>
interpreter.toRoutes(getOutputByIdDef) { id =>
service
.getOutputById(id)
.adaptThrowable
Expand All @@ -32,22 +34,22 @@ final class BoxesRoutes[
}

private def getOutputsByErgoTreeR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getOutputsByErgoTreeDef) { tree =>
interpreter.toRoutes(getOutputsByErgoTreeDef) { tree =>
service.getOutputsByErgoTree(tree).compile.toList.adaptThrowable.value
}

private def getUnspentOutputsByErgoTreeR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getUnspentOutputsByErgoTreeDef) { tree =>
interpreter.toRoutes(getUnspentOutputsByErgoTreeDef) { tree =>
service.getUnspentOutputsByErgoTree(tree).compile.toList.adaptThrowable.value
}

private def getOutputsByAddressR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getOutputsByAddressDef) { address =>
interpreter.toRoutes(getOutputsByAddressDef) { address =>
service.getOutputsByAddress(address).compile.toList.adaptThrowable.value
}

private def getUnspentOutputsByAddressR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getUnspentOutputsByAddressDef) { address =>
interpreter.toRoutes(getUnspentOutputsByAddressDef) { address =>
service.getUnspentOutputsByAddress(address).compile.toList.adaptThrowable.value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,50 @@ final class ChartsRoutes[
getAvgTxsNumPerBlockR <+> getTotalTxsNumR <+> getAvgDifficultyR <+>
getMinersRevenuR <+> getHashRateR <+> getHashRateDistributionR

private def interpreter = Http4sServerInterpreter(opts)

private def getTotalCoinsAmtR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getTotalCoinsAmtDef) { timespan =>
interpreter.toRoutes(getTotalCoinsAmtDef) { timespan =>
service.getTotalCoins(timespan).adaptThrowable.value
}

private def getAvgBlockSizeR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getAvgBlockSizeDef) { timespan =>
interpreter.toRoutes(getAvgBlockSizeDef) { timespan =>
service.getAvgBlockSize(timespan).adaptThrowable.value
}

private def getBlockChainSizeR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getBlockChainSizeDef) { timespan =>
interpreter.toRoutes(getBlockChainSizeDef) { timespan =>
service.getBlockChainSize(timespan).adaptThrowable.value
}

private def getAvgTxsNumPerBlockR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getAvgTxsNumPerBlockDef) { timespan =>
interpreter.toRoutes(getAvgTxsNumPerBlockDef) { timespan =>
service.getAvgTxsNumPerBlock(timespan).adaptThrowable.value
}

private def getTotalTxsNumR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getTotalTxsNumDef) { timespan =>
interpreter.toRoutes(getTotalTxsNumDef) { timespan =>
service.getTransactionsNum(timespan).adaptThrowable.value
}

private def getAvgDifficultyR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getAvgDifficultyDef) { timespan =>
interpreter.toRoutes(getAvgDifficultyDef) { timespan =>
service.getAvgDifficulty(timespan).adaptThrowable.value
}

private def getMinersRevenuR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getMinersRevenueDef) { timespan =>
interpreter.toRoutes(getMinersRevenueDef) { timespan =>
service.getMinersRevenue(timespan).adaptThrowable.value
}

private def getHashRateR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getHashRateDef) { timespan =>
interpreter.toRoutes(getHashRateDef) { timespan =>
service.getHashRate(timespan).adaptThrowable.value
}

private def getHashRateDistributionR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getHashRateDistributionDef) { _ =>
interpreter.toRoutes(getHashRateDistributionDef) { _ =>
service.getHashRateDistribution(24.hours).adaptThrowable.value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ final class DexRoutes[
val routes: HttpRoutes[F] =
getUnspentSellOrdersR <+> getUnspentBuyOrdersR

private def interpreter = Http4sServerInterpreter(opts)

private def getUnspentSellOrdersR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getUnspentSellOrdersDef) {
interpreter.toRoutes(getUnspentSellOrdersDef) {
case (tokenId, paging) =>
service
.getUnspentSellOrders(tokenId, paging)
Expand All @@ -33,7 +35,7 @@ final class DexRoutes[
}

private def getUnspentBuyOrdersR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getUnspentBuyOrdersDef) {
interpreter.toRoutes(getUnspentBuyOrdersDef) {
case (tokenId, paging) =>
service
.getUnspentBuyOrders(tokenId, paging)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class DocsRoutes[F[_]: Concurrent: ContextShift: Timer](implicit opts: Htt

val routes: HttpRoutes[F] = openApiSpecR <+> redocApiSpecR

private def interpreter = Http4sServerInterpreter(opts)

private def allEndpoints =
AddressesEndpointDefs.endpoints ++
AssetsEndpointDefs.endpoints ++
Expand All @@ -46,13 +48,13 @@ final class DocsRoutes[F[_]: Concurrent: ContextShift: Timer](implicit opts: Htt
Nil

private val docsAsYaml =
OpenAPIDocsInterpreter
OpenAPIDocsInterpreter()
.toOpenAPI(allEndpoints, "Ergo Explorer API v0", "1.0")
.tags(tags)
.toYaml

private def openApiSpecR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(apiSpecDef) { _ =>
interpreter.toRoutes(apiSpecDef) { _ =>
docsAsYaml
.asRight[ApiErr]
.pure[F]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ final class InfoRoutes[

val routes: HttpRoutes[F] = getCurrentStatsR <+> getCurrentSupplyR

private def interpreter = Http4sServerInterpreter(opts)

private def getCurrentStatsR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getBlockChainInfoDef)(_ => service.getBlockChainInfo.adaptThrowable.value)
interpreter.toRoutes(getBlockChainInfoDef)(_ => service.getBlockChainInfo.adaptThrowable.value)

private def getCurrentSupplyR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(getCurrentSupplyDef) { _ =>
interpreter.toRoutes(getCurrentSupplyDef) { _ =>
service.getBlockChainInfo
.map { info =>
BigDecimal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ final class SearchRoutes[

val routes: HttpRoutes[F] = searchR

private def interpreter = Http4sServerInterpreter(opts)

private def searchR: HttpRoutes[F] =
Http4sServerInterpreter.toRoutes(searchDef) { q =>
interpreter.toRoutes(searchDef) { q =>
search.search(q).adaptThrowable.value
}
}
Expand Down
Loading

0 comments on commit 47b3cbb

Please sign in to comment.