From 1c7233c323bcda60e66bba8316b88addbb5cb88c Mon Sep 17 00:00:00 2001 From: Lundez Date: Tue, 6 Sep 2022 20:01:00 +0200 Subject: [PATCH 1/3] feat: Api route tickers --- src/com/londogard/api/ApiRoute.kt | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/com/londogard/api/ApiRoute.kt b/src/com/londogard/api/ApiRoute.kt index e9c44ef..f69b9bc 100644 --- a/src/com/londogard/api/ApiRoute.kt +++ b/src/com/londogard/api/ApiRoute.kt @@ -14,9 +14,15 @@ import com.londogard.summarizer.SummarizeReq import com.londogard.summarizer.summarize import com.londogard.textgen.LanguageModelHelper import com.londogard.textgen.TextGenInput -import io.ktor.server.application.call +import io.ktor.client.* +import io.ktor.client.engine.cio.* +import io.ktor.client.request.* +import io.ktor.client.statement.* import io.ktor.server.auth.authenticate import io.ktor.http.* +import io.ktor.http.content.* +import io.ktor.server.application.* +import io.ktor.server.plugins.cachingheaders.* import io.ktor.server.request.receive import io.ktor.server.request.receiveOrNull import io.ktor.server.response.* @@ -43,6 +49,21 @@ fun Route.apiRoute(redirections: MutableMap): Route = route("/ap call.respond(splitBills(payments)) } + route("/easyindex") { + install(CachingHeaders) { + options { call, content -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 5*60)) } + } + get("/{ticker}") { + val ticker = call.parameters["ticker"] ?: throw InvalidRouteException() + val yahoo = HttpClient(CIO) + .use { client -> + client.get("https://query1.finance.yahoo.com/v7/finance/quote?symbols=$ticker").bodyAsText() + } + + call.respond(resultResponse(yahoo)) + } + } + /** * POST: /api/url * {url: string} --> {result: string} (result is the hash) From 339df583c8d183c27d755725b4867a8f12a44bc6 Mon Sep 17 00:00:00 2001 From: Lundez Date: Tue, 6 Sep 2022 20:10:31 +0200 Subject: [PATCH 2/3] maybe fix cache? --- src/com/londogard/Main.kt | 6 +++++- src/com/londogard/api/ApiRoute.kt | 6 +----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/com/londogard/Main.kt b/src/com/londogard/Main.kt index c1573cc..a15a908 100644 --- a/src/com/londogard/Main.kt +++ b/src/com/londogard/Main.kt @@ -28,6 +28,7 @@ import io.ktor.server.plugins.hsts.* import io.ktor.server.plugins.httpsredirect.* import io.ktor.server.plugins.partialcontent.* import io.ktor.server.plugins.statuspages.* +import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.server.util.* @@ -151,7 +152,10 @@ fun Application.module() { } install(CachingHeaders) { - options { _, outgoingContent -> + options { call, outgoingContent -> + if ("easyindex" in call.request.uri) { + return@options CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 5 * 60), ZonedDateTime.now().plusMinutes(5)) + } when (outgoingContent.contentType?.withoutParameters()) { ContentType.Text.CSS -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 24 * 60 * 60), ZonedDateTime.now().plusDays(1)) ContentType.Application.Rss -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 24 * 60 * 60), ZonedDateTime.now().plusDays(1)) diff --git a/src/com/londogard/api/ApiRoute.kt b/src/com/londogard/api/ApiRoute.kt index f69b9bc..451c1a5 100644 --- a/src/com/londogard/api/ApiRoute.kt +++ b/src/com/londogard/api/ApiRoute.kt @@ -23,8 +23,7 @@ import io.ktor.http.* import io.ktor.http.content.* import io.ktor.server.application.* import io.ktor.server.plugins.cachingheaders.* -import io.ktor.server.request.receive -import io.ktor.server.request.receiveOrNull +import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* import kotlinx.coroutines.Dispatchers @@ -50,9 +49,6 @@ fun Route.apiRoute(redirections: MutableMap): Route = route("/ap } route("/easyindex") { - install(CachingHeaders) { - options { call, content -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 5*60)) } - } get("/{ticker}") { val ticker = call.parameters["ticker"] ?: throw InvalidRouteException() val yahoo = HttpClient(CIO) From a494c3c36c149aa5ef51c29a2d046c147c7c5f1b Mon Sep 17 00:00:00 2001 From: Lundez Date: Sun, 11 Sep 2022 13:33:02 +0200 Subject: [PATCH 3/3] fix: CORS to only londogard subdomains --- src/com/londogard/Main.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/com/londogard/Main.kt b/src/com/londogard/Main.kt index a15a908..7657950 100644 --- a/src/com/londogard/Main.kt +++ b/src/com/londogard/Main.kt @@ -66,7 +66,8 @@ fun Application.module() { // Default: [Accept, AcceptLanguages, ContentLanguage, ContentType] install(CORS) { - anyHost() + //anyHost() + allowHost("*.londogard.com", schemes = listOf("https")) allowMethod(HttpMethod.Options) allowHeader("Authorization") allowNonSimpleContentTypes = true