diff options
-rw-r--r-- | src/main/resources/swagger-ui/index.html | 4 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/core/rest/Swagger.scala | 34 |
2 files changed, 35 insertions, 3 deletions
diff --git a/src/main/resources/swagger-ui/index.html b/src/main/resources/swagger-ui/index.html index 7610421..9691d7d 100644 --- a/src/main/resources/swagger-ui/index.html +++ b/src/main/resources/swagger-ui/index.html @@ -1,7 +1,7 @@ <!DOCTYPE html> <html> <head> - <title>Users Service</title> + <title>{{title}}</title> <link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" /> <link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" /> <link href='css/typography.css' media='screen' rel='stylesheet' type='text/css'/> @@ -82,7 +82,7 @@ <body class="swagger-section"> <div id='header'> <div class="swagger-ui-wrap"> - <a id="logo" href="http://driver.xyz">Users Service</a> + <a id="logo" href="http://driver.xyz">{{title}}</a> <form id='api_selector'> <div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div> <div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div> diff --git a/src/main/scala/xyz/driver/core/rest/Swagger.scala b/src/main/scala/xyz/driver/core/rest/Swagger.scala index ab5ad76..d110cd3 100644 --- a/src/main/scala/xyz/driver/core/rest/Swagger.scala +++ b/src/main/scala/xyz/driver/core/rest/Swagger.scala @@ -1,6 +1,11 @@ package xyz.driver.core.rest +import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpEntity} import akka.http.scaladsl.server.Route +import akka.http.scaladsl.server.directives.FileAndResourceDirectives.ResourceFile +import akka.stream.ActorAttributes +import akka.stream.scaladsl.{Framing, StreamConverters} +import akka.util.ByteString import com.github.swagger.akka.SwaggerHttpService import com.github.swagger.akka.model._ import com.typesafe.config.Config @@ -87,9 +92,36 @@ class Swagger( vendorExtensions = Map.empty[String, AnyRef] ) + /** A very simple templating extractor. Gets a resource from the classpath and subsitutes any `{{key}}` with a value. */ + private def getTemplatedResource( + resourceName: String, + contentType: ContentType, + substitution: (String, String)): Route = get { + Option(this.getClass.getClassLoader.getResource(resourceName)) flatMap ResourceFile.apply match { + case Some(ResourceFile(url, length, _)) => + extractSettings { settings => + val stream = StreamConverters + .fromInputStream(() => url.openStream()) + .withAttributes(ActorAttributes.dispatcher(settings.fileIODispatcher)) + .via(Framing.delimiter(ByteString("\n"), 4096, true).map(_.utf8String)) + .map { line => + line.replaceAll(s"\\{\\{${substitution._1}\\}\\}", substitution._2) + } + .map(line => ByteString(line)) + complete( + HttpEntity.Default(contentType, length, stream) + ) + } + case None => reject + } + } + def swaggerUI: Route = pathEndOrSingleSlash { - getFromResource("swagger-ui/index.html") + getTemplatedResource( + "swagger-ui/index.html", + ContentTypes.`text/html(UTF-8)`, + "title" -> config.getString("swagger.apiInfo.title")) } ~ getFromResourceDirectory("swagger-ui") } |