Quickstart 🔗
This lesson starts a Sharaf server and makes one request. You need JDK 21 and Scala CLI installed.
Create a file named hello.sc with this content:
//> using scala "3.7.3"
//> using dep ba.sake::sharaf-undertow:0.19.0
import ba.sake.sharaf.*
import ba.sake.sharaf.undertow.UndertowSharafServer
val routes = Routes:
case GET -> Path("hello", name) =>
Response.withBody(s"Hello $name")
case GET -> _ =>
Response.withBody("Go to http://localhost:8181/hello/your-name")
UndertowSharafServer("localhost", 8181, routes).start()
println(s"Server started at http://localhost:8181")
Run it from the directory containing the file:
scala hello.sc
In another terminal, make a request:
curl http://localhost:8181/hello/Bob
The response is Hello Bob. Stop the server with Ctrl-C.
The route matches a GET request whose path begins with hello and captures the next segment as name. Continue with path parameters, or see dependency setup for Mill and sbt.
More examples 🔗
- Scala CLI examples, standalone examples using Scala CLI
- Scala CLI HTMX examples, standalone examples featuring HTMX
- API example featuring JSON and validation
- OpenAPI API starter, a spec-first Postgres application with generated Sharaf routes and STTP client, Swagger UI, Docker, and integration tests
- full-stack example featuring HTML, static files and forms
- sharaf-todo-backend, implementation of the todobackend.com spec, featuring CORS handling
- Username+Password form login with Pac4J
- JWT auth with Pac4J
- OAuth2 login with Pac4J
- Snunit demo app
- Http4s demo app
- PetClinic implementation, featuring full-stack app with Postgres db, config, integration tests etc.
- Giter8 template for fullstack app