-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SwaggerZioHttp #1369
Merged
Merged
Add SwaggerZioHttp #1369
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
96e89f9
Created module for zioHttp swagger
de63244
Swagger ui for zio http
511d2e2
Add info about SwaggerZioHttp to docs
slabiakt 5dcfa3d
Added Scala 3 to zioHttpSwagger supported Scala versions
cfb42a3
Changed the way of retriving yaml file
e122457
Add ExampleZioHttpServer
slabiakt 23f525d
Merge remote-tracking branch 'origin/ziohttp-swagger' into ziohttp-sw…
slabiakt 7d202e0
Fix SwaggerZioHttp
slabiakt 807733f
Replace ifs with
2850780
Merge branch 'ziohttp-swagger' of github.com:softwaremill/tapir into …
27433de
Rename and added to doc/examples.md
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 46 additions & 0 deletions
46
docs/swagger-ui-zio-http/src/main/scala/sttp/tapir/swagger/ziohttp/SwaggerZioHttp.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,46 @@ | ||
package sttp.tapir.swagger.ziohttp | ||
|
||
import zhttp.http._ | ||
import zio.Chunk | ||
import zio.blocking.Blocking | ||
import zio.stream.ZStream | ||
|
||
import java.util.Properties | ||
|
||
class SwaggerZioHttp( | ||
yaml: String, | ||
contextPath: String = "docs", | ||
yamlName: String = "docs.yaml" | ||
) { | ||
private val resourcePathPrefix = { | ||
val swaggerVersion: String = { | ||
val p = new Properties() | ||
val pomProperties = getClass.getResourceAsStream("/META-INF/maven/org.webjars/swagger-ui/pom.properties") | ||
try p.load(pomProperties) | ||
finally pomProperties.close() | ||
p.getProperty("version") | ||
} | ||
s"META-INF/resources/webjars/swagger-ui/$swaggerVersion" | ||
} | ||
|
||
def route: Http[Blocking, Throwable, Request, Response[Blocking, Throwable]] = { | ||
Http.collect[Request] { | ||
case Method.GET -> Root / path => | ||
if (path.equals(contextPath)) { | ||
val location = s"/$contextPath/index.html?url=/$contextPath/$yamlName" | ||
Response.http(Status.MOVED_PERMANENTLY, List(Header.custom("Location", location))) | ||
} else Response.http(Status.NOT_FOUND) | ||
case Method.GET -> Root / path / resource => | ||
if (path.equals(contextPath)) { | ||
if (resource.equals(yamlName)) { | ||
val body = HttpData.CompleteData(Chunk.fromArray(yaml.getBytes(HTTP_CHARSET))) | ||
Response.http[Blocking, Throwable](Status.OK, List(Header.custom("content-type", "text/yaml")), body) | ||
} else { | ||
val staticResource = this.getClass.getClassLoader.getResourceAsStream(s"$resourcePathPrefix/$resource") | ||
val content = HttpData.fromStream(ZStream.fromInputStream(staticResource)) | ||
Response.http(content = content) | ||
} | ||
} else Response.http(Status.NOT_FOUND) | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
examples/src/main/scala/sttp/tapir/examples/ExampleZioHttpServer.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,47 @@ | ||
package sttp.tapir.examples | ||
|
||
import io.circe.generic.auto._ | ||
import sttp.tapir.generic.auto._ | ||
import sttp.tapir.json.circe._ | ||
import sttp.tapir.server.ziohttp.ZioHttpInterpreter | ||
import sttp.tapir.swagger.ziohttp.SwaggerZioHttp | ||
import sttp.tapir.ztapir._ | ||
import zhttp.http.HttpApp | ||
import zhttp.service.Server | ||
import zio.{App, ExitCode, IO, UIO, URIO, ZIO} | ||
|
||
object ExampleZioHttpServer extends App { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should have a better name :) Plus, please add to |
||
case class Pet(species: String, url: String) | ||
|
||
// Sample endpoint, with the logic implemented directly using .toRoutes | ||
val petEndpoint: ZEndpoint[Int, String, Pet] = | ||
endpoint.get.in("pet" / path[Int]("petId")).errorOut(stringBody).out(jsonBody[Pet]) | ||
|
||
val petRoutes: HttpApp[Any, Throwable] = | ||
ZioHttpInterpreter().toHttp(petEndpoint)(petId => | ||
if (petId == 35) ZIO.succeed(Right(Pet("Tapirus terrestris", "https://en.wikipedia.org/wiki/Tapir"))) | ||
else ZIO.succeed(Left("Unknown pet id")) | ||
) | ||
|
||
// Same as above, but combining endpoint description with server logic: | ||
val petServerEndpoint: ZServerEndpoint[Any, Int, String, Pet] = petEndpoint.zServerLogic { petId => | ||
if (petId == 35) { | ||
UIO(Pet("Tapirus terrestris", "https://en.wikipedia.org/wiki/Tapir")) | ||
} else { | ||
IO.fail("Unknown pet id") | ||
} | ||
} | ||
val petServerRoutes: HttpApp[Any, Throwable] = ZioHttpInterpreter().toHttp(List(petServerEndpoint)) | ||
|
||
// | ||
|
||
val yaml: String = { | ||
import sttp.tapir.docs.openapi.OpenAPIDocsInterpreter | ||
import sttp.tapir.openapi.circe.yaml._ | ||
OpenAPIDocsInterpreter().toOpenAPI(petEndpoint, "Our pets", "1.0").toYaml | ||
} | ||
|
||
// Starting the server | ||
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = | ||
Server.start(8080, petRoutes <> new SwaggerZioHttp(yaml).route).exitCode | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want to match in a pattern match, you can do:
or better:
The backticks are used to tell the pattern matcher, that you want to use the value of the given value (here:
contextPath
), instead of assigning the any matched value to this name