/examples/akkaHttp/src/main/scala/ru/tinkoff/tschema/examples/TestServer.scala

https://github.com/TinkoffCreditSystems/typed-schema · Scala · 71 lines · 62 code · 9 blank · 0 comment · 1 complexity · 63e1b39f69762ac7be541fd4659274d8 MD5 · raw file

  1. package ru.tinkoff.tschema.examples
  2. import java.util.Locale
  3. import akka.actor.ActorSystem
  4. import akka.http.scaladsl.Http
  5. import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse}
  6. import akka.http.scaladsl.server.Directives.{complete, get, pathPrefix, _}
  7. import akka.stream.ActorMaterializer
  8. import cats.instances.list._
  9. import cats.syntax.foldable._
  10. import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
  11. import io.circe.Printer
  12. import ru.tinkoff.tschema.swagger.{OpenApiInfo, PathDescription}
  13. object TestServer {
  14. implicit val system = ActorSystem()
  15. implicit val mat = ActorMaterializer()
  16. import system.dispatcher
  17. val descriptions =
  18. PathDescription.utf8I18n("swagger", Locale.forLanguageTag("ru"))
  19. val modules = List[ExampleModule](
  20. TestModule,
  21. VersionModule,
  22. FiltersModule,
  23. FormFieldsModule,
  24. Authorize,
  25. CustomAuth,
  26. MultiParameters,
  27. ProxyModule
  28. ).combineAll
  29. private[this] implicit val printer: Printer =
  30. Printer.noSpaces.copy(dropNullValues = true)
  31. val route =
  32. pathPrefix("api") {
  33. modules.route
  34. } ~
  35. path("swagger")(
  36. get(
  37. complete(
  38. modules.swag
  39. .describe(descriptions)
  40. .make(OpenApiInfo())
  41. .addServer("/api")
  42. )
  43. )
  44. ) ~
  45. pathPrefix("webjars")(
  46. getFromResourceDirectory("META-INF/resources/webjars")
  47. ) ~
  48. path("swagger.php")(
  49. complete(
  50. HttpResponse(
  51. entity = HttpEntity(
  52. contentType = ContentTypes.`text/html(UTF-8)`,
  53. string = SwaggerIndex.index.render
  54. )
  55. )
  56. )
  57. )
  58. def main(args: Array[String]): Unit = {
  59. for (_ <- Http().bindAndHandle(route, "localhost", 8081))
  60. println("server started at http://localhost:8081/swagger.php")
  61. }
  62. }