-
Notifications
You must be signed in to change notification settings - Fork 1
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
21 changed files
with
290 additions
and
36 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
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
59 changes: 59 additions & 0 deletions
59
examples/jvm/src/main/scala/kreuzberg/examples/showcase/ServerMainLoom.scala
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,59 @@ | ||
package kreuzberg.examples.showcase | ||
|
||
import kreuzberg.examples.showcase.todo.{ListItemResponse, TodoApi} | ||
import kreuzberg.miniserver.loom.MiniServer | ||
import kreuzberg.miniserver.{AssetCandidatePath, AssetPaths, MiniServerConfig} | ||
import kreuzberg.rpc.{Dispatcher, Id, SecurityError} | ||
|
||
import scala.annotation.experimental | ||
|
||
class TodoServiceLoom extends TodoApi[Id] { | ||
private var items: Vector[String] = Vector.empty | ||
|
||
private object lock | ||
|
||
override def listItems(): ListItemResponse = { | ||
lock.synchronized { | ||
ListItemResponse( | ||
items, | ||
statusCode = 200 | ||
) | ||
} | ||
} | ||
|
||
override def addItem(item: String): Unit = { | ||
lock.synchronized { | ||
items = items :+ item | ||
} | ||
} | ||
} | ||
|
||
@experimental | ||
object ServerMainLoom extends App { | ||
val todoDispatcher: Dispatcher[Id] = | ||
Dispatcher.makeIdDispatcher[TodoApi[Id]](new TodoServiceLoom: TodoApi[Id]).preRequestFlatMap { request => | ||
// Demonstrating adding a pre filter | ||
// Note: Headers are lower cased | ||
val id = request.headers.collectFirst { | ||
case (key, value) if key == "x-client-id" => value | ||
} | ||
id match { | ||
case None => throw new SecurityError("Missing client id") | ||
case _ => request | ||
} | ||
} | ||
|
||
val config = MiniServerConfig[Id]( | ||
AssetPaths( | ||
Seq( | ||
AssetCandidatePath("examples/js/target/client_bundle/client/fast"), | ||
AssetCandidatePath("../examples/js/target/client_bundle/client/fast"), | ||
AssetCandidatePath("../../examples/js/target/client_bundle/client/fast") | ||
) | ||
), | ||
api = Some(todoDispatcher) | ||
) | ||
|
||
val miniServer = MiniServer(config) | ||
miniServer.run() | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions
5
...in/scala/kreuzberg/miniserver/Index.scala → ...in/scala/kreuzberg/miniserver/Index.scala
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
49 changes: 49 additions & 0 deletions
49
miniserver-loom/src/main/scala/kreuzberg/miniserver/loom/ApiHandler.scala
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,49 @@ | ||
package kreuzberg.miniserver.loom | ||
|
||
import io.circe.Json | ||
import kreuzberg.rpc.{Dispatcher, Id, UnknownServiceError} | ||
import sttp.model.StatusCode | ||
import sttp.tapir.* | ||
import sttp.tapir.json.circe.* | ||
|
||
import scala.util.control.NonFatal | ||
import quest._ | ||
|
||
case class ApiHandler(dispatcher: Dispatcher[Id]) { | ||
|
||
val apiEndpoint | ||
: PublicEndpoint[(List[String], Json, List[sttp.model.Header]), (Json, StatusCode), (Json, StatusCode), Any] = { | ||
endpoint.post | ||
.in("api" / paths) | ||
.in(jsonBody[Json]) | ||
.in(headers) | ||
.out(jsonBody[Json]) | ||
.out(statusCode) | ||
.errorOut(jsonBody[Json]) | ||
.errorOut(statusCode) | ||
} | ||
|
||
val handler = apiEndpoint.serverLogic[Id] { case (paths, json, headers) => | ||
quest[Either[(Json, StatusCode), (Json, StatusCode)]] { | ||
val (serviceName, callName) = paths match { | ||
case List(s, c) => (s, c) | ||
case _ => bail(Left(Json.obj("msg" -> Json.fromString("Invalid path")) -> StatusCode.BadRequest)) | ||
} | ||
|
||
if (!dispatcher.handles(serviceName)) { | ||
bail(Left(UnknownServiceError(serviceName).encodeToJson -> StatusCode.NotFound)) | ||
} | ||
|
||
val request = kreuzberg.rpc.Request(json, headers.map { h => h.name -> h.value }) | ||
val response = | ||
try { | ||
dispatcher.call(serviceName, callName, request) | ||
} catch { | ||
case NonFatal(e) => | ||
val decoded = kreuzberg.rpc.Failure.fromThrowable(e) | ||
bail(Left(decoded.encodeToJson -> StatusCode.InternalServerError)) | ||
} | ||
Right(response.json -> StatusCode(response.statusCode)) | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
miniserver-loom/src/main/scala/kreuzberg/miniserver/loom/MiniServer.scala
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,79 @@ | ||
package kreuzberg.miniserver.loom | ||
|
||
import kreuzberg.miniserver.{DeploymentType, Index, MiniServerConfig} | ||
import org.slf4j.LoggerFactory | ||
import sttp.model.StatusCode | ||
import sttp.tapir.* | ||
import sttp.tapir.server.ServerEndpoint | ||
import sttp.tapir.server.netty.loom.{Id, NettyIdServer} | ||
import sttp.tapir.swagger.bundle.SwaggerInterpreter | ||
|
||
import java.nio.ByteBuffer | ||
import scala.util.Using | ||
|
||
class MiniServer(config: MiniServerConfig[Id]) { | ||
val logger = LoggerFactory.getLogger(getClass) | ||
|
||
def run(): Unit = { | ||
|
||
val endpoints = List( | ||
assetHandler, | ||
indexHandler | ||
) ++ apiEndpointHandler.toList ++ List( | ||
otherIndexHandler | ||
) | ||
|
||
val docEndpoints: List[ServerEndpoint[Any, Id]] = SwaggerInterpreter() | ||
.fromServerEndpoints[Id](endpoints, "MiniServer", "0.1") | ||
|
||
val server = NettyIdServer() | ||
.port(config.port) | ||
.addEndpoints(endpoints) | ||
.addEndpoints(if (config.deploymentType.contains(DeploymentType.Debug)) docEndpoints else Nil) | ||
.start() | ||
logger.info(s"Listening on port ${config.port}") | ||
} | ||
|
||
private val indexHtml: String = Index(config).index.toString | ||
|
||
val assetEndpoint: PublicEndpoint[List[String], StatusCode, ByteBuffer, Any] = { | ||
endpoint.get | ||
.in("assets" / paths) | ||
.errorOut(statusCode) | ||
.out(byteBufferBody) | ||
} | ||
|
||
private val assetHandler = assetEndpoint.serverLogic[Id] { paths => | ||
val fullName = paths.mkString("/") | ||
config.locateAsset(fullName) match { | ||
case None => Left(StatusCode.NotFound) | ||
case Some(value) => | ||
Using.resource(value.load()) { data => | ||
val bytes = data.readAllBytes() | ||
Right(ByteBuffer.wrap(bytes)) | ||
} | ||
} | ||
} | ||
|
||
val indexEndpoint: PublicEndpoint[Unit, Unit, String, Any] = { | ||
endpoint.get | ||
.in("") | ||
.out(htmlBodyUtf8) | ||
} | ||
|
||
private val indexHandler = indexEndpoint.serverLogicSuccess[Id] { _ => | ||
indexHtml | ||
} | ||
|
||
val otherIndexEndpoint: PublicEndpoint[List[String], Unit, String, Any] = { | ||
endpoint.get | ||
.in(paths) | ||
.out(htmlBodyUtf8) | ||
} | ||
|
||
private val otherIndexHandler = otherIndexEndpoint.serverLogicSuccess[Id] { _ => | ||
indexHtml | ||
} | ||
|
||
val apiEndpointHandler = config.api.map(ApiHandler(_).handler) | ||
} |
9 changes: 3 additions & 6 deletions
9
.../kreuzberg/miniserver/ApiDispatcher.scala → ...rg/miniserver/ziohttp/ApiDispatcher.scala
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
Oops, something went wrong.