Skip to content

Commit

Permalink
myndla-api: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Nov 30, 2023
1 parent 8667aac commit 63b6813
Show file tree
Hide file tree
Showing 35 changed files with 334 additions and 41 deletions.
5 changes: 2 additions & 3 deletions article-api/src/main/scala/no/ndla/articleapi/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

package no.ndla.articleapi

import cats.effect.{ExitCode, IO, IOApp}
import no.ndla.common.Environment.setPropsFromEnv

object Main extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
object Main {
def main(args: Array[String]): Unit = {
setPropsFromEnv()
val props = new ArticleApiProperties
val mainClass = new MainClass(props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ class MainClass(override val props: ArticleApiProperties) extends NdlaTapirMain[
logger.info(s"Done db migration, took ${System.currentTimeMillis() - startDBMillis}ms")
}

override def startServer(name: String, port: Int)(warmupFunc: => Unit): IO[Unit] =
override def startServer(name: String, port: Int)(warmupFunc: => Unit): Unit =
componentRegistry.Routes.startJdkServer(name, port)(warmupFunc)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package no.ndla.articleapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import no.ndla.articleapi.model.{api, domain}
import no.ndla.articleapi.model.search.SearchResult
Expand Down Expand Up @@ -49,7 +50,7 @@ class ArticleControllerV2Test extends UnitSuite with TestEnvironment {
override val services = List(controller)

override def beforeAll(): Unit = {
Routes.startJdkServer(this.getClass.getName, serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer(this.getClass.getName, serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package no.ndla.articleapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import no.ndla.articleapi.{Eff, TestEnvironment, UnitSuite}
import no.ndla.network.tapir.Service
Expand All @@ -21,7 +22,7 @@ class HealthControllerTest extends UnitSuite with TestEnvironment {
override val services: List[Service[Eff]] = List(healthController)

override def beforeAll(): Unit = {
Routes.startJdkServer(this.getClass.getName, serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer(this.getClass.getName, serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package no.ndla.articleapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import enumeratum.Json4s
import no.ndla.articleapi.{TestEnvironment, UnitSuite}
Expand Down Expand Up @@ -39,7 +40,7 @@ class InternControllerTest extends UnitSuite with TestEnvironment {
override val services = List(controller)

override def beforeAll(): Unit = {
Routes.startJdkServer(this.getClass.getName, serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer(this.getClass.getName, serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
4 changes: 2 additions & 2 deletions audio-api/src/main/scala/no/ndla/audioapi/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ package no.ndla.audioapi
import cats.effect.{ExitCode, IO, IOApp}
import no.ndla.common.Environment.setPropsFromEnv

object Main extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
object Main {
def main(args: Array[String]): Unit = {
setPropsFromEnv()
val props = new AudioApiProperties
val mainClass = new MainClass(props)
Expand Down
2 changes: 1 addition & 1 deletion audio-api/src/main/scala/no/ndla/audioapi/MainClass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class MainClass(val props: AudioApiProperties) extends NdlaTapirMain[Eff] {
logger.info(s"Done DB Migration took ${System.currentTimeMillis() - dBstartMillis} ms")
}

override def startServer(name: String, port: Int)(warmupFunc: => Unit): IO[Unit] =
override def startServer(name: String, port: Int)(warmupFunc: => Unit): Unit =
componentRegistry.Routes.startJdkServer(name, port)(warmupFunc)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package no.ndla.audioapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import io.circe.parser
import no.ndla.audioapi.model.api._
Expand Down Expand Up @@ -36,7 +37,7 @@ class AudioControllerTest extends UnitSuite with TestEnvironment with Retries {
override val services: List[Service[Eff]] = List(controller)

override def beforeAll(): Unit = {
Routes.startJdkServer("AudioControllerTest", serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer("AudioControllerTest", serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package no.ndla.audioapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import no.ndla.audioapi.model.domain
import no.ndla.audioapi.model.domain._
Expand All @@ -33,7 +34,7 @@ class HealthControllerTest extends UnitSuite with TestEnvironment {

override val services: List[Service[Eff]] = List(controller)
override def beforeAll(): Unit = {
Routes.startJdkServer("HealthControllerTest", serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer("HealthControllerTest", serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package no.ndla.audioapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import no.ndla.audioapi.TestData._
import no.ndla.audioapi.model.domain
Expand All @@ -27,7 +28,7 @@ class InternControllerTest extends UnitSuite with TestEnvironment {
override val services: List[InternController] = List(controller)

override def beforeAll(): Unit = {
Routes.startJdkServer("InternControllerTest", serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer("InternControllerTest", serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
12 changes: 12 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ lazy val `search-api` = Module.setup(
)
)

lazy val `myndla-api` = Module.setup(
project in file("./myndla-api/"),
myndlaapi,
deps = Seq(
network,
mapping,
language,
common,
testWith(scalatestsuite)
)
)

// Libraries
lazy val common = Module.setup(project in file("./common/"), commonlib, deps = Seq(testWith(scalatestsuite), language))
lazy val scalatestsuite = Module.setup(project in file("./scalatestsuite/"), scalatestsuitelib)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,8 @@ class WriteServiceTest extends UnitSuite with TestEnvironment {

when(taxonomyApiClient.getNode(nodeId)).thenReturn(Success(node))
when(taxonomyApiClient.getChildNodes(nodeId)).thenReturn(Success(List(child)))
when(taxonomyApiClient.getChildResources(anyString())).thenReturn(Success(List(resource)), Success(List(resource)), Success(List.empty))
when(taxonomyApiClient.getChildResources(anyString()))
.thenReturn(Success(List(resource)), Success(List(resource)), Success(List.empty))
when(draftRepository.withId(eqTo(1))(any)).thenReturn(Some(article1))
when(draftRepository.withId(eqTo(2))(any)).thenReturn(Some(article2))
when(draftRepository.withId(eqTo(3))(any)).thenReturn(Some(article3))
Expand Down Expand Up @@ -1404,7 +1405,8 @@ class WriteServiceTest extends UnitSuite with TestEnvironment {

when(taxonomyApiClient.getNode(nodeId)).thenReturn(Success(node))
when(taxonomyApiClient.getChildNodes(nodeId)).thenReturn(Success(List(child)))
when(taxonomyApiClient.getChildResources(anyString())).thenReturn(Success(List(resource)), Success(List(resource)), Success(List.empty))
when(taxonomyApiClient.getChildResources(anyString()))
.thenReturn(Success(List(resource)), Success(List(resource)), Success(List.empty))
when(draftRepository.withId(eqTo(1))(any)).thenReturn(Some(article1))
when(draftRepository.withId(eqTo(2))(any)).thenReturn(Some(article2))
service.copyRevisionDates(nodeId) should be(Success(()))
Expand Down
4 changes: 2 additions & 2 deletions frontpage-api/src/main/scala/no/ndla/frontpageapi/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ package no.ndla.frontpageapi
import cats.effect.{ExitCode, IO, IOApp}
import no.ndla.common.Environment.setPropsFromEnv

object Main extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
object Main {
def main(args: Array[String]): Unit = {
setPropsFromEnv()
val props = new FrontpageApiProperties
val mainClass = new MainClass(props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ class MainClass(override val props: FrontpageApiProperties) extends NdlaTapirMai
componentRegistry.healthController.setWarmedUp()
}

override def startServer(name: String, port: Int)(warmupFunc: => Unit): IO[Unit] =
override def startServer(name: String, port: Int)(warmupFunc: => Unit): Unit =
componentRegistry.Routes.startJdkServer(name, port)(warmupFunc)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package no.ndla.frontpageapi

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import sttp.client3.quick._

Expand All @@ -18,7 +19,7 @@ class FilmPageControllerTest extends UnitSuite with TestEnvironment {
override val services = List(filmPageController)

override def beforeAll(): Unit = {
Routes.startJdkServer(this.getClass.getName, serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer(this.getClass.getName, serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package no.ndla.frontpageapi

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import io.circe.syntax.EncoderOps
import no.ndla.common.model.NDLADate
Expand All @@ -20,7 +21,7 @@ class SubjectPageControllerTest extends UnitSuite with TestEnvironment {
override val services = List(subjectPageController)

override def beforeAll(): Unit = {
Routes.startJdkServer(this.getClass.getName, serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer(this.getClass.getName, serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package no.ndla.frontpageapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import no.ndla.common.{errors => common}
import no.ndla.frontpageapi.model.api
Expand All @@ -24,7 +25,7 @@ class FrontPageControllerTest extends UnitSuite with TestEnvironment {
override val services = List(controller)

override def beforeAll(): Unit = {
Routes.startJdkServer("FrontPageControllerTest", serverPort) {}.unsafeRunAndForget()
IO { Routes.startJdkServer("FrontPageControllerTest", serverPort) {} }.unsafeRunAndForget()
Thread.sleep(1000)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package no.ndla.integrationtests.draftapi.articleapi

import cats.effect.unsafe
import cats.effect.{IO, unsafe}
import no.ndla.articleapi.ArticleApiProperties
import no.ndla.common.model.domain.Priority
import no.ndla.common.model.domain.draft.Draft
Expand Down Expand Up @@ -61,7 +61,7 @@ class ArticleApiClientTest

override def beforeAll(): Unit = {
articleApi = new articleapi.MainClass(articleApiProperties)
cancelFunc = articleApi.run().unsafeRunCancelable()(unsafe.IORuntime.global)
cancelFunc = IO { articleApi.run() }.unsafeRunCancelable()(unsafe.IORuntime.global)
blockUntil(() => {
import sttp.client3.quick._
val req = quickRequest.get(uri"$articleApiBaseUrl/health")
Expand Down Expand Up @@ -126,7 +126,7 @@ class ArticleApiClientTest
slug = None,
comments = Seq.empty,
priority = Priority.Unspecified,
started = false,
started = false
)

val exampleToken =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package no.ndla.integrationtests.searchapi.articleapi

import cats.effect.unsafe
import cats.effect.{IO, unsafe}
import enumeratum.Json4s
import no.ndla.articleapi.ArticleApiProperties
import no.ndla.common.model.NDLADate
Expand Down Expand Up @@ -71,7 +71,7 @@ class ArticleApiClientTest

override def beforeAll(): Unit = {
articleApi = new articleapi.MainClass(articleApiProperties)
cancelFunc = articleApi.run().unsafeRunCancelable()(unsafe.IORuntime.global)
cancelFunc = IO { articleApi.run() }.unsafeRunCancelable()(unsafe.IORuntime.global)

blockUntil(() => {
import sttp.client3.quick._
Expand Down
15 changes: 15 additions & 0 deletions myndla-api/src/main/resources/log-license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
NDLA MyNDLA-API.
Copyright (C) 2023 NDLA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
21 changes: 21 additions & 0 deletions myndla-api/src/main/resources/log4j2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Configuration:
status: warn

Appenders:
Console:
name: Console
target: SYSTEM_OUT
PatternLayout:
Pattern: "[myndla-api] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] (%X{correlationID}) %-5level %C.%M#%L: %msg%n"

Loggers:
Logger:
name: "no.ndla"
level: info
additivity: false
AppenderRef:
- ref: Console
Root:
level: warn
AppenderRef:
- ref: Console
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Part of NDLA myndla-api
* Copyright (C) 2023 NDLA
*
* See LICENSE
*/

package no.ndla.myndlaapi

import no.ndla.common.Clock
import no.ndla.common.configuration.BaseComponentRegistry
import no.ndla.myndlaapi.controller.{ErrorHelpers, SwaggerDocControllerConfig}
import no.ndla.network.tapir.{
NdlaMiddleware,
Routes,
Service,
SwaggerControllerConfig,
TapirErrorHelpers,
TapirHealthController
}

class ComponentRegistry(properties: MyNdlaApiProperties)
extends BaseComponentRegistry[MyNdlaApiProperties]
with Props
with ErrorHelpers
with Routes[Eff]
with TapirErrorHelpers
with NdlaMiddleware
with Clock
with TapirHealthController
with SwaggerControllerConfig
with SwaggerDocControllerConfig {
override val props: MyNdlaApiProperties = properties

lazy val healthController = new TapirHealthController[Eff]
lazy val clock: SystemClock = new SystemClock

private val swagger = new SwaggerController[Eff](
List(
healthController
),
SwaggerDocControllerConfig.swaggerInfo
)
override val services: List[Service[Eff]] = swagger.getServices()
}
Loading

0 comments on commit 63b6813

Please sign in to comment.