Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,33 @@ lazy val root =
project
.in(file("."))
.aggregate(
core.js,
core.jvm,
lambda.js,
lambda.jvm,
lambdaEvents.js,
lambdaEvents.jvm,
lambdaApiGatewayProxyHttp4s.js,
lambdaApiGatewayProxyHttp4s.jvm)
lambdaApiGatewayProxyHttp4s.jvm,
cloudflareWorker
)
.enablePlugins(NoPublishPlugin)

lazy val core = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("core"))
.settings(
name := "feral-core",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % catsEffectVersion,
)
)

lazy val lambda = crossProject(JSPlatform, JVMPlatform)
.in(file("lambda"))
.settings(
name := "feral-lambda",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % catsEffectVersion,
"io.circe" %%% "circe-core" % circeVersion
)
)
Expand All @@ -63,6 +76,7 @@ lazy val lambda = crossProject(JSPlatform, JVMPlatform)
"io.circe" %%% "circe-fs2" % "0.14.0"
)
)
.dependsOn(core)

lazy val lambdaEvents = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
Expand All @@ -84,3 +98,18 @@ lazy val lambdaApiGatewayProxyHttp4s = crossProject(JSPlatform, JVMPlatform)
)
)
.dependsOn(lambda, lambdaEvents)

lazy val cloudflareWorker = project
.in(file("cloudflare-worker"))
.enablePlugins(ScalaJSPlugin)
.settings(
name := "feral-cloudflare-worker",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % catsEffectVersion,
"co.fs2" %%% "fs2-core" % fs2Version,
"io.circe" %%% "circe-generic" % circeVersion,
"io.circe" %%% "circe-scalajs" % circeVersion,
"org.http4s" %%% "http4s-client" % http4sVersion
)
).dependsOn(core.js)

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package feral
package cloudflare.worker

import cats.effect.IO
import cats.effect.SyncIO
import cats.effect.kernel.Resource
import cats.effect.std.Supervisor
import io.circe.Decoder
import io.circe.Json
import io.circe.generic.semiauto._
import io.circe.scalajs._
import org.http4s.HttpRoutes
import org.http4s.Request
import org.typelevel.vault.Key
import org.http4s.HttpVersion

trait FetchEventListener extends IOSetup {

def routes: Resource[IO, HttpRoutes[IO]]

final type Setup = HttpRoutes[IO]
protected override final val setup = routes

private def apply(event: facade.FetchEvent): Unit =
event.waitUntil(
Supervisor[IO]
.use { supervisor =>
for {
routes <- setupMemo
request <- fromRequest[IO](event.request)
properties <- IO.fromEither(decodeJs[IncomingRequestCfProperties](event.request.cf))
httpVersion <- IO.fromEither(HttpVersion.fromString(properties.httpProtocol))
_ <- routes(request
.withHttpVersion(httpVersion)
.withAttribute(FetchEventContext.key, FetchEventContext(supervisor, properties)))
.map(toResponse[IO])
.foldF(IO.unit)(r => IO(event.respondWith(r)))
} yield ()
}
.unsafeToPromise()(runtime))

final def main(args: Array[String]): Unit =
addEventListener[facade.FetchEvent]("fetch", apply)

}

final case class FetchEventContext(
supervisor: Supervisor[IO],
properties: IncomingRequestCfProperties)

object FetchEventContext {
private[worker] val key = Key.newKey[SyncIO, FetchEventContext].unsafeRunSync()
def apply(request: Request[IO]): IO[FetchEventContext] =
IO.fromEither(request.attributes.lookup(key).toRight(new NoSuchElementException))
}

final case class IncomingRequestCfProperties(
asn: String,
colo: String,
country: Option[String],
httpProtocol: String,
requestPriority: Option[String],
tlsCipher: String,
tlsClientAuth: Option[Json],
tlsVersion: String,
city: Option[String],
continent: Option[String],
latitude: Option[String],
longitude: Option[String],
postalCode: Option[String],
metroCode: Option[String],
region: Option[String],
regionCode: Option[String],
timezone: String
)

object IncomingRequestCfProperties {
private[worker] implicit def decoder: Decoder[IncomingRequestCfProperties] = deriveDecoder
}
Loading