Skip to content

Commit

Permalink
super wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Aug 29, 2023
1 parent 9a244b3 commit 94b8dc2
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ class ComponentRegistry(properties: AudioApiProperties)

lazy val clock = new SystemClock

private val services: List[Service] = List(
audioApiController,
seriesController,
private val services: List[SwaggerService] = List(
// audioApiController,
// seriesController,
internController,
healthController
)

private val swaggerDocController = new SwaggerController(services, SwaggerDocControllerConfig.swaggerInfo)

def routes: Kleisli[IO, Request[IO], Response[IO]] = Routes.build(services :+ swaggerDocController)
// private val swaggerDocController = new SwaggerController(services, SwaggerDocControllerConfig.swaggerInfo)

val allServices = services
}
2 changes: 1 addition & 1 deletion audio-api/src/main/scala/no/ndla/audioapi/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ object Main extends IOApp {
setPropsFromEnv()
val props = new AudioApiProperties
val mainClass = new MainClass(props)
mainClass.run(args)
IO.never.as(ExitCode.Success)
}
}
34 changes: 25 additions & 9 deletions audio-api/src/main/scala/no/ndla/audioapi/MainClass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

package no.ndla.audioapi

import cats.data.Kleisli
import cats.effect.IO
import no.ndla.common.Warmup
import no.ndla.network.tapir.NdlaTapirMain
import org.http4s.{Request, Response}
import org.log4s.getLogger
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.jdkhttp.{Id, JdkHttpServer, JdkHttpServerOptions}

