Skip to content

Commit

Permalink
feat: Add static file paths to remove env specific files in resources
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jul 15, 2024
1 parent 435beae commit 39d0b78
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 97 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ configuration.toml
docker-compose.yml
patches-public-key.asc
integrations-public-key.asc
node_modules/
node_modules/
static/
2 changes: 2 additions & 0 deletions configuration.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ cors-allowed-hosts = [
]
endpoint = "https://api.revanced.app"
old-api-endpoint = "https://old-api.revanced.app"
static-files-path = "static/root"
versioned-static-files-path = "static/versioned"
1 change: 1 addition & 0 deletions docker-compose.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
- /data/revanced-api/configuration.toml:/app/configuration.toml
- /data/revanced-api/patches-public-key.asc:/app/patches-public-key.asc
- /data/revanced-api/integrations-public-key.asc:/app/integrations-public-key.asc
- /data/revanced-api/static:/app/static
environment:
- COMMAND=start
ports:
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/app/revanced/api/configuration/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import io.bkbn.kompendium.core.plugin.NotarizedRoute
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.http.content.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.io.File
import java.nio.file.Path
import kotlin.time.Duration

internal suspend fun ApplicationCall.respondOrNotFound(value: Any?) = respond(value ?: HttpStatusCode.NotFound)
Expand All @@ -25,3 +29,14 @@ internal fun ApplicationCallPipeline.installCache(cacheControl: CacheControl) =

internal fun ApplicationCallPipeline.installNotarizedRoute(configure: NotarizedRoute.Config.() -> Unit = {}) =
install(NotarizedRoute(), configure)

internal fun Route.staticFiles(
remotePath: String,
dir: Path,
block: StaticContentConfig<File>.() -> Unit = {
contentType {
ContentType.Application.Json
}
extensions("json")
},
) = staticFiles(remotePath, dir.toFile(), null, block)
7 changes: 1 addition & 6 deletions src/main/kotlin/app/revanced/api/configuration/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import app.revanced.api.configuration.routes.oldApiRoute
import app.revanced.api.configuration.routes.patchesRoute
import io.bkbn.kompendium.core.routes.redoc
import io.bkbn.kompendium.core.routes.swagger
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.http.content.*
import io.ktor.server.routing.*
import kotlin.time.Duration.Companion.minutes
import org.koin.ktor.ext.get as koinGet
Expand All @@ -27,10 +25,7 @@ internal fun Application.configureRouting() = routing {
apiRoute()
}

staticResources("/", "/app/revanced/api/static/root") {
contentType { ContentType.Application.Json }
extensions("json")
}
staticFiles("/", configuration.staticFilesPath)

swagger(pageTitle = "ReVanced API", path = "/")
redoc(pageTitle = "ReVanced API", path = "/redoc")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.io.File
import java.nio.file.Path

/**
* The repository storing the configuration for the API.
Expand All @@ -24,6 +25,8 @@ import java.io.File
* @property corsAllowedHosts The hosts allowed to make requests to the API.
* @property endpoint The endpoint of the API.
* @property oldApiEndpoint The endpoint of the old API to proxy requests to.
* @property staticFilesPath The path to the static files to be served under the root path.
* @property versionedStaticFilesPath The path to the static files to be served under a versioned path.
*/
@Serializable
internal class ConfigurationRepository(
Expand All @@ -40,6 +43,12 @@ internal class ConfigurationRepository(
val endpoint: String,
@SerialName("old-api-endpoint")
val oldApiEndpoint: String,
@Serializable(with = PathSerializer::class)
@SerialName("static-files-path")
val staticFilesPath: Path,
@Serializable(with = PathSerializer::class)
@SerialName("versioned-static-files-path")
val versionedStaticFilesPath: Path,
) {
/**
* Am asset configuration whose asset is signed.
Expand Down Expand Up @@ -108,3 +117,11 @@ private object FileSerializer : KSerializer<File> {

override fun deserialize(decoder: Decoder) = File(decoder.decodeString())
}

private object PathSerializer : KSerializer<Path> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Path", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: Path) = encoder.encodeString(value.toString())

override fun deserialize(decoder: Decoder) = Path.of(decoder.decodeString())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.revanced.api.configuration.routes

import app.revanced.api.configuration.*
import app.revanced.api.configuration.installCache
import app.revanced.api.configuration.installNoCache
import app.revanced.api.configuration.installNotarizedRoute
Expand All @@ -13,7 +14,6 @@ import io.bkbn.kompendium.core.metadata.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.http.content.*
import io.ktor.server.plugins.ratelimit.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
Expand Down Expand Up @@ -75,10 +75,7 @@ internal fun Route.apiRoute() {
}
}

staticResources("/", "/app/revanced/api/static/versioned") {
contentType { ContentType.Application.Json }
extensions("json")
}
staticFiles("/", apiService.versionedStaticFilesPath)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal class ApiService(
private val backendRepository: BackendRepository,
private val configurationRepository: ConfigurationRepository,
) {
val versionedStaticFilesPath = configurationRepository.versionedStaticFilesPath

suspend fun contributors() = withContext(Dispatchers.IO) {
configurationRepository.contributorsRepositoryNames.map {
async {
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/app/revanced/api/static/root/robots.txt

This file was deleted.

83 changes: 0 additions & 83 deletions src/main/resources/app/revanced/api/static/versioned/about.json

This file was deleted.

0 comments on commit 39d0b78

Please sign in to comment.