Skip to content

Commit

Permalink
Feat/low resources mode (#61)
Browse files Browse the repository at this point in the history
* Add a flag to disable resources which are not supported in low level
machines

* Rename variable to make it clear
  • Loading branch information
juanitodread authored Oct 8, 2019
1 parent 6c6adf6 commit 8a33017
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ server {
port = ${?PORT}
}

app {
lowResourcesMode = false
lowResourcesMode = ${?LOW_RESOURCES}
}

api {
service = "pitaya"
context = "api"
Expand Down
10 changes: 10 additions & 0 deletions src/main/scala/org/juanitodread/pitayafinch/model/Errors.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.juanitodread.pitayafinch.model

sealed abstract class PitayaError(title: String, msg: String) extends Exception(msg) {
def getTitle: String = title
def message: String
}

case class ResourceNotAvailable(
title: String = "ResourceNotAvailable",
message: String) extends PitayaError(title, message)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.circe.{ Encoder, Json }
import io.finch._
import io.finch.circe.dropNullValues._

import org.juanitodread.pitayafinch.model.PitayaError
import org.juanitodread.pitayafinch.routes.cookbook.CookbookEndpoints
import org.juanitodread.pitayafinch.routes.nlp.NlpEndpoints

Expand Down Expand Up @@ -36,6 +37,12 @@ object HandleErrorsAsJson extends Logging {
toJson("InvalidJSONProperty", error.getMessage)
}

def pitayaErrorToJson(ex: PitayaError): Json = ex match {
case _ =>
info(ex)
toJson(ex.getTitle, ex.message)
}

private def toJson(error: String, message: String) = {
Json.obj(
"error" -> Json.fromString(error),
Expand All @@ -45,5 +52,6 @@ object HandleErrorsAsJson extends Logging {
implicit val errorEncoder: Encoder[Exception] = Encoder.instance {
case error: Error => errorToJson(error)
case Errors(errors) => Json.arr(errors.toList.map(errorToJson): _*)
case ex: PitayaError => pitayaErrorToJson(ex)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ import io.finch._
import io.finch.catsEffect._
import io.finch.circe._

import org.juanitodread.pitayafinch.model.ResourceNotAvailable
import org.juanitodread.pitayafinch.model.nlp.entities._
import org.juanitodread.pitayafinch.nlp.tools.entities._
import org.juanitodread.pitayafinch.routes.nlp.NlpEndpoint
import org.juanitodread.pitayafinch.utils.AppConf

object EntitiesEndpoint extends NlpEndpoint {
object EntitiesEndpoint extends NlpEndpoint with AppConf {
private final val entitiesPath = basePath :: "entities"

private val postedEntities: Endpoint[IO, EntitiesRequest] = jsonBody[EntitiesRequest]
def getEntities(): Endpoint[IO, EntitiesResponse] = post(entitiesPath :: postedEntities) { request: EntitiesRequest =>
Ok(EntitiesResponse(
request.text,
EntityRecognizer(request.text)))
if (lowResourcesMode) {
Conflict(ResourceNotAvailable(message = "This resource is not allowed when running in low resources mode"))
} else {
Ok(EntitiesResponse(
request.text,
EntityRecognizer(request.text)))
}
}
}
16 changes: 11 additions & 5 deletions src/main/scala/org/juanitodread/pitayafinch/utils/AppConf.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package org.juanitodread.pitayafinch.utils

import com.typesafe.config.ConfigFactory
import com.typesafe.config.{ Config, ConfigFactory }

trait AppConf {
private[this] val config = ConfigFactory.load()

// app
private[this] val appCfg: Config = config.getConfig("app")

// server
private[this] val server = config.getConfig("server")
private[this] val server: Config = config.getConfig("server")

// api
private[this] val apiCfg = config.getConfig("api")
private[this] val apiCfg: Config = config.getConfig("api")

// public app conf
val lowResourcesMode = appCfg.getBoolean("lowResourcesMode")

// public server conf
val serverCtx = server.getString("context")
val port = server.getString("port")
val serverCtx: String = server.getString("context")
val port: String = server.getString("port")

// public api conf
val serviceName = apiCfg.getString("service")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.juanitodread.pitayafinch.nlp.tools.entities.EntityRecognizer
import org.juanitodread.pitayafinch.nlp.tools.entities.models._

object EntityRecognizerFixture extends NumberFormatter {
println("ENTRE AL FIXTURE: EntityRecognizerFixture")
private final val recognizers = List(
DateEntityModel(),
LocationEntityModel(),
Expand Down

0 comments on commit 8a33017

Please sign in to comment.