mirror of
https://github.com/ReVanced/revanced-api.git
synced 2026-01-29 14:11:04 +00:00
feat: Add proxy for old API
This commit is contained in:
@@ -2,6 +2,7 @@ package app.revanced.api.configuration
|
||||
|
||||
import app.revanced.api.repository.AnnouncementRepository
|
||||
import app.revanced.api.repository.ConfigurationRepository
|
||||
import app.revanced.api.repository.OldApiService
|
||||
import app.revanced.api.repository.backend.BackendRepository
|
||||
import app.revanced.api.repository.backend.github.GitHubBackendRepository
|
||||
import app.revanced.api.services.AnnouncementService
|
||||
@@ -11,14 +12,27 @@ import app.revanced.api.services.PatchesService
|
||||
import com.akuleshov7.ktoml.Toml
|
||||
import com.akuleshov7.ktoml.source.decodeFromStream
|
||||
import io.github.cdimascio.dotenv.Dotenv
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.okhttp.*
|
||||
import io.ktor.client.plugins.*
|
||||
import io.ktor.client.plugins.auth.*
|
||||
import io.ktor.client.plugins.auth.providers.*
|
||||
import io.ktor.client.plugins.cache.*
|
||||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.client.plugins.resources.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import io.ktor.server.application.*
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonNamingStrategy
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.bind
|
||||
import org.koin.core.parameter.parameterArrayOf
|
||||
import org.koin.dsl.module
|
||||
import org.koin.ktor.plugin.Koin
|
||||
import java.io.File
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
fun Application.configureDependencies() {
|
||||
val globalModule = module {
|
||||
single {
|
||||
@@ -26,6 +40,16 @@ fun Application.configureDependencies() {
|
||||
.systemProperties()
|
||||
.load()
|
||||
}
|
||||
factory { params ->
|
||||
val defaultRequestUri: String = params.get<String>()
|
||||
val configBlock = params.getOrNull<(HttpClientConfig<OkHttpConfig>.() -> Unit)>() ?: {}
|
||||
|
||||
HttpClient(OkHttp) {
|
||||
defaultRequest { url(defaultRequestUri) }
|
||||
|
||||
configBlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val repositoryModule = module {
|
||||
@@ -40,6 +64,43 @@ fun Application.configureDependencies() {
|
||||
)
|
||||
}
|
||||
|
||||
single<BackendRepository> {
|
||||
GitHubBackendRepository(
|
||||
get {
|
||||
val defaultRequestUri = "https://api.github.com"
|
||||
val configBlock: HttpClientConfig<OkHttpConfig>.() -> Unit = {
|
||||
install(HttpCache)
|
||||
install(Resources)
|
||||
install(ContentNegotiation) {
|
||||
json(
|
||||
Json {
|
||||
ignoreUnknownKeys = true
|
||||
namingStrategy = JsonNamingStrategy.SnakeCase
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
get<Dotenv>()["GITHUB_TOKEN"]?.let {
|
||||
install(Auth) {
|
||||
bearer {
|
||||
loadTokens {
|
||||
BearerTokens(
|
||||
accessToken = it,
|
||||
refreshToken = "", // Required dummy value
|
||||
)
|
||||
}
|
||||
|
||||
sendWithoutRequest { true }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parameterArrayOf(defaultRequestUri, configBlock)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
single {
|
||||
val configFilePath = get<Dotenv>()["CONFIG_FILE_PATH"]
|
||||
val configFile = File(configFilePath).inputStream()
|
||||
@@ -64,10 +125,13 @@ fun Application.configureDependencies() {
|
||||
AuthService(issuer, validityInMin, jwtSecret, basicUsername, basicPassword)
|
||||
}
|
||||
single {
|
||||
val token = get<Dotenv>()["GITHUB_TOKEN"]
|
||||
|
||||
GitHubBackendRepository(token)
|
||||
} bind BackendRepository::class
|
||||
OldApiService(
|
||||
get {
|
||||
val defaultRequestUri = get<Dotenv>()["OLD_API_URL"]
|
||||
parameterArrayOf(defaultRequestUri)
|
||||
},
|
||||
)
|
||||
}
|
||||
singleOf(::AnnouncementService)
|
||||
singleOf(::PatchesService)
|
||||
singleOf(::ApiService)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package app.revanced.api.configuration.routing
|
||||
|
||||
import app.revanced.api.configuration.routing.routes.configureAnnouncementsRoute
|
||||
import app.revanced.api.configuration.routing.routes.configurePatchesRoute
|
||||
import app.revanced.api.configuration.routing.routes.configureRootRoute
|
||||
import app.revanced.api.configuration.routing.routes.announcementsRoute
|
||||
import app.revanced.api.configuration.routing.routes.oldApiRoute
|
||||
import app.revanced.api.configuration.routing.routes.patchesRoute
|
||||
import app.revanced.api.configuration.routing.routes.rootRoute
|
||||
import app.revanced.api.repository.ConfigurationRepository
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.routing.*
|
||||
@@ -12,8 +13,11 @@ internal fun Application.configureRouting() = routing {
|
||||
val configuration = get<ConfigurationRepository>()
|
||||
|
||||
route("/v${configuration.apiVersion}") {
|
||||
configureRootRoute()
|
||||
configurePatchesRoute()
|
||||
configureAnnouncementsRoute()
|
||||
rootRoute()
|
||||
patchesRoute()
|
||||
announcementsRoute()
|
||||
}
|
||||
|
||||
// TODO: Remove, once migration period from v2 API is over (In 1-2 years).
|
||||
oldApiRoute()
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ import io.ktor.server.routing.*
|
||||
import io.ktor.server.util.*
|
||||
import org.koin.ktor.ext.get as koinGet
|
||||
|
||||
internal fun Route.configureAnnouncementsRoute() = route("/announcements") {
|
||||
internal fun Route.announcementsRoute() = route("announcements") {
|
||||
val announcementService = koinGet<AnnouncementService>()
|
||||
|
||||
route("/{channel}/latest") {
|
||||
get("/id") {
|
||||
route("{channel}/latest") {
|
||||
get("id") {
|
||||
val channel: String by call.parameters
|
||||
|
||||
call.respond(
|
||||
@@ -33,14 +33,14 @@ internal fun Route.configureAnnouncementsRoute() = route("/announcements") {
|
||||
}
|
||||
}
|
||||
|
||||
get("/{channel}") {
|
||||
get("{channel}") {
|
||||
val channel: String by call.parameters
|
||||
|
||||
call.respond(announcementService.all(channel))
|
||||
}
|
||||
|
||||
route("/latest") {
|
||||
get("/id") {
|
||||
route("latest") {
|
||||
get("id") {
|
||||
call.respond(announcementService.latestId() ?: return@get call.respond(HttpStatusCode.NotFound))
|
||||
}
|
||||
|
||||
@@ -58,26 +58,26 @@ internal fun Route.configureAnnouncementsRoute() = route("/announcements") {
|
||||
announcementService.new(call.receive<APIAnnouncement>())
|
||||
}
|
||||
|
||||
post("/{id}/archive") {
|
||||
post("{id}/archive") {
|
||||
val id: Int by call.parameters
|
||||
val archivedAt = call.receiveNullable<APIAnnouncementArchivedAt>()?.archivedAt
|
||||
|
||||
announcementService.archive(id, archivedAt)
|
||||
}
|
||||
|
||||
post("/{id}/unarchive") {
|
||||
post("{id}/unarchive") {
|
||||
val id: Int by call.parameters
|
||||
|
||||
announcementService.unarchive(id)
|
||||
}
|
||||
|
||||
patch("/{id}") {
|
||||
patch("{id}") {
|
||||
val id: Int by call.parameters
|
||||
|
||||
announcementService.update(id, call.receive<APIAnnouncement>())
|
||||
}
|
||||
|
||||
delete("/{id}") {
|
||||
delete("{id}") {
|
||||
val id: Int by call.parameters
|
||||
|
||||
announcementService.delete(id)
|
||||
|
||||
@@ -10,31 +10,31 @@ import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import org.koin.ktor.ext.get
|
||||
|
||||
internal fun Route.configureRootRoute() {
|
||||
internal fun Route.rootRoute() {
|
||||
val apiService = get<ApiService>()
|
||||
val authService = get<AuthService>()
|
||||
|
||||
get("/contributors") {
|
||||
get("contributors") {
|
||||
call.respond(apiService.contributors())
|
||||
}
|
||||
|
||||
get("/team") {
|
||||
get("team") {
|
||||
call.respond(apiService.team())
|
||||
}
|
||||
|
||||
route("/ping") {
|
||||
route("ping") {
|
||||
handle {
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
}
|
||||
|
||||
authenticate("basic") {
|
||||
get("/token") {
|
||||
get("token") {
|
||||
call.respond(authService.newToken())
|
||||
}
|
||||
}
|
||||
|
||||
staticResources("/", "/static/api") {
|
||||
staticResources("/", "/app/revanced/api/static") {
|
||||
contentType { ContentType.Application.Json }
|
||||
extensions("json")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package app.revanced.api.configuration.routing.routes
|
||||
|
||||
import app.revanced.api.repository.OldApiService
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.routing.*
|
||||
import org.koin.ktor.ext.get
|
||||
|
||||
internal fun Route.oldApiRoute() {
|
||||
val oldApiService = get<OldApiService>()
|
||||
|
||||
route(Regex("(v2|tools|contributor).*")) {
|
||||
handle {
|
||||
oldApiService.proxy(call)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import org.koin.ktor.ext.get as koinGet
|
||||
|
||||
internal fun Route.configurePatchesRoute() = route("/patches") {
|
||||
internal fun Route.patchesRoute() = route("patches") {
|
||||
val patchesService = koinGet<PatchesService>()
|
||||
|
||||
route("latest") {
|
||||
@@ -15,11 +15,11 @@ internal fun Route.configurePatchesRoute() = route("/patches") {
|
||||
call.respond(patchesService.latestRelease())
|
||||
}
|
||||
|
||||
get("/version") {
|
||||
get("version") {
|
||||
call.respond(patchesService.latestVersion())
|
||||
}
|
||||
|
||||
get("/list") {
|
||||
get("list") {
|
||||
call.respondBytes(ContentType.Application.Json) { patchesService.list() }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user