Skip to content

Commit

Permalink
Generate swagger.json only once
Browse files Browse the repository at this point in the history
  • Loading branch information
jasny committed Mar 4, 2022
1 parent bf45a95 commit bf7a5d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/main/scala/com/ltonetwork/api/CompositeHttpService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.ltonetwork.api
import akka.actor.ActorSystem
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.model.{HttpRequest, StatusCodes}
import akka.http.scaladsl.model.{HttpEntity, HttpRequest, MediaTypes, StatusCodes}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.RouteResult.Complete
import akka.http.scaladsl.server.directives.{DebuggingDirectives, LoggingMagnet}
import akka.http.scaladsl.server.{Directive0, Route, RouteResult}
import com.github.swagger.akka.CustomMediaTypes
import com.github.swagger.akka.SwaggerHttpService.apiDocsBase
import com.ltonetwork.api.swagger.SwaggerDocService
import com.ltonetwork.settings.RestAPISettings
import com.ltonetwork.utils.ScorexLogging
Expand All @@ -22,24 +24,26 @@ case class CompositeHttpService(system: ActorSystem, apiTypes: Set[Class[_]], ro
respondWithHeader(`Access-Control-Allow-Origin`.*)
else pass

private val headers: scala.collection.immutable.Seq[String] = scala.collection.immutable.Seq("Authorization",
"Content-Type",
"X-Requested-With",
"Timestamp",
"Signature") ++
private val headers: scala.collection.immutable.Seq[String] =
scala.collection.immutable.Seq("Authorization", "Content-Type", "X-Requested-With", "Timestamp", "Signature") ++
(if (settings.apiKeyDifferentHost) Seq("X-API-Key") else Seq.empty[String])

val compositeRoute: Route =
withCors(routes.map(_.route).reduce(_ ~ _)) ~
swaggerService.routes ~
(pathEndOrSingleSlash | path("swagger")) {
redirect("/api-docs/index.html", StatusCodes.PermanentRedirect)
} ~
pathPrefix("api-docs") {
pathEndOrSingleSlash {
redirect("/api-docs/index.html", StatusCodes.PermanentRedirect)
} ~
getFromResourceDirectory("swagger-ui")
path("swagger.json") {
complete(HttpEntity(MediaTypes.`application/json`, swaggerService.swaggerJson))
} ~
path("swagger.yaml") {
complete(HttpEntity(MediaTypes.`application/json`, swaggerService.swaggerYaml))
} ~
getFromResourceDirectory("swagger-ui")
} ~ options {
respondWithDefaultHeaders(`Access-Control-Allow-Credentials`(true),
`Access-Control-Allow-Headers`(headers),
Expand Down
12 changes: 10 additions & 2 deletions src/main/scala/com/ltonetwork/api/swagger/SwaggerDocService.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package com.ltonetwork.api.swagger

import akka.actor.ActorSystem
import com.github.swagger.akka.SwaggerHttpService
import akka.http.scaladsl.model.{HttpEntity, MediaTypes}
import akka.http.scaladsl.server.Route
import com.github.swagger.akka.{CustomMediaTypes, SwaggerGenerator, SwaggerHttpService}
import com.github.swagger.akka.SwaggerHttpService.apiDocsBase
import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache}
import com.ltonetwork.Version
import com.ltonetwork.settings.RestAPISettings
import io.swagger.v3.oas.models.info.{Info, License}
import io.swagger.v3.oas.models.security.{SecurityRequirement, SecurityScheme}
import io.swagger.v3.oas.models.servers.Server
import io.swagger.v3.oas.models.{Components, OpenAPI}

class SwaggerDocService(val actorSystem: ActorSystem, val apiClasses: Set[Class[_]], settings: RestAPISettings) extends SwaggerHttpService {
class SwaggerDocService(val actorSystem: ActorSystem, val apiClasses: Set[Class[_]], settings: RestAPISettings) extends SwaggerGenerator {
import SwaggerHttpService._

override val host: String = settings.bindAddress + ":" + settings.port

lazy val swaggerJson = generateSwaggerJson
lazy val swaggerYaml = generateSwaggerYaml

val license = new License()
license.setName("Apache License, Version 2.0")
license.setUrl("https://github.com/legalthings/PublicNode/blob/master/LICENSE")
Expand Down

0 comments on commit bf7a5d9

Please sign in to comment.