/examples/src/main/scala/HeadersInHeadersOut.scala

https://github.com/jtownson/swakka · Scala · 83 lines · 51 code · 12 blank · 20 comment · 0 complexity · 35313abebcd821f8f1b641830bf9e5f3 MD5 · raw file

  1. /*
  2. * Copyright 2017 Jeremy Townson
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import akka.actor.ActorSystem
  17. import akka.http.scaladsl.Http
  18. import akka.http.scaladsl.model.HttpMethods.GET
  19. import akka.http.scaladsl.model.StatusCodes.NoContent
  20. import akka.http.scaladsl.model.headers.RawHeader
  21. import akka.http.scaladsl.model.HttpResponse
  22. import akka.http.scaladsl.server.Directives.complete
  23. import akka.http.scaladsl.server.Route
  24. import akka.stream.ActorMaterializer
  25. import net.jtownson.swakka.openapijson._
  26. import net.jtownson.swakka.openapimodel._
  27. import net.jtownson.swakka.coreroutegen._
  28. import net.jtownson.swakka.openapiroutegen._
  29. import scala.collection.immutable.Seq
  30. // Shows how to declare
  31. // 1. an endpoint that accepts parameters in headers and returns headers in the response
  32. // 2. extract request information that you do not wish to declare in the swagger definition
  33. //
  34. // Usage: curl -i -H'x-header-in: 3.14' http://localhost:8080/
  35. object HeadersInHeadersOut extends App {
  36. implicit val system = ActorSystem()
  37. implicit val mat = ActorMaterializer()
  38. implicit val executionContext = system.dispatcher
  39. val corsHeaders = Seq(
  40. RawHeader("Access-Control-Allow-Origin", "*"),
  41. RawHeader("Access-Control-Allow-Methods", "GET"))
  42. val multiplyInputBy2: Double => Route = number => {
  43. val ret = (number * 2).toString
  44. complete(HttpResponse(
  45. NoContent,
  46. corsHeaders :+ RawHeader("x-header-out", ret))
  47. )
  48. }
  49. val api =
  50. OpenApi(
  51. paths =
  52. PathItem(
  53. path = "/",
  54. method = GET,
  55. operation = Operation(
  56. parameters = Tuple1(HeaderParameter[Double](Symbol("x-header-in"))),
  57. responses = ResponseValue[Unit, Header[Double]](
  58. responseCode = "204",
  59. description = "the input x-header-in parameter will be multiplied by 2 and returned in x-header-out",
  60. headers = Header[Double](Symbol("x-header-out"), Some("the value of x-header-in multiplied by 2"))),
  61. endpointImplementation = multiplyInputBy2
  62. )
  63. )
  64. )
  65. val route: Route = openApiRoute(api, Some(DocRouteSettings(
  66. corsUseCase = SpecificallyThese(corsHeaders))))
  67. val bindingFuture = Http().bindAndHandle(
  68. route,
  69. "localhost",
  70. 8080)
  71. }