Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.
Merged
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: 5 additions & 5 deletions frameworks/Scala/finch/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ com.github.retronym.SbtOneJar.oneJarSettings
mainClass in (Compile, run) := Some("WebServer")

libraryDependencies ++= Seq(
"com.github.finagle" %% "finch-core" % "0.8.0",
"com.github.finagle" %% "finch-circe" % "0.8.0",
"io.circe" %% "circe-core" % "0.1.1",
"io.circe" %% "circe-generic" % "0.1.1",
"io.circe" %% "circe-jawn" % "0.1.1"
"com.github.finagle" %% "finch-core" % "0.10.0",
"com.github.finagle" %% "finch-circe" % "0.10.0" ,
"io.circe" %% "circe-core" % "0.3.0",
"io.circe" %% "circe-generic" % "0.3.0",
"io.circe" %% "circe-jawn" % "0.3.0"
)

resolvers += Resolver.sonatypeRepo("snapshots")
36 changes: 20 additions & 16 deletions frameworks/Scala/finch/src/main/scala/WebServer.scala
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
import java.net.InetSocketAddress
import java.text.{DateFormat, SimpleDateFormat}
import java.util.Date

import io.finch._
import io.finch.route._
import io.finch.response._

import com.twitter.finagle.Service
import com.twitter.finagle.Httpx
import com.twitter.finagle.httpx.{Response, Request}

import com.twitter.finagle.Http
import com.twitter.finagle.stats.NullStatsReceiver
import com.twitter.finagle.tracing.NullTracer
import com.twitter.util.Await

import io.circe._
import io.circe.generic.auto._
import io.circe.jawn._
import io.circe.syntax._
import io.finch.response.EncodeResponse
import io.circe.{Decoder, Encoder, Json}

object WebServer extends App {
private val dateFormat: DateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z")

import io.finch.circe._
val json = get("json") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put type here Endpoint[io.circe.Json] and return Json.obj("message" -> Json.string("Hello, World!")) directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider it done.

import io.finch.circe._

case class Message(message: String)
Ok(Message("Hello, World!").asJson)
Ok(Json.obj("message" -> Json.string("Hello, World!")))
.withHeader("Server" -> "finch")
.withHeader("Date" -> dateFormat.format(new Date()))
}

val plaintext = get("plaintext") {
"Hello, World!"
val plaintext: Endpoint[String] = get("plaintext") {
Ok("Hello, World!")
.withHeader("Server" -> "finch")
.withHeader("Date" -> dateFormat.format(new Date()))
.withContentType(Some("text/plain"))
}

val api: Service[Request, Response] = (json :+: plaintext).toService

Await.ready(
Httpx.serve(":9000", api)
Await.ready(Http.server
.withCompressionLevel(0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to bother again, but can you also disable stats receiver and tracer on a server instance? (in a same way as per #1941).

.withStatsReceiver(NullStatsReceiver)
.withTracer(NullTracer)

.withStatsReceiver(NullStatsReceiver)
.withTracer(NullTracer)
.serve(":9000", (json :+: plaintext).toService)
)
}