Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active August 18, 2023 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/f6adb563b52b497d9ce44d061355489f to your computer and use it in GitHub Desktop.
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/905dadcafc186af233115a162fc9b023c2881ae1
<html>
<body>
<h1>Hello world</h1>
</body>
</html>
// 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.3.0"
//> 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