/
AppConfig.scala
287 lines (253 loc) · 7.93 KB
/
AppConfig.scala
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.config
import com.typesafe.config.ConfigFactory
import zio._
import zio.config._
import zio.config.magnolia._
import zio.config.typesafe._
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.Duration
import scala.concurrent.duration
import scala.util.Failure
import scala.util.Success
import scala.util.Try
import dsp.errors.FileWriteException
import org.knora.webapi.messages.util.rdf.RdfFeatureFactory
import org.knora.webapi.util.cache.CacheUtil
/**
* Represents the configuration as defined in application.conf.
*/
final case class AppConfig(
testing: Boolean = false,
printExtendedConfig: Boolean,
defaultTimeout: String,
dumpMessages: Boolean,
showInternalErrors: Boolean,
bcryptPasswordStrength: Int,
jwtSecretKey: String,
jwtLongevity: String,
cookieDomain: String,
allowReloadOverHttp: Boolean,
fallbackLanguage: String,
knoraApi: KnoraApi,
sipi: Sipi,
ark: Ark,
salsah1: Salsah1,
caches: List[CacheConfig],
tmpDatadir: String,
datadir: String,
maxResultsPerSearchResultPage: Int,
standoffPerPage: Int,
v2: V2,
gui: Gui,
routesToReject: List[String],
triplestore: Triplestore,
shacl: Shacl,
cacheService: CacheService,
clientTestDataService: ClientTestDataService,
instrumentationServerConfig: InstrumentationServerConfig
) {
val jwtLongevityAsDuration = scala.concurrent.duration.Duration(jwtLongevity)
val defaultTimeoutAsDuration =
scala.concurrent.duration.Duration.apply(defaultTimeout).asInstanceOf[duration.FiniteDuration]
val cacheConfigs: Seq[org.knora.webapi.util.cache.CacheUtil.KnoraCacheConfig] = caches.map { c =>
CacheUtil.KnoraCacheConfig(
c.cacheName,
c.maxElementsInMemory,
c.overflowToDisk,
c.eternal,
c.timeToLiveSeconds,
c.timeToIdleSeconds
)
}
// create the directories
val tmpDataDirCreation: Try[Path] = Try {
Files.createDirectories(Paths.get(tmpDatadir))
}
tmpDataDirCreation match {
case Success(_) => ZIO.logInfo(s"Created tmp directory $tmpDatadir")
case Failure(e) => throw FileWriteException(s"Tmp data directory $tmpDatadir could not be created: ${e.getMessage}")
}
val dataDirCreation: Try[Path] = Try {
Files.createDirectories(Paths.get(datadir))
}
dataDirCreation match {
case Success(_) => ZIO.logInfo(s"Created directory $datadir")
case Failure(e) => throw FileWriteException(s"Data directory $datadir could not be created: ${e.getMessage}")
}
}
final case class KnoraApi(
internalHost: String,
internalPort: Int,
externalProtocol: String,
externalHost: String,
externalPort: Int,
externalZioPort: Int
) {
val internalKnoraApiBaseUrl: String = "http://" + internalHost + (if (internalPort != 80)
":" + internalPort
else "")
val externalKnoraApiHostPort: String = externalHost + (if (externalPort != 80)
":" + externalPort
else "")
val externalKnoraApiBaseUrl: String = externalProtocol + "://" + externalHost + (if (externalPort != 80)
":" + externalPort
else "")
/**
* If the external hostname is localhost or 0.0.0.0, include the configured
* external port number in ontology IRIs for manual testing.
*/
val externalOntologyIriHostAndPort: String =
if (externalHost == "0.0.0.0" || externalHost == "localhost") {
externalKnoraApiHostPort
} else {
// Otherwise, don't include any port number in IRIs, so the IRIs will work both with http
// and with https.
externalHost
}
}
final case class Sipi(
internalProtocol: String,
internalHost: String,
internalPort: Int,
timeout: String,
externalProtocol: String,
externalHost: String,
externalPort: Int,
fileServerPath: String,
moveFileRoute: String,
deleteTempFileRoute: String,
imageMimeTypes: List[String],
documentMimeTypes: List[String],
textMimeTypes: List[String],
videoMimeTypes: List[String],
audioMimeTypes: List[String],
archiveMimeTypes: List[String]
) {
val internalBaseUrl: String =
internalProtocol + "://" + internalHost + (if (internalPort != 80) ":" + internalPort else "")
val externalBaseUrl: String =
externalProtocol + "://" + externalHost + (if (externalPort != 80) ":" + externalPort else "")
val timeoutInSeconds: duration.Duration = scala.concurrent.duration.Duration(timeout)
}
final case class Ark(
resolver: String,
assignedNumber: Int
)
final case class Salsah1(
baseUrl: String,
projectIconsBasepath: String
)
final case class CacheConfig(
cacheName: String,
maxElementsInMemory: Int,
overflowToDisk: Boolean,
eternal: Boolean,
timeToLiveSeconds: Int,
timeToIdleSeconds: Int
)
final case class V2(
resourcesSequence: ResourcesSequence,
fulltextSearch: FulltextSearch,
graphRoute: GraphRoute
)
final case class ResourcesSequence(
resultsPerPage: Int
)
final case class FulltextSearch(
searchValueMinLength: Int
)
final case class GraphRoute(
defaultGraphDepth: Int,
maxGraphDepth: Int,
maxGraphBreadth: Int
)
final case class Gui(
defaultIconSize: DefaultIconSize
)
final case class DefaultIconSize(
dimX: Int,
dimY: Int
)
final case class Triplestore(
dbtype: String,
useHttps: Boolean,
host: String,
queryTimeout: String,
gravsearchTimeout: String,
autoInit: Boolean,
fuseki: Fuseki,
profileQueries: Boolean
) {
val queryTimeoutAsDuration = zio.Duration.fromScala(scala.concurrent.duration.Duration(queryTimeout))
val gravsearchTimeoutAsDuration = zio.Duration.fromScala(scala.concurrent.duration.Duration(gravsearchTimeout))
}
final case class Fuseki(
port: Int,
repositoryName: String,
username: String,
password: String
)
final case class Shacl(
shapesDir: String
) {
val shapesDirPath = Paths.get(shapesDir)
}
final case class CacheService(
enabled: Boolean,
redis: Redis
)
final case class Redis(
host: String,
port: Int
)
final case class ClientTestDataService(
collectClientTestData: Boolean
)
final case class InstrumentationServerConfig(
port: Int,
interval: Duration
)
/**
* Loads the applicaton configuration using ZIO-Config. ZIO-Config is capable of loading
* the Typesafe-Config format.
*/
object AppConfig {
/**
* Reads in the applicaton configuration using ZIO-Config. ZIO-Config is capable of loading
* the Typesafe-Config format. Reads the 'app' configuration from 'application.conf'.
*/
private val source: ConfigSource =
TypesafeConfigSource.fromTypesafeConfig(ZIO.attempt(ConfigFactory.load().getConfig("app").resolve))
/**
* Instantiates our config class hierarchy using the data from the 'app' configuration from 'application.conf'.
*/
private val configFromSource: IO[ReadError[String], AppConfig] = read(
descriptor[AppConfig].mapKey(toKebabCase) from source
)
/**
* Application configuration from application.conf
*/
val live: ZLayer[Any, Nothing, AppConfig] =
ZLayer {
for {
c <- configFromSource.orDie
_ <- ZIO.attempt(RdfFeatureFactory.init(c)).orDie // needs early init before first usage
} yield c
}.tap(_ => ZIO.logInfo(">>> AppConfig Live Initialized <<<"))
/**
* Application configuration from test.conf for testing
*/
val test: ZLayer[Any, Nothing, AppConfig] =
ZLayer {
for {
c <- configFromSource.orDie
_ <- ZIO.attempt(RdfFeatureFactory.init(c)).orDie // needs early init before first usage
} yield c
}.tap(_ => ZIO.logInfo(">>> AppConfig Test Initialized <<<"))
}