import snunit.*
@main
def run =
SyncServerBuilder
.setRequestHandler(req =>
req.send(
statusCode = StatusCode.OK,
content = "Hello world!\n",
headers = Headers("Content-Type" -> "text/plain")
)
)
.build()
.listen()SNUnit is a Scala Native library to write HTTP server applications on top of NGINX Unit. It allows you to write both synchronous and asynchronous web servers with great performance.
SNUnit has two different server implementations.
With SyncServerBuilder you need to call .listen() to start listening.
It is a blocking operation so your process is stuck on listening and can't do
anything else while listening.
Moreover, all the request handlers need to respond directly and can't be implemented
using Futures or any other asyncronous mechanism since no Future will run, being
the process stuck on the listen() Unit event loop.
With http4s or tapir-cats-effect the server is automatically scheduled to run either on the
cats effect event loop, based on epoll/kqueue.
This allows you to complete requests asyncronously using whatever mechanism you prefer.
A process can accept multiple requests concurrently, allowing great parallelism.
SNUnit offers interpreters for Tapir server endpoints.
You can write all your application using Tapir and the convert your Tapir endpoints
with logic into a SNUnit Handler.
Currently two interpreters are available:
SNUnitIdServerInterpreterwhich works best withSyncServerHandlerfor synchronous applications- You can find an example in tests
- An interpreter for cats hidden behind
snunit.tapir.SNUnitServerBuilderin thesnunit-tapir-cats-effectartifact.- You can find an example in tests
snunit.TapirApp extends cats.effect.IOApp building the SNUnit server.
It exposes a def serverEndpoints: Resource[IO, List[ServerEndpoint[Any, IO]]] that you need to
implement with your server logic.
Here an example "Hello world" app:
import cats.effect.*
import sttp.tapir.*
object Main extends snunit.TapirApp {
def serverEndpoints = Resource.pure(
endpoint.get
.in("hello")
.in(query[String]("name"))
.out(stringBody)
.serverLogic[IO](name => IO(Right(s"Hello $name!"))) :: Nil
)
}SNUnit offers a server implementation for http4s. It is based on the epollcat asynchronous event loop.
There are two ways you can build a http4s server.
snunit.Http4sApp extends cats.effect.IOApp building the SNUnit server.
It exposes a def routes: Resource[IO, HttpApp[IO]] that you need to implement with your
server logic.
Here an example "Hello world" app:
import cats.effect.*
import org.http4s.*
import org.http4s.dsl.io.*
object app extends snunit.Http4sApp {
def routes = Resource.pure(
HttpRoutes
.of[IO] { case GET -> Root =>
Ok("Hello from SNUnit Http4s!")
}
.orNotFound
)
}If you want to have more control over the server creation, you can use the
SNUnitServerBuilder and manually use it.
For example, here you see it in combination with cats.effect.IOApp
package snunit.tests
import cats.effect.*
import org.http4s.*
import org.http4s.dsl.io.*
import snunit.http4s.*
object Http4sHelloWorld extends IOApp.Simple {
def helloWorldRoutes: HttpRoutes[IO] =
HttpRoutes.of[IO] { case GET -> Root =>
Ok("Hello Http4s!")
}
def run: IO[Unit] =
SNUnitServerBuilder
.default[IO]
.withHttpApp(helloWorldRoutes.orNotFound)
.run
}