Skip to content
Open
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
10 changes: 7 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ lazy val vercelNodeJS = project
)

lazy val examples = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("examples"))
.settings(
libraryDependencies ++= Seq(
Expand All @@ -198,6 +197,12 @@ lazy val examples = crossProject(JSPlatform, JVMPlatform)
)
.settings(commonSettings)
.dependsOn(lambda, lambdaHttp4s)
.jsConfigure(_.dependsOn(vercelNodeJS))
.jsSettings(
scalaJSUseMainModuleInitializer := true,
Compile / mainClass := Some("feral.examples.vercelNodejsHandler"),
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.CommonJSModule) }
)
.enablePlugins(NoPublishPlugin)

lazy val unidocs = project
Expand All @@ -212,8 +217,7 @@ lazy val unidocs = project
inProjects(
lambda.jvm,
lambdaHttp4s.jvm,
lambdaCloudFormationCustomResource.jvm,
vercelNodeJS
lambdaCloudFormationCustomResource.jvm
)
}
)
Expand Down
38 changes: 38 additions & 0 deletions examples/js/src/main/scala/feral/examples/VercelNodejs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.examples

import cats.effect._
import feral.vercel._
import org.http4s._
import org.http4s.dsl.io._

object vercelNodejsHandler extends IOVercel {
def handler = {
val app = HttpRoutes
.of[IO] {
case GET -> Root / "hello" / name =>
Ok(s"Hello, $name.")
}
.orNotFound

Resource.pure(app)

/*Resource.pure(HttpApp.pure(Response[IO](Status.Ok)))*/
}

}
57 changes: 57 additions & 0 deletions vercel-nodejs/src/main/scala/feral/vercel/IOVercel.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.vercel

import cats.effect.IO
import cats.effect.kernel.Resource
import cats.effect.std.Dispatcher
import cats.effect.unsafe.IORuntime
import cats.syntax.all._
import org.http4s.HttpApp
import org.http4s.nodejs.IncomingMessage
import org.http4s.nodejs.ServerResponse

import scala.scalajs.js

abstract class IOVercel {

final def main(args: Array[String]): Unit =
js.Dynamic.global.module.exports = handlerFn

protected def runtime: IORuntime = IORuntime.global

def handler: Resource[IO, HttpApp[IO]]

private[vercel] lazy val handlerFn: js.Function2[IncomingMessage, ServerResponse, Unit] = {
val dispatcherHandle = {
Dispatcher
.parallel[IO](await = false)
.product(handler)
.allocated
.map(_._1) // drop unused finalizer
.unsafeToPromise()(runtime)
}

(request: IncomingMessage, response: ServerResponse) =>
val _ = dispatcherHandle.`then`[Unit] {
case (dispatcher, handle) =>
dispatcher.unsafeRunAndForget(
request.toRequest[IO].flatMap(handle(_)).flatMap(response.writeResponse[IO])
)
}
}
}