Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scalafmt to the project #3151

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = 2.7.5
20 changes: 12 additions & 8 deletions admin-tools/dev/app/AdminToolsComponents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ import router.Routes

object AdminToolsComponents {
def config(resources: GridConfigResources) = new AdminToolsConfig(
resources.configuration ++ Configuration.from(Map(
"domain.root" -> "local.dev-gutools.co.uk",
"auth.keystore.bucket" -> "not-used",
"thrall.kinesis.stream.name"-> "not-used",
"thrall.kinesis.lowPriorityStream.name"-> "not-used"
))
resources.configuration ++ Configuration.from(
Map(
"domain.root" -> "local.dev-gutools.co.uk",
"auth.keystore.bucket" -> "not-used",
"thrall.kinesis.stream.name" -> "not-used",
"thrall.kinesis.lowPriorityStream.name" -> "not-used"
)
)
)
}

class AdminToolsComponents(context: Context) extends GridComponents(context, AdminToolsComponents.config) {
class AdminToolsComponents(context: Context)
extends GridComponents(context, AdminToolsComponents.config) {
final override val buildInfo = utils.buildinfo.BuildInfo

val controller = new AdminToolsCtr(config, controllerComponents)

override lazy val router = new Routes(httpErrorHandler, controller, management)
override lazy val router =
new Routes(httpErrorHandler, controller, management)
}
3 changes: 2 additions & 1 deletion admin-tools/dev/app/AppLoader.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.gu.mediaservice.lib.play.GridAppLoader

class AppLoader extends GridAppLoader("admin-tools", new AdminToolsComponents(_))
class AppLoader
extends GridAppLoader("admin-tools", new AdminToolsComponents(_))
41 changes: 32 additions & 9 deletions admin-tools/dev/app/controllers/AdminToolsCtr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@ package controllers
import com.gu.mediaservice.lib.argo.ArgoHelpers
import com.gu.mediaservice.lib.argo.model.Link
import com.gu.mediaservice.model.Image._
import com.gu.mediaservice.{FullImageProjectionFailed, FullImageProjectionSuccess, ImageDataMerger, ImageDataMergerConfig}
import com.gu.mediaservice.{
FullImageProjectionFailed,
FullImageProjectionSuccess,
ImageDataMerger,
ImageDataMergerConfig
}
import lib.AdminToolsConfig
import play.api.libs.json.Json
import play.api.mvc.{BaseController, ControllerComponents}

import scala.concurrent.ExecutionContext

class AdminToolsCtr(config: AdminToolsConfig, override val controllerComponents: ControllerComponents)(implicit val ec: ExecutionContext)
extends BaseController with ArgoHelpers {
class AdminToolsCtr(
config: AdminToolsConfig,
override val controllerComponents: ControllerComponents
)(implicit val ec: ExecutionContext)
extends BaseController
with ArgoHelpers {

private val cfg = ImageDataMergerConfig(apiKey = config.apiKey, domainRoot = config.domainRoot, imageLoaderEndpointOpt = None)
private val cfg = ImageDataMergerConfig(
apiKey = config.apiKey,
domainRoot = config.domainRoot,
imageLoaderEndpointOpt = None
)

private val merger = new ImageDataMerger(cfg)

Expand All @@ -39,13 +52,23 @@ class AdminToolsCtr(config: AdminToolsConfig, override val controllerComponents:
case Some(img) =>
Ok(Json.toJson(img)).as(ArgoMediaType)
case _ =>
respondError(NotFound, "not-found", s"image with mediaId: $mediaId not found")
respondError(
NotFound,
"not-found",
s"image with mediaId: $mediaId not found"
)
}
case FullImageProjectionFailed(expMessage, downstreamMessage) =>
respondError(InternalServerError, "image-projection-failed", Json.obj(
"errorMessage" -> expMessage,
"downstreamErrorMessage" -> downstreamMessage
).toString)
respondError(
InternalServerError,
"image-projection-failed",
Json
.obj(
"errorMessage" -> expMessage,
"downstreamErrorMessage" -> downstreamMessage
)
.toString
)
}
}
}
3 changes: 2 additions & 1 deletion admin-tools/dev/app/lib/AdminToolsConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package lib
import com.gu.mediaservice.lib.config.CommonConfig
import play.api.Configuration

class AdminToolsConfig(playAppConfiguration: Configuration) extends CommonConfig(playAppConfiguration) {
class AdminToolsConfig(playAppConfiguration: Configuration)
extends CommonConfig(playAppConfiguration) {
// hardcoded for dev
val apiKey: String = "dev-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class BatchIndexLambdaHandler {
threshold = sys.env.get("LATENCY_THRESHOLD").map(t => Integer.parseInt(t)),
maxSize = sys.env("MAX_SIZE").toInt,
startState = IndexInputCreation.get(sys.env("START_STATE").toInt),
checkerStartState = IndexInputCreation.get(sys.env("CHECKER_START_STATE").toInt)
checkerStartState =
IndexInputCreation.get(sys.env("CHECKER_START_STATE").toInt)
)

private val batchIndex = new BatchIndexHandler(cfg)


def handleRequest() = {
batchIndex.processImages()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.gu.mediaservice

import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.events.{APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent}
import com.amazonaws.services.lambda.runtime.events.{
APIGatewayProxyRequestEvent,
APIGatewayProxyResponseEvent
}
import com.gu.mediaservice.lib.auth.provider.ApiKeyAuthenticationProvider
import com.gu.mediaservice.model.Image
import com.typesafe.scalalogging.LazyLogging
Expand All @@ -12,7 +15,10 @@ import scala.concurrent.ExecutionContext.Implicits.global

class ImageProjectionLambdaHandler extends LazyLogging {

def handleRequest(event: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent = {
def handleRequest(
event: APIGatewayProxyRequestEvent,
context: Context
): APIGatewayProxyResponseEvent = {

logger.info(s"handleImageProjection event: $event")

Expand All @@ -27,14 +33,19 @@ class ImageProjectionLambdaHandler extends LazyLogging {

apiKey match {
case Some(key) =>
val cfg: ImageDataMergerConfig = ImageDataMergerConfig(apiKey = key, domainRoot = domainRoot, imageLoaderEndpointOpt = imageLoaderEndpoint)
val cfg: ImageDataMergerConfig = ImageDataMergerConfig(
apiKey = key,
domainRoot = domainRoot,
imageLoaderEndpointOpt = imageLoaderEndpoint
)
if (!cfg.isValidApiKey()) return getUnauthorisedResponse

logger.info(s"starting handleImageProjection for mediaId=$mediaId")
logger.info(s"with config: $cfg")

val merger = new ImageDataMerger(cfg)
val result: FullImageProjectionResult = merger.getMergedImageData(mediaId.asInstanceOf[String])
val result: FullImageProjectionResult =
merger.getMergedImageData(mediaId.asInstanceOf[String])
result match {
case FullImageProjectionSuccess(mayBeImage) =>
mayBeImage match {
Expand All @@ -60,19 +71,27 @@ class ImageProjectionLambdaHandler extends LazyLogging {
}

private def getNotFoundResponse(mediaId: String) = {
val emptyRes = Json.obj("message" -> s"image with id=$mediaId not-found").toString
val emptyRes =
Json.obj("message" -> s"image with id=$mediaId not-found").toString
logger.info(s"image not projected \n $emptyRes")
new APIGatewayProxyResponseEvent()
.withStatusCode(404)
.withHeaders(Map("content-type" -> "application/json").asJava)
.withBody(emptyRes)
}

private def getErrorFoundResponse(message: String, downstreamMessage: String) = {
val res = Json.obj("message" -> Json.obj(
"errorMessage" -> message,
"downstreamErrorMessage" -> downstreamMessage
)).toString
private def getErrorFoundResponse(
message: String,
downstreamMessage: String
) = {
val res = Json
.obj(
"message" -> Json.obj(
"errorMessage" -> message,
"downstreamErrorMessage" -> downstreamMessage
)
)
.toString

logger.info(s"image not projected due to error \n $res")

Expand All @@ -83,7 +102,8 @@ class ImageProjectionLambdaHandler extends LazyLogging {
}

private def getUnauthorisedResponse = {
val res = Json.obj("message" -> s"missing or invalid api key header").toString
val res =
Json.obj("message" -> s"missing or invalid api key header").toString

new APIGatewayProxyResponseEvent()
.withStatusCode(401)
Expand All @@ -93,8 +113,10 @@ class ImageProjectionLambdaHandler extends LazyLogging {

private def getAuthKeyFrom(headers: Map[String, String]) = {
// clients like curl or API gateway may lowerCases custom header names, yay!
headers.find {
case (k, _) => k.equalsIgnoreCase(ApiKeyAuthenticationProvider.apiKeyHeaderName)
}.map(_._2)
headers
.find { case (k, _) =>
k.equalsIgnoreCase(ApiKeyAuthenticationProvider.apiKeyHeaderName)
}
.map(_._2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import scala.collection.JavaConverters._

object ImagesGroupByProgressState extends App with LazyLogging {

if (args.isEmpty) throw new IllegalArgumentException("please provide dynamo table name")
if (args.isEmpty)
throw new IllegalArgumentException("please provide dynamo table name")

import InputIdsStore._

Expand All @@ -19,7 +20,8 @@ object ImagesGroupByProgressState extends App with LazyLogging {

def stateNameToCount(progressType: ProduceProgress): (String, Int) = {
logger.info(s"calculating stateNameToCount for $progressType")
val queryRes = stateIndex.query(getAllMediaIdsWithinProgressQuery(progressType))
val queryRes =
stateIndex.query(getAllMediaIdsWithinProgressQuery(progressType))
val result = progressType.name -> queryRes.iterator.asScala.length
logger.info(s"result=$result")
result
Expand All @@ -42,7 +44,7 @@ object ImagesGroupByProgressState extends App with LazyLogging {
stateNameToCount(NotFound),
stateNameToCount(KnownError),
stateNameToCount(NotStarted),
stateNameToCount(InProgress),
stateNameToCount(InProgress)
).mkString("\n")

logger.info(s"results from dynamoTable=$dynamoTable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import scala.concurrent.duration.Duration

object ResetImageBatchIndexTable extends App with LazyLogging {

if (args.isEmpty) throw new IllegalArgumentException("please provide dynamo table name")
if (args.isEmpty)
throw new IllegalArgumentException("please provide dynamo table name")

private val dynamoTable = args(0)
private val ddbClient = AwsHelpers.buildDynamoTableClient(dynamoTable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import scala.collection.JavaConverters._

object ResetKnownErrors extends App with LazyLogging {

if (args.isEmpty) throw new IllegalArgumentException("please provide dynamo table name")
if (args.isEmpty)
throw new IllegalArgumentException("please provide dynamo table name")

import IndexInputCreation._
import InputIdsStore._
Expand All @@ -21,13 +22,18 @@ object ResetKnownErrors extends App with LazyLogging {
val ignoredAtThisScript = 100
val InputIdsStore = new InputIdsStore(ddbClient, ignoredAtThisScript)

val mediaIDsWithKnownErrors = stateIndex.query(getAllMediaIdsWithinProgressQuery(KnownError))
.asScala.toList.map { it =>
val json = Json.parse(it.toJSON).as[JsObject]
(json \ PKField).as[String]
}

logger.info(s"got ${mediaIDsWithKnownErrors.size}, mediaIds blacklisted as KnownError")
val mediaIDsWithKnownErrors = stateIndex
.query(getAllMediaIdsWithinProgressQuery(KnownError))
.asScala
.toList
.map { it =>
val json = Json.parse(it.toJSON).as[JsObject]
(json \ PKField).as[String]
}

logger.info(
s"got ${mediaIDsWithKnownErrors.size}, mediaIds blacklisted as KnownError"
)
InputIdsStore.resetItemsState(mediaIDsWithKnownErrors)
}

Expand Down
24 changes: 15 additions & 9 deletions auth/app/auth/AuthComponents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import play.api.{Configuration, Environment}
import play.api.http.HttpConfiguration
import router.Routes

class AuthComponents(context: Context) extends GridComponents(context, new AuthConfig(_)) {
final override lazy val httpConfiguration = AuthHttpConfig(configuration, context.environment)
class AuthComponents(context: Context)
extends GridComponents(context, new AuthConfig(_)) {
final override lazy val httpConfiguration =
AuthHttpConfig(configuration, context.environment)

final override val buildInfo = utils.buildinfo.BuildInfo

val controller = new AuthController(auth, providers, config, controllerComponents)
val permissionsAwareManagement = new ManagementWithPermissions(controllerComponents, controller, buildInfo)
val controller =
new AuthController(auth, providers, config, controllerComponents)
val permissionsAwareManagement =
new ManagementWithPermissions(controllerComponents, controller, buildInfo)

override val router = new Routes(httpErrorHandler, controller, permissionsAwareManagement)
override val router =
new Routes(httpErrorHandler, controller, permissionsAwareManagement)
}

object AuthHttpConfig {
def apply(playConfig: Configuration, environment: Environment): HttpConfiguration = {
def apply(
playConfig: Configuration,
environment: Environment
): HttpConfiguration = {
val base = HttpConfiguration.fromConfiguration(playConfig, environment)
base.copy(session =
base.session.copy(sameSite = None)
)
base.copy(session = base.session.copy(sameSite = None))
}
}
3 changes: 2 additions & 1 deletion auth/app/auth/AuthConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package auth

import com.gu.mediaservice.lib.config.{CommonConfig, GridConfigResources}

class AuthConfig(resources: GridConfigResources) extends CommonConfig(resources.configuration) {
class AuthConfig(resources: GridConfigResources)
extends CommonConfig(resources.configuration) {
val rootUri: String = services.authBaseUri
val mediaApiUri: String = services.apiBaseUri
val kahunaUri = services.kahunaBaseUri
Expand Down
Loading