Last active
May 25, 2024 08:39
-
-
Save dacr/f6adb563b52b497d9ce44d061355489f to your computer and use it in GitHub Desktop.
http server to serve local static files using tapir endpoint and zhttp / published by https://github.com/dacr/code-examples-manager #32323581-378e-403e-bc77-cede7b4080f4/d63c1d2df83fb6ff21879869b2b044fb930c0861
This file contains hidden or 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
<html> | |
<body> | |
<h1>Hello world</h1> | |
</body> | |
</html> |
This file contains hidden or 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
// summary : http server to serve local static files using tapir endpoint and zhttp | |
// keywords : scala, zio, tapir, http, zhttp, @testable, @exclusive | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 32323581-378e-403e-bc77-cede7b4080f4 | |
// attachments: tapir-zio-static-files.html | |
// created-on : 2022-03-26T08:16:23+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// test-with : curl -L http://127.0.0.1:8080/ | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "com.softwaremill.sttp.tapir::tapir-zio:1.6.0" | |
//> using dep "com.softwaremill.sttp.tapir::tapir-files:1.6.0" | |
//> using dep "com.softwaremill.sttp.tapir::tapir-zio-http-server:1.6.0" | |
// --------------------- | |
import sttp.tapir.ztapir.* | |
import sttp.tapir.server.ziohttp.ZioHttpInterpreter | |
import sttp.tapir.files.{staticFileGetServerEndpoint, staticFilesGetServerEndpoint} | |
import zio.*, zio.http.Server | |
object WebApp extends ZIOAppDefault { | |
override def run = | |
for { | |
cwd <- System.property("user.dir").some | |
serverEndPoints = List( | |
staticFileGetServerEndpoint("")(s"$cwd/tapir-zio-static-files.html").widen[Environment], | |
staticFilesGetServerEndpoint(emptyInput)(cwd).widen[Environment] | |
) | |
routes = ZioHttpInterpreter().toHttp(serverEndPoints) | |
server <- Server.serve(routes.withDefaultErrorResponse).provide(Server.default) | |
} yield server | |
} | |
WebApp.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment