Skip to content

Commit

Permalink
feat: Setup cors and cache
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jun 8, 2024
1 parent f5ef072 commit accfa3d
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 23 deletions.
1 change: 1 addition & 0 deletions configuration.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ contributors-repositories = [
"revanced-manager",
]
api-version = 1
host = "*.revanced.app"
1 change: 1 addition & 0 deletions configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ contributors-repositories = [
"revanced-manager",
]
api-version = 1
host = "*.revanced.app"
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal object StartAPICommand : Runnable {
override fun run() {
embeddedServer(Jetty, port, host) {
configureDependencies(configFile)
configureHTTP(allowedHost = host)
configureHTTP()
configureSerialization()
configureSecurity()
configureRouting()
Expand Down
22 changes: 7 additions & 15 deletions src/main/kotlin/app/revanced/api/configuration/HTTP.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package app.revanced.api.configuration

import io.ktor.http.*
import io.ktor.http.content.*
import app.revanced.api.configuration.repository.ConfigurationRepository
import io.ktor.server.application.*
import io.ktor.server.plugins.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.ratelimit.*
import org.koin.ktor.ext.get
import kotlin.time.Duration.Companion.minutes

fun Application.configureHTTP(
allowedHost: String,
) {
fun Application.configureHTTP() {
val configurationRepository = get<ConfigurationRepository>()

install(CORS) {
allowMethod(HttpMethod.Options)
allowMethod(HttpMethod.Put)
allowMethod(HttpMethod.Delete)
allowMethod(HttpMethod.Patch)
allowHeader(HttpHeaders.Authorization)
allowHost(allowedHost)
}
install(CachingHeaders) {
options { _, _ -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 5.minutes.inWholeSeconds.toInt())) }
allowHost(host = configurationRepository.host)
}

install(RateLimit) {
register(RateLimitName("weak")) {
rateLimiter(limit = 30, refillPeriod = 2.minutes)
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/app/revanced/api/configuration/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ import app.revanced.api.configuration.routes.announcementsRoute
import app.revanced.api.configuration.routes.oldApiRoute
import app.revanced.api.configuration.routes.patchesRoute
import app.revanced.api.configuration.routes.rootRoute
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.routing.*
import org.koin.ktor.ext.get
import kotlin.time.Duration.Companion.minutes

internal fun Application.configureRouting() = routing {
val configuration = get<ConfigurationRepository>()

install(CachingHeaders) {
options { _, _ ->
CachingOptions(
CacheControl.MaxAge(maxAgeSeconds = 5.minutes.inWholeSeconds.toInt()),
)
}
}

route("/v${configuration.apiVersion}") {
rootRoute()
patchesRoute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ internal class ConfigurationRepository(
val contributorsRepositoryNames: Set<String>,
@SerialName("api-version")
val apiVersion: Int = 1,
val host: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ import app.revanced.api.configuration.respondOrNotFound
import app.revanced.api.configuration.schema.APIAnnouncement
import app.revanced.api.configuration.schema.APIAnnouncementArchivedAt
import app.revanced.api.configuration.services.AnnouncementService
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.plugins.ratelimit.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.util.*
import kotlin.time.Duration.Companion.minutes
import org.koin.ktor.ext.get as koinGet

internal fun Route.announcementsRoute() = route("announcements") {
val announcementService = koinGet<AnnouncementService>()

install(CachingHeaders) {
options { _, _ ->
CachingOptions(
CacheControl.MaxAge(maxAgeSeconds = 1.minutes.inWholeSeconds.toInt()),
)
}
}

rateLimit(RateLimitName("weak")) {
route("{channel}/latest") {
get("id") {
Expand All @@ -39,6 +51,7 @@ internal fun Route.announcementsRoute() = route("announcements") {
call.respond(announcementService.all(channel))
}
}

rateLimit(RateLimitName("strong")) {
route("latest") {
get("id") {
Expand Down
40 changes: 33 additions & 7 deletions src/main/kotlin/app/revanced/api/configuration/routes/ApiRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import app.revanced.api.configuration.respondOrNotFound
import app.revanced.api.configuration.services.ApiService
import app.revanced.api.configuration.services.AuthService
import io.ktor.http.*
import io.ktor.http.content.CachingOptions
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.http.content.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.ratelimit.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.koin.ktor.ext.get
import kotlin.time.Duration.Companion.days
import org.koin.ktor.ext.get as koinGet

internal fun Route.rootRoute() {
val apiService = get<ApiService>()
val authService = get<AuthService>()
val apiService = koinGet<ApiService>()
val authService = koinGet<AuthService>()

rateLimit(RateLimitName("strong")) {
authenticate("basic") {
Expand All @@ -23,16 +27,38 @@ internal fun Route.rootRoute() {
}
}

get("contributors") {
call.respond(apiService.contributors())
route("contributors") {
install(CachingHeaders) {
options { _, _ ->
CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 1.days.inWholeSeconds.toInt()))
}
}

get {
call.respond(apiService.contributors())
}
}

get("team") {
call.respond(apiService.team())
route("team") {
install(CachingHeaders) {
options { _, _ ->
CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 1.days.inWholeSeconds.toInt()))
}
}

get {
call.respond(apiService.team())
}
}
}

route("ping") {
install(CachingHeaders) {
options { _, _ ->
CachingOptions(CacheControl.NoCache(null))
}
}

handle {
call.respond(HttpStatusCode.NoContent)
}
Expand Down

0 comments on commit accfa3d

Please sign in to comment.