class MainClass(override val props: AudioApiProperties) extends NdlaTapirMain {
private val componentRegistry = new ComponentRegistry(props)
override val app: Kleisli[IO, Request[IO], Response[IO]] = componentRegistry.routes
class MainClass(val props: AudioApiProperties) {
val logger = getLogger
private val componentRegistry = new ComponentRegistry(props)
// override val app: Kleisli[IO, Request[IO], Response[IO]] = componentRegistry.routes

private def warmupRequest = (path, params) => Warmup.warmupRequest(props.ApplicationPort, path, params)
override def warmup(): Unit = {
def warmup(): Unit = {
warmupRequest("/audio-api/v1/audio", Map("query" -> "norge", "fallback" -> "true"))
warmupRequest("/audio-api/v1/audio/1", Map("language" -> "nb"))
warmupRequest("/audio-api/v1/series", Map("language" -> "nb"))
Expand All @@ -29,10 +29,26 @@ class MainClass(override val props: AudioApiProperties) extends NdlaTapirMain {
componentRegistry.healthController.setWarmedUp()
}

override def beforeStart(): Unit = {
def beforeStart(): Unit = {
logger.info("Starting DB Migration")
val dBstartMillis = System.currentTimeMillis()
componentRegistry.migrator.migrate(): Unit
logger.info(s"Done DB Migration took ${System.currentTimeMillis() - dBstartMillis} ms")
}

val tapirEndpoints: List[ServerEndpoint[Any, Id]] = componentRegistry.allServices.flatMap(_.builtEndpoints)


val opts = JdkHttpServerOptions.Default
.appendInterceptor(fourohfour)

beforeStart()

JdkHttpServer()
.options(opts)
.addEndpoints(tapirEndpoints)
.port(props.ApplicationPort)
.start()

warmup()
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import sttp.tapir.generic.auto._
import sttp.tapir.model.CommaSeparated
import sttp.tapir.server.ServerEndpoint
import sttp.tapir._
import sttp.tapir.server.jdkhttp.Id

import java.io.File
import java.nio.file.Files
Expand Down Expand Up @@ -267,16 +268,16 @@ trait AudioController {
readService.getAllTags(query.underlyingOrElse(""), pageSize, pageNo, language).handleErrorsOrOk
}

override val endpoints: List[ServerEndpoint[Any, IO]] = List(
getSearch,
postSearch,
getIds,
getSingle,
deleteAudio,
deleteLanguage,
postNewAudio,
putUpdateAudio,
tagSearch
override val endpoints: List[ServerEndpoint[Any, Id]] = List(
// getSearch,
// postSearch,
// getIds,
// getSingle,
// deleteAudio,
// deleteLanguage,
// postNewAudio,
// putUpdateAudio,
// tagSearch
)

def getBytesAndDeleteFile(file: Part[File]): Part[Array[Byte]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ trait HealthController {

private def getReturnCode(imageResponse: Response[String]) = {
imageResponse.code.code match {
case 200 => Ok()
case _ => InternalServerError()
case 200 => Right("Healthy")
case _ => Left("Internal server error")
}
}

override def checkHealth(): IO[http4s.Response[IO]] = {
override def checkHealth(): Either[String, String] = {
audioRepository
.getRandomAudio()
.map(audio => {
val id = audio.id.get
val previewUrl = s"http://$localhost:$localport${props.AudioControllerPath}$id"
getReturnCode(getApiResponse(previewUrl))
})
.getOrElse(Ok())
.getOrElse(Right("Healthy"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package no.ndla.audioapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import cats.implicits._
import io.circe.generic.auto._
import no.ndla.audioapi.Props
Expand All @@ -25,6 +26,7 @@ import sttp.model.StatusCode
import sttp.tapir._
import sttp.tapir.generic.auto._
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.jdkhttp.Id

import scala.util.{Failure, Success}

Expand All @@ -46,99 +48,103 @@ trait InternController {
override val enableSwagger = false
private val internalErrorStringBody = statusCode(StatusCode.InternalServerError).and(stringBody)

override val endpoints: List[ServerEndpoint[Any, IO]] = List(
override val endpoints: List[ServerEndpoint[Any, Id]] = List(
endpoint.get
.in("external")
.in(path[String]("external_id"))
.in(query[Option[String]]("language"))
.out(jsonBody[Option[api.AudioMetaInformation]])
.serverLogicPure { case (externalId, language) =>
readService.withExternalId(externalId, language).asRight
},
endpoint.post
.in("index")
.in(query[Option[Int]]("numShards"))
.out(stringBody)
.errorOut(internalErrorStringBody)
.serverLogic { numShards =>
val result = IO(
(
audioIndexService.indexDocuments(numShards),
tagIndexService.indexDocuments(numShards),
seriesIndexService.indexDocuments(numShards)
)
)

result.flatMap {
case (Success(audioReindexResult), Success(tagReindexResult), Success(seriesReIndexResult)) =>
val result =
s"""Completed indexing of ${audioReindexResult.totalIndexed} documents in ${audioReindexResult.millisUsed} (audios) ms.
|Completed indexing of ${tagReindexResult.totalIndexed} documents in ${tagReindexResult.millisUsed} (tags) ms.
|Completed indexing of ${seriesReIndexResult.totalIndexed} documents in ${seriesReIndexResult.millisUsed} (series) ms.""".stripMargin
logger.info(result) >>
IO.pure(result.asRight)
case (Failure(f), _, _) =>
logger.warn(f.getMessage, f) >>
IO.pure(f.getMessage.asLeft)
case (_, Failure(f), _) =>
logger.warn(f.getMessage, f) >>
IO.pure(f.getMessage.asLeft)
case (_, _, Failure(f)) =>
logger.warn(f.getMessage, f) >>
IO.pure(f.getMessage.asLeft)
}
},
endpoint.delete
.in("index")
.errorOut(internalErrorStringBody)
.out(stringBody)
.serverLogic { _ =>
def pluralIndex(n: Int) = if (n == 1) "1 index" else s"$n indexes"
audioIndexService.findAllIndexes(props.SearchIndex) match {
case Failure(f) => IO.pure(f.getMessage.asLeft)
case Success(indexes) =>
val deletes = indexes.traverse(index => {
logger.info(s"Deleting index $index") >>
IO.pure(audioIndexService.deleteIndexWithName(Option(index)))
})
deletes.map { deleteResults =>
val (errors, successes) = deleteResults.partition(_.isFailure)
if (errors.nonEmpty) {
val message = s"Failed to delete ${pluralIndex(errors.length)}: " +
s"${errors.map(_.failed.get.getMessage).mkString(", ")}. " +
s"${pluralIndex(successes.length)} were deleted successfully."
message.asLeft
} else {
s"Deleted ${pluralIndex(successes.length)}".asRight
}
}
}
},
endpoint.get
.in("dump" / "audio")
.in(query[Int]("page").default(1))
.in(query[Int]("page-size").default(250))
.out(jsonBody[AudioMetaDomainDump])
.errorOut(errorOutputsFor(400, 500))
.serverLogicPure { case (pageNo, pageSize) =>
readService.getMetaAudioDomainDump(pageNo, pageSize).asRight
},
endpoint.get
.in("dump" / "audio")
.in(path[Long]("id"))
.errorOut(errorOutputsFor(400, 404))
.out(jsonBody[AudioMetaInformation])
.serverLogic { id =>
audioRepository.withId(id) match {
case Some(image) => IO(image.asRight)
case None => returnLeftError(new NotFoundException(s"Could not find audio with id: '$id'"))
}
},
endpoint.post
.in("dump" / "audio")
.in(jsonBody[AudioMetaInformation])
.out(jsonBody[AudioMetaInformation])
.serverLogicPure { domainMeta => audioRepository.insert(domainMeta).asRight }
}
// endpoint.post
// .in("index")
// .in(query[Option[Int]]("numShards"))
// .out(stringBody)
// .errorOut(internalErrorStringBody)
// .serverLogic { numShards =>
// val result = IO(
// (
// audioIndexService.indexDocuments(numShards),
// tagIndexService.indexDocuments(numShards),
// seriesIndexService.indexDocuments(numShards)
// )
// )
//
// result
// .flatMap {
// case (Success(audioReindexResult), Success(tagReindexResult), Success(seriesReIndexResult)) =>
// val result =
// s"""Completed indexing of ${audioReindexResult.totalIndexed} documents in ${audioReindexResult.millisUsed} (audios) ms.
// |Completed indexing of ${tagReindexResult.totalIndexed} documents in ${tagReindexResult.millisUsed} (tags) ms.
// |Completed indexing of ${seriesReIndexResult.totalIndexed} documents in ${seriesReIndexResult.millisUsed} (series) ms.""".stripMargin
// logger.info(result) >>
// IO.pure(result.asRight)
// case (Failure(f), _, _) =>
// logger.warn(f.getMessage, f) >>
// IO.pure(f.getMessage.asLeft)
// case (_, Failure(f), _) =>
// logger.warn(f.getMessage, f) >>
// IO.pure(f.getMessage.asLeft)
// case (_, _, Failure(f)) =>
// logger.warn(f.getMessage, f) >>
// IO.pure(f.getMessage.asLeft)
// }
// .unsafeRunSync()
// },
// endpoint.delete
// .in("index")
// .errorOut(internalErrorStringBody)
// .out(stringBody)
// .serverLogic { _ =>
// def pluralIndex(n: Int) = if (n == 1) "1 index" else s"$n indexes"
// val x = audioIndexService.findAllIndexes(props.SearchIndex) match {
// case Failure(f) => IO.pure(f.getMessage.asLeft)
// case Success(indexes) =>
// val deletes = indexes.traverse(index => {
// logger.info(s"Deleting index $index") >>
// IO.pure(audioIndexService.deleteIndexWithName(Option(index)))
// })
// deletes.map { deleteResults =>
// val (errors, successes) = deleteResults.partition(_.isFailure)
// if (errors.nonEmpty) {
// val message = s"Failed to delete ${pluralIndex(errors.length)}: " +
// s"${errors.map(_.failed.get.getMessage).mkString(", ")}. " +
// s"${pluralIndex(successes.length)} were deleted successfully."
// message.asLeft
// } else {
// s"Deleted ${pluralIndex(successes.length)}".asRight
// }
// }
// }
// // TODO: Ikke io eru grei
// x.unsafeRunSync()
// },
// endpoint.get
// .in("dump" / "audio")
// .in(query[Int]("page").default(1))
// .in(query[Int]("page-size").default(250))
// .out(jsonBody[AudioMetaDomainDump])
// .errorOut(errorOutputsFor(400, 500))
// .serverLogicPure { case (pageNo, pageSize) =>
// readService.getMetaAudioDomainDump(pageNo, pageSize).asRight
// },
// endpoint.get
// .in("dump" / "audio")
// .in(path[Long]("id"))
// .errorOut(errorOutputsFor(400, 404))
// .out(jsonBody[AudioMetaInformation])
// .serverLogic { id =>
// audioRepository.withId(id) match {
// case Some(image) => image.asRight
// case None => returnLeftPureError(new NotFoundException(s"Could not find audio with id: '$id'"))
// }
// },
// endpoint.post
// .in("dump" / "audio")
// .in(jsonBody[AudioMetaInformation])
// .out(jsonBody[AudioMetaInformation])
// .serverLogicPure { domainMeta => audioRepository.insert(domainMeta).asRight }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import sttp.tapir.EndpointIO.annotations.{header, jsonbody}
import sttp.tapir.generic.auto._
import sttp.tapir.server.ServerEndpoint
import sttp.tapir._
import sttp.tapir.server.jdkhttp.Id

import scala.util.{Failure, Success, Try}

Expand Down Expand Up @@ -261,14 +262,14 @@ trait SeriesController {
case _ => orFunction
}

override protected val endpoints: List[ServerEndpoint[Any, IO]] = List(
getSeriesSearch,
postSeriesSearch,
getSingleSeries,
deleteSeries,
deleteLanguage,
postNewSeries,
putUpdateSeries
override protected val endpoints: List[ServerEndpoint[Any, Id]] = List(
// getSeriesSearch,
// postSeriesSearch,
// getSingleSeries,
// deleteSeries,
// deleteLanguage,
// postNewSeries,
// putUpdateSeries
)

}
Expand Down
Loading

0 comments on commit 94b8dc2

Please sign in to comment.