forked from guardian/grid
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommonConfig.scala
More file actions
252 lines (212 loc) · 10.2 KB
/
Copy pathCommonConfig.scala
File metadata and controls
252 lines (212 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.gu.mediaservice.lib.config
import com.gu.mediaservice.lib.aws.{AwsClientV1BuilderUtils, AwsClientV2BuilderUtils, KinesisSenderConfig}
import com.gu.mediaservice.model.UsageRightsSpec
import com.typesafe.config.Config
import com.typesafe.scalalogging.StrictLogging
import play.api.{ConfigLoader, Configuration}
import java.net.URI
import java.util.UUID
import scala.jdk.CollectionConverters._
import scala.util.Try
abstract class CommonConfig(resources: GridConfigResources) extends AwsClientV1BuilderUtils with AwsClientV2BuilderUtils with StrictLogging {
val configuration: Configuration = resources.configuration
final val stackName = "media-service"
final val sessionId = UUID.randomUUID().toString
// TODO:SAH - remove these and favour explicit config for anything that is derived from here
val stage: String = string(GridConfigLoader.STAGE_KEY)
val appName: String = string(GridConfigLoader.APP_KEY)
val isProd: Boolean = stage == "PROD"
override val isDev: Boolean = stage == "DEV"
override val awsRegion: String = stringDefault("aws.region", "eu-west-1")
override val awsLocalEndpoint: Option[String] = if(isDev) stringOpt("aws.local.endpoint").filter(_.nonEmpty) else None
override val awsLocalEndpointUri: Option[URI] = awsLocalEndpoint.map(new URI(_))
val useLocalAuth: Boolean = isDev && boolean("auth.useLocal")
val localLogShipping: Boolean = sys.env.getOrElse("LOCAL_LOG_SHIPPING", "false").toBoolean
val thrallKinesisStream = string("thrall.kinesis.stream.name")
val thrallKinesisLowPriorityStream = string("thrall.kinesis.lowPriorityStream.name")
val thrallKinesisStreamConfig = getKinesisConfigForStream(thrallKinesisStream)
val thrallKinesisLowPriorityStreamConfig = getKinesisConfigForStream(thrallKinesisLowPriorityStream)
val requestMetricsEnabled: Boolean = boolean("metrics.request.enabled")
val defaultShouldBlurGraphicImages: Boolean = boolean("defaultShouldBlurGraphicImages")
val staffPhotographerOrganisation: String = stringOpt("branding.staffPhotographerOrganisation").filterNot(_.isEmpty).getOrElse("GNM")
val shouldDisplayOrgOwnedCountAndFilterCheckbox: Boolean = boolean("filters.shouldDisplayOrgOwnedCountAndFilterCheckbox")
val systemName: String = stringOpt("branding.systemName").filterNot(_.isEmpty).getOrElse("the Grid")
lazy val softDeletedMetadataTable: String = string("dynamo.table.softDelete.metadata")
val maybeIngestSqsQueueUrl: Option[String] = stringOpt("sqs.ingest.queue.url")
val maybeIngestBucket: Option[String] = stringOpt("s3.ingest.bucket")
val maybeFailBucket: Option[String] = stringOpt("s3.fail.bucket")
val maybeUploadLimitInBytes: Option[Int] = intOpt("upload.limit.mb").map(_ * 1_000_000)
// Note: had to make these lazy to avoid init order problems ;_;
val domainRoot: String = string("domain.root")
val domainRootOverride: Option[String] = stringOpt("domain.root-override")
val rootAppName: String = stringDefault("app.name.root", "media")
val serviceHosts = ServiceHosts(
stringDefault("hosts.kahunaPrefix", s"$rootAppName."),
stringDefault("hosts.apiPrefix", s"api.$rootAppName."),
stringDefault("hosts.loaderPrefix", s"loader.$rootAppName."),
stringDefault("hosts.projectionPrefix", s"loader-projection.$rootAppName."),
stringDefault("hosts.cropperPrefix", s"cropper.$rootAppName."),
stringDefault("hosts.metadataPrefix", s"$rootAppName-metadata."),
stringDefault("hosts.imgopsPrefix", s"$rootAppName-imgops."),
stringDefault("hosts.usagePrefix", s"$rootAppName-usage."),
stringDefault("hosts.collectionsPrefix", s"$rootAppName-collections."),
stringDefault("hosts.leasesPrefix", s"$rootAppName-leases."),
stringDefault("hosts.authPrefix", s"$rootAppName-auth."),
stringDefault("hosts.thrallPrefix", s"thrall.$rootAppName.")
)
val corsAllowedOrigins: Set[String] = getStringSet("security.cors.allowedOrigins")
val services = new Services(domainRoot, serviceHosts, corsAllowedOrigins, domainRootOverride)
/**
* Load in a list of domain metadata specifications from configuration. For example:
* {{{
* domainMetadata.specifications = [
* {
* name: "specificationa"
* label: "Specification A"
* description: "Description of specification A"
* fields = [
* {
* name = "field-a"
* label = "Field A"
* key = "Search field key for A" (optional)
* type = "string" # type can either be string, integer, select or datetime
* }
* {
* name = "field-b"
* label = "Field B"
* type = "integer"
* }
* {
* name = "field-c"
* label = "Field C"
* type = "datetime"
* }
* {
* name = "field-d"
* label = "Field D"
* type = "select"
* options = ["Option 1", "Option 2"]
* }
* ]
* }
* ]
* }}}
*/
val domainMetadataSpecs: Seq[DomainMetadataSpec] = configuration.getOptional[Seq[DomainMetadataSpec]]("domainMetadata.specifications").getOrElse(Seq.empty)
val fieldAliasConfigs: Seq[FieldAlias] = configuration.get[Seq[FieldAlias]]("field.aliases")
val recordDownloadAsUsage: Boolean = boolean("image.record.download")
val shortenDownloadFilename: Boolean = boolean("image.download.shorten")
/**
* Load in a list of external staff photographers, internal staff photographers, contracted photographers,
* contract illustrators, staff illustrators and creative commons licenses. For example:
* {{{
* usageRightsConfigProvider {
* className: "com.gu.mediaservice.lib.config.RuntimeUsageRightsConfig"
* config {
* externalStaffPhotographers = [
* {
* name = "Publication 1",
* photographers = ["John Doe"]
* }
* ]
* internalStaffPhotographers = [
* {
* name = "Publication 1",
* photographers = ["Jane Doe"]
* }
* ]
* contractedPhotographers = [
* {
* name = "Contract Photographers 1",
* photographers = ["Peter Larry"]
* }
* ]
* contractIllustrators = [
* {
* name = "Contract Illustrators 1",
* photographers = ["Tom Hardy"]
* }
* ]
* staffIllustrators = ["John Doe", "Jane Doe", "Larry Wolf"]
* creativeCommonsLicense = [
* "CC BY-4.0",
* "CC BY-SA-4.0",
* "CC BY-ND-4.0"
* ]
* freeSuppliers = ["Supplier 1", "Supplier 2"]
* suppliersCollectionExcl {
* Supplier 1 = ["Coll 1", "Coll 2"]
* }
* programmesOrganisationOwned {
* description = "This is a configurable description of the usage right." # Optional config - if not sensible default description is used
* }
* programmesIndependents {
* description = "This is a configurable description of the usage right." # Optional config - if not sensible default description is used
* independentTypes = [
* {
* name = "BBC Studios"
* productionsCompanies = ["Studio 1", "Studio 2"]
* }
* {
* name = "Independent"
* productionsCompanies = ["A", "B", "C"]
* }
* {
* name = "Joint Production"
* productionsCompanies = ["Joint Prod 1", "Joint Prod 2"]
* }
* ]
* }
* programmesAcquisitions {
* description = "This is a configurable description of the usage right." # Optional config - if not sensible default description is used
* }
* }
* }
* }}}
*/
val usageRightsConfig: UsageRightsConfigProvider = {
implicit val loader: ConfigLoader[UsageRightsConfigProvider] =
UsageRightsConfigProvider.ProviderLoader.singletonConfigLoader(UsageRightsConfigProvider.Resources(), resources.applicationLifecycle)
configuration.get[UsageRightsConfigProvider]("usageRightsConfigProvider")
}
/**
* Load in a list of applicable usage right objects that implement [[com.gu.mediaservice.model.UsageRightsSpec]] from config. For example:
* {{{
* usageRights.applicable = [
* "com.gu.mediaservice.model.NoRights",
* "com.gu.mediaservice.model.Handout"
* ]
* }}}
*
* Depending on the type it will be loaded differently using reflection. Companion objects will be looked up
* and the singleton instance added to the list.
*/
val applicableUsageRights: Seq[UsageRightsSpec] = configuration.get[Seq[UsageRightsSpec]]("usageRights.applicable")
val stdUserExcludedUsageRights = getStringSet("usageRights.stdUserExcluded")
private def getKinesisConfigForStream(streamName: String) = KinesisSenderConfig(awsRegion, awsCredentials, awsLocalEndpoint, isDev, streamName)
final def getOptionalStringSet(key: String): Option[Set[String]] = Try {
configuration.getOptional[Seq[String]](key)
}.getOrElse(
configuration.getOptional[String](key).map(_.split(",").toSeq.map(_.trim))
).map(_.toSet)
final def getStringSet(key: String): Set[String] = getOptionalStringSet(key).getOrElse(Set.empty)
def getConfigList(key:String): List[_ <: Config] =
if (configuration.has(key)) configuration.underlying.getConfigList(key).asScala.toList
else List.empty
final def apply(key: String): String =
string(key)
final def string(key: String): String =
configuration.getOptional[String](key) getOrElse missing(key, "string")
final def stringDefault(key: String, default: String): String =
configuration.getOptional[String](key) getOrElse default
final def stringOpt(key: String): Option[String] = configuration.getOptional[String](key)
final def intOpt(key: String): Option[Int] = configuration.getOptional[Int](key)
final def intDefault(key: String, default: Int): Int =
configuration.getOptional[Int](key) getOrElse default
final def boolean(key: String): Boolean =
configuration.getOptional[Boolean](key).getOrElse(false)
final def booleanOpt(key: String): Option[Boolean] =
configuration.getOptional[Boolean](key)
private def missing(key: String, type_ : String): Nothing =
sys.error(s"Required $type_ configuration property missing: $key")
}