-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.11.5 | ||
0.11.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//> using scala "3.4.2" | ||
//> using dep ba.sake::sharaf:0.6.0 | ||
|
||
import io.undertow.Undertow | ||
import ba.sake.querson.QueryStringRW | ||
import ba.sake.tupson.JsonRW | ||
import ba.sake.validson.Validator | ||
import ba.sake.sharaf.*, routing.* | ||
|
||
case class Car(model: String, quantity: Int) derives JsonRW | ||
|
||
case class CarQuery(model: Option[String]) derives QueryStringRW | ||
|
||
var carsDB = Seq[Car]() | ||
|
||
val routes = Routes: | ||
case GET() -> Path("cars") => | ||
val qp = Request.current.queryParamsValidated[CarQuery] | ||
val filteredCars = qp.model match | ||
case Some(b) => carsDB.filter(_.model == b) | ||
case None => carsDB | ||
Response.withBody(filteredCars) | ||
|
||
case POST() -> Path("cars") => | ||
val newCar = Request.current.bodyJsonValidated[Car] | ||
carsDB = carsDB.appended(newCar) | ||
Response.withBody(newCar) | ||
|
||
Undertow.builder | ||
.addHttpListener(8181, "localhost") | ||
.setHandler( | ||
SharafHandler(routes).withExceptionMapper(ExceptionMapper.json) | ||
) | ||
.build | ||
.start() | ||
|
||
println(s"Server started at http://localhost:8181") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters