Skip to content

Commit c93c0c1

Browse files
committed
Genericize object storage backend for private storage
In reality, most "S3-compatible" service providers are not, in fact, S3-compatible and so waste our time by causing compatibility issues with our code when we choose to use a S3 feature not supported by the service provider we currently happen to be using. Instead, genericize the object storage backend as preliminary work toward implementing proper first-party support for other object storage backends.
1 parent e94abbd commit c93c0c1

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

.run/console.run.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SPDX-License-Identifier: AGPL-3.0-only
1414
<env name="DEBUG_USER_REVIEWER_EMAIL" value="[email protected]" />
1515
<env name="POSTGRESQL_PASSWORD" value="password" />
1616
<env name="POSTGRESQL_SSL_MODE" value="disable" />
17+
<env name="PRIVATE_STORAGE_BACKEND" value="S3" />
1718
<env name="GITHUB_OAUTH2_REDIRECT_URL" value="http://localhost:4200/auth/github/callback" />
1819
<env name="HOST" value="0.0.0.0" />
1920
<env name="PORT" value="8080" />

console/src/main/kotlin/app/accrescent/parcelo/console/Application.kt

+23-14
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,20 @@ fun Application.module() {
6161
password = System.getenv("POSTGRESQL_PASSWORD"),
6262
sslMode = System.getenv("POSTGRESQL_SSL_MODE") ?: POSTGRESQL_DEFAULT_SSL_MODE,
6363
),
64-
privateStorage = Config.S3(
65-
endpointUrl = System.getenv("PRIVATE_STORAGE_ENDPOINT_URL"),
66-
region = System.getenv("PRIVATE_STORAGE_REGION"),
67-
bucket = System.getenv("PRIVATE_STORAGE_BUCKET"),
68-
accessKeyId = System.getenv("PRIVATE_STORAGE_ACCESS_KEY_ID"),
69-
secretAccessKey = System.getenv("PRIVATE_STORAGE_SECRET_ACCESS_KEY"),
70-
),
64+
privateStorage = System.getenv("PRIVATE_STORAGE_BACKEND")?.let {
65+
when (it) {
66+
"S3" -> Config.ObjectStorage.S3(
67+
endpointUrl = System.getenv("PRIVATE_STORAGE_ENDPOINT_URL"),
68+
region = System.getenv("PRIVATE_STORAGE_REGION"),
69+
bucket = System.getenv("PRIVATE_STORAGE_BUCKET"),
70+
accessKeyId = System.getenv("PRIVATE_STORAGE_ACCESS_KEY_ID"),
71+
secretAccessKey = System.getenv("PRIVATE_STORAGE_SECRET_ACCESS_KEY"),
72+
)
73+
74+
else ->
75+
throw Exception("invalid private storage backend $it; must be one of [S3]")
76+
}
77+
} ?: throw Exception("PRIVATE_STORAGE_BACKEND is not specified in the environment"),
7178
s3 = Config.S3(
7279
endpointUrl = System.getenv("S3_ENDPOINT_URL"),
7380
region = System.getenv("S3_REGION"),
@@ -90,13 +97,15 @@ fun Application.module() {
9097
val mainModule = module {
9198
single { config }
9299
single<FileStorageService> {
93-
S3FileStorageService(
94-
Url.parse(config.privateStorage.endpointUrl),
95-
config.privateStorage.region,
96-
config.privateStorage.bucket,
97-
config.privateStorage.accessKeyId,
98-
config.privateStorage.secretAccessKey,
99-
)
100+
when (config.privateStorage) {
101+
is Config.ObjectStorage.S3 -> S3FileStorageService(
102+
Url.parse(config.privateStorage.endpointUrl),
103+
config.privateStorage.region,
104+
config.privateStorage.bucket,
105+
config.privateStorage.accessKeyId,
106+
config.privateStorage.secretAccessKey,
107+
)
108+
}
100109
}
101110
single { HttpClient { install(HttpTimeout) } }
102111
single<PublishService> {

console/src/main/kotlin/app/accrescent/parcelo/console/Config.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ data class Config(
88
val application: Application,
99
val cors: Cors,
1010
val postgresql: Postgresql,
11-
val privateStorage: S3,
11+
val privateStorage: ObjectStorage,
1212
val s3: S3,
1313
val github: GitHub,
1414
) {
@@ -25,6 +25,16 @@ data class Config(
2525
val sslMode: String,
2626
)
2727

28+
sealed class ObjectStorage {
29+
data class S3(
30+
val endpointUrl: String,
31+
val region: String,
32+
val bucket: String,
33+
val accessKeyId: String,
34+
val secretAccessKey: String,
35+
) : ObjectStorage()
36+
}
37+
2838
data class S3(
2939
val endpointUrl: String,
3040
val region: String,

0 commit comments

Comments
 (0)