Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rockwotj
Created April 4, 2017 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rockwotj/93a9edd5c25cba06f71bf3dd9228ba2d to your computer and use it in GitHub Desktop.
Save rockwotj/93a9edd5c25cba06f71bf3dd9228ba2d to your computer and use it in GitHub Desktop.
The HTTP GCF example from https://cloud.google.com/functions/docs/writing/http but in scalajs
package com.google.functions.examples.scalajs
import scala.scalajs.js.annotation._
import scala.scalajs.js
import js.DynamicImplicits._
@js.native
trait Request extends js.Any {
val body: js.Dictionary[Any] = js.native
}
@js.native
trait Response extends js.Any {
def status(code: Int): Response = js.native
def send(body: String): Response = js.native
def end(): Response = js.native
}
object MyFunctions {
@JSExportTopLevel("helloWorld")
def helloWorld(req: Request, res: Response): Unit = {
req.body.get("message") match {
case Some(str: String) =>
println(s"Function called with message: '$str'")
res.status(200).end()
case _ =>
res.status(400).send("There was no message defined!")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment