Skip to content

Commit

Permalink
Merge pull request #399 from NDLANO/tapir-search-api
Browse files Browse the repository at this point in the history
search-api: Rewrite to tapir
  • Loading branch information
jnatten authored Feb 23, 2024
2 parents 7160507 + 6dba7fd commit 4cf4785
Show file tree
Hide file tree
Showing 46 changed files with 1,542 additions and 1,176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
*/

package no.ndla.common.model.domain
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.common.model.NDLADate

case class EditorNote(note: String, user: String, status: Status, timestamp: NDLADate)

object EditorNote {
implicit val encoder: Encoder[EditorNote] = deriveEncoder
implicit val decoder: Decoder[EditorNote] = deriveDecoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

package no.ndla.common.model.domain

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.common.model.domain.draft.DraftStatus

case class Status(current: DraftStatus, other: Set[DraftStatus])

object Status {
implicit val encoder: Encoder[Status] = deriveEncoder
implicit val decoder: Decoder[Status] = deriveDecoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package no.ndla.common.model.domain.draft

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.common.model.NDLADate

import java.util.UUID
Expand All @@ -19,3 +21,8 @@ case class Comment(
isOpen: Boolean,
solved: Boolean
)

object Comment {
implicit val encoder: Encoder[Comment] = deriveEncoder
implicit val decoder: Decoder[Comment] = deriveDecoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
package no.ndla.common.model.domain.draft

import enumeratum.Json4s
import no.ndla.common.model.NDLADate
import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import no.ndla.common.implicits._
import no.ndla.common.model.{NDLADate, RelatedContentLink}
import no.ndla.common.model.domain._
import no.ndla.language.Language.getSupportedLanguages
import org.json4s.ext.{EnumNameSerializer, JavaTimeSerializers, JavaTypesSerializers}
Expand Down Expand Up @@ -52,6 +55,11 @@ case class Draft(
}

object Draft {
implicit def eitherEnc: Encoder[Either[RelatedContentLink, Long]] = eitherEncoder[RelatedContentLink, Long]
implicit def eitherDec: Decoder[Either[RelatedContentLink, Long]] = eitherDecoder[RelatedContentLink, Long]
implicit val encoder: Encoder[Draft] = deriveEncoder
implicit val decoder: Decoder[Draft] = deriveDecoder

val serializers = Seq(
new EnumNameSerializer(Availability),
Json4s.serializer(DraftStatus),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package no.ndla.common.model.domain.draft

import enumeratum._
import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import no.ndla.common.model.NDLADate

import java.util.UUID
Expand All @@ -22,6 +24,9 @@ case class RevisionMeta(
}

object RevisionMeta {
implicit val encoder: Encoder[RevisionMeta] = deriveEncoder
implicit val decoder: Decoder[RevisionMeta] = deriveDecoder

def default: Seq[RevisionMeta] = Seq(
RevisionMeta(
UUID.randomUUID(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package no.ndla.common.model.domain.learningpath

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.common.errors.{ValidationException, ValidationMessage}
import no.ndla.language.model.LanguageField

Expand All @@ -16,6 +18,11 @@ case class EmbedUrl(url: String, language: String, embedType: EmbedType.Value) e
override def isEmpty: Boolean = url.isEmpty
}

object EmbedUrl {
implicit val encoder: Encoder[EmbedUrl] = deriveEncoder
implicit val decoder: Decoder[EmbedUrl] = deriveDecoder
}

object EmbedType extends Enumeration {

val OEmbed = Value("oembed")
Expand All @@ -39,4 +46,7 @@ object EmbedType extends Enumeration {
def valueOfOrDefault(s: String): EmbedType.Value = {
valueOf(s).getOrElse(EmbedType.OEmbed)
}

implicit val encoder: Encoder[EmbedType.Value] = Encoder.encodeEnumeration(EmbedType)
implicit val decoder: Decoder[EmbedType.Value] = Decoder.decodeEnumeration(EmbedType)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

package no.ndla.common.model.domain.learningpath

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.common.model.domain.Author

case class LearningpathCopyright(license: String, contributors: Seq[Author])

object LearningpathCopyright {
implicit val encoder: Encoder[LearningpathCopyright] = deriveEncoder
implicit val decoder: Decoder[LearningpathCopyright] = deriveDecoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ trait PublishedConceptController {
val language = searchParams.language.getOrElse(Language.AllLanguages)
val pageSize = searchParams.pageSize.getOrElse(DefaultPageSize)
val page = searchParams.page.getOrElse(1)
val idList = searchParams.idList
val idList = searchParams.ids
val fallback = searchParams.fallback.getOrElse(false)
val subjects = searchParams.subjects
val tagsToFilterBy = searchParams.tags
val exactTitleMatch = searchParams.exactTitleMatch.getOrElse(false)
val exactTitleMatch = searchParams.exactMatch.getOrElse(false)
val shouldScroll = searchParams.scrollId.exists(InitialScrollContextKeywords.contains)
val embedResource = searchParams.embedResource
val embedId = searchParams.embedId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ case class ConceptSearchParams(
@description("The ISO 639-1 language code describing language used in query-params.") language: Option[String],
@description("The page number of the search hits to display.") page: Option[Int],
@description("The number of search hits to display for each page.") pageSize: Option[Int],
@description("Return only articles that have one of the provided ids.") idList: List[Long],
@description("Return only articles that have one of the provided ids.") ids: List[Long],
@description("The sorting used on results. Default is by -relevance.") sort: Option[String],
@description("Whether to fallback to existing language if not found in selected language.") fallback: Option[Boolean],
@description("A search context retrieved from the response header of a previous search.") scrollId: Option[String],
@description("A comma-separated list of subjects that should appear in the search.") subjects: Set[String],
@description("A comma-separated list of tags to filter the search by.") tags: Set[String],
@description("If provided, only return concept where query matches title exactly.") exactTitleMatch: Option[Boolean],
@description("If provided, only return concept where query matches title exactly.") exactMatch: Option[Boolean],
@description("Embed resource type that should exist in the concepts.") embedResource: Option[String],
@description("Embed id attribute that should exist in the concepts.") embedId: Option[String],
@description("The type of concepts to return.") conceptType: Option[String],
Expand Down
10 changes: 7 additions & 3 deletions project/searchapi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ object searchapi extends Module {
),
melody,
elastic4s,
scalatra,
tapirHttp4sCirce,
vulnerabilityOverrides
)

lazy val tsSettings: Seq[Def.Setting[_]] = typescriptSettings(
imports = Seq("no.ndla.searchapi.model.api._"),
imports = Seq(
"no.ndla.searchapi.model.api._",
"no.ndla.searchapi.controller.parameters._"
),
exports = Seq(
"ApiTaxonomyContext",
"ArticleResult",
Expand All @@ -46,7 +49,8 @@ object searchapi extends Module {
"ImageResults",
"LearningpathResults",
"SearchError",
"ValidationError"
"ValidationError",
"DraftSearchParams"
)
)

Expand Down
47 changes: 33 additions & 14 deletions search-api/src/main/scala/no/ndla/searchapi/ComponentRegistry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
package no.ndla.searchapi

import com.typesafe.scalalogging.StrictLogging
import no.ndla.common.Clock
import no.ndla.common.configuration.BaseComponentRegistry
import no.ndla.network.NdlaClient
import no.ndla.network.clients.{FeideApiClient, RedisClient}
import no.ndla.network.scalatra.{NdlaControllerBase, NdlaSwaggerSupport}
import no.ndla.network.tapir.{
NdlaMiddleware,
Routes,
Service,
SwaggerControllerConfig,
TapirErrorHelpers,
TapirHealthController
}
import no.ndla.search.{BaseIndexService, Elastic4sClient}
import no.ndla.searchapi.controller.{HealthController, InternController, NdlaController, SearchController}
import no.ndla.searchapi.controller.{InternController, SearchController, SwaggerDocControllerConfig}
import no.ndla.searchapi.integration._
import no.ndla.searchapi.model.api.ErrorHelpers
import no.ndla.searchapi.service.search._
Expand All @@ -28,12 +36,12 @@ class ComponentRegistry(properties: SearchApiProperties)
with DraftIndexService
with MultiSearchService
with ErrorHelpers
with Clock
with MultiDraftSearchService
with AudioApiClient
with ConverterService
with DraftApiClient
with Elastic4sClient
with HealthController
with TaxonomyApiClient
with ImageApiClient
with IndexService
Expand All @@ -46,26 +54,21 @@ class ComponentRegistry(properties: SearchApiProperties)
with SearchService
with ApiSearchService
with SearchController
with NdlaSwaggerSupport
with FeideApiClient
with RedisClient
with InternController
with SearchApiClient
with GrepApiClient
with Props
with NdlaController
with NdlaControllerBase
with SearchApiInfo {
with Routes[Eff]
with NdlaMiddleware
with TapirErrorHelpers
with SwaggerControllerConfig
with SwaggerDocControllerConfig
with TapirHealthController {
override val props: SearchApiProperties = properties
import props._

implicit val swagger: SearchSwagger = new SearchSwagger

lazy val searchController = new SearchController
lazy val healthController = new HealthController
lazy val internController = new InternController
lazy val resourcesApp = new ResourcesApp

lazy val ndlaClient = new NdlaClient
var e4sClient: NdlaE4sClient = Elastic4sClientFactory.getClient(SearchServer)

Expand Down Expand Up @@ -94,4 +97,20 @@ class ComponentRegistry(properties: SearchApiProperties)
lazy val learningPathIndexService = new LearningPathIndexService
lazy val draftIndexService = new DraftIndexService
lazy val multiDraftSearchService = new MultiDraftSearchService

lazy val searchController = new SearchController
lazy val healthController = new TapirHealthController[Eff]
lazy val internController = new InternController
lazy val clock: SystemClock = new SystemClock

private val swagger = new SwaggerController(
List[Service[Eff]](
searchController,
internController,
healthController
),
SwaggerDocControllerConfig.swaggerInfo
)

override val services: List[Service[Eff]] = swagger.getServices()
}
2 changes: 1 addition & 1 deletion search-api/src/main/scala/no/ndla/searchapi/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ object Main {
setPropsFromEnv()
val props = new SearchApiProperties
val mainClass = new MainClass(props)
mainClass.start()
mainClass.run()
}
}
30 changes: 13 additions & 17 deletions search-api/src/main/scala/no/ndla/searchapi/MainClass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,29 @@

package no.ndla.searchapi

import com.typesafe.scalalogging.StrictLogging
import no.ndla.common.Environment.booleanPropOrFalse
import no.ndla.network.scalatra.NdlaScalatraServer
import no.ndla.common.Warmup
import no.ndla.network.tapir.NdlaTapirMain
import no.ndla.searchapi.service.StandaloneIndexing
import org.eclipse.jetty.server.Server

class MainClass(props: SearchApiProperties) extends StrictLogging {
class MainClass(override val props: SearchApiProperties) extends NdlaTapirMain[Eff] {
val componentRegistry = new ComponentRegistry(props)

def startServer(): Server = {
new NdlaScalatraServer[SearchApiProperties, ComponentRegistry](
"no.ndla.searchapi.ScalatraBootstrap",
componentRegistry,
{},
warmupRequest => {
warmupRequest("/search-api/v1/search", Map("query" -> "norge"))
warmupRequest("/health", Map.empty)
}
)
private def warmupRequest = (path: String, options: Map[String, String]) =>
Warmup.warmupRequest(props.ApplicationPort, path, options)

override def warmup(): Unit = {
warmupRequest("/search-api/v1/search", Map("query" -> "norge"))
warmupRequest("/health", Map.empty)
}

def start(): Unit = {
override def beforeStart(): Unit = {}

override def startServer(name: String, port: Int)(warmupFunc: => Unit): Unit = {
if (booleanPropOrFalse("STANDALONE_INDEXING_ENABLED")) {
new StandaloneIndexing(props, componentRegistry).doStandaloneIndexing()
} else {
val server = startServer()
server.join()
componentRegistry.Routes.startJdkServer(name, port)(warmupFunc)
}
}
}

This file was deleted.

Loading

0 comments on commit 4cf4785

Please sign in to comment.