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.9.0
//> using dep ba.sake::sharaf-undertow:0.21.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 🔗
Try a standalone example:
- Scala CLI examples cover routes, JSON, forms, validation, and native executables.
- HTMX examples demonstrate interactive HTML.
- Authorization examples cover roles, ownership, and access policies.
Explore a larger application:
- API example: JSON and validation.
- Full-stack example: HTML, static files, and forms.
- OpenAPI API starter: generated routes and client, PostgreSQL, and integration tests.
- Todo backend: a TodoBackend implementation with CORS.
- PetClinic: a full-stack application with PostgreSQL and integration tests.
- Full-stack Giter8 template: a starting point for a new application.
For specific integrations, see form login, JWT, and OAuth2 with pac4j; or the http4s and snunit server examples. See also the server reference for platform support and native examples.