Skip to content

Commit

Permalink
Merge pull request #556 from mkurz/rm_lightbend_examples_service
Browse files Browse the repository at this point in the history
Migrate away from example.lightbend.com webservice
  • Loading branch information
mkurz authored Oct 30, 2023
2 parents 986135d + 2e650ce commit ab98d70
Show file tree
Hide file tree
Showing 6 changed files with 2,040 additions and 10 deletions.
26 changes: 18 additions & 8 deletions app/models/PlayExampleProjects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package models

import com.google.inject.AbstractModule
import com.google.inject.Singleton
import org.apache.commons.io.IOUtils

import javax.inject.Inject
import play.api.Configuration
import play.api.Environment
import play.api.cache.SyncCacheApi
import play.api.libs.json._
import play.api.libs.ws.WSClient
Expand All @@ -28,7 +31,6 @@ object TemplateParameter {

case class ExampleProject(
displayName: String,
downloadUrl: String,
gitHubRepo: String,
gitHubUrl: String,
keywords: Seq[String],
Expand All @@ -54,13 +56,14 @@ class PlayExampleProjectsService @Inject() (
configuration: Configuration,
ws: WSClient,
cache: SyncCacheApi,
environment: Environment
)(implicit ec: ExecutionContext) {

val validPlayVersions: Seq[String] = configuration.get[Seq[String]]("examples.playVersions")

private val logger = org.slf4j.LoggerFactory.getLogger(this.getClass)

private val examplesUrl = configuration.get[String]("examples.apiUrl")
//private val examplesUrl = configuration.get[String]("examples.apiUrl")

// NOTE: TTL is really just a safety measure here.
// We should re-deploy when we make major changes to projects
Expand Down Expand Up @@ -89,12 +92,19 @@ class PlayExampleProjectsService @Inject() (

def examples(): Future[Seq[ExampleProject]] = {
Future
.sequence(validPlayVersions.map { version =>
ws.url(examplesUrl)
.withQueryStringParameters(playQueryString(version): _*)
.get()
.map(response => (version, response.json))
})
.sequence(validPlayVersions.map { version => {
lazy val samples: JsValue =
environment
.resourceAsStream(s"playSamples_${version}.json")
.flatMap { is =>
try {
Json.fromJson[JsValue](Json.parse(IOUtils.toByteArray(is))).asOpt
} finally {
is.close()
}
}.getOrElse(JsArray())
Future.successful(version, samples)
}})
.map { response =>
response.flatMap((convertExampleProjects _).tupled)
}
Expand Down
2 changes: 1 addition & 1 deletion app/views/gettingStarted.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@renderProject(project: ExampleProject) = {
<tr>
<td><a href="@project.downloadUrl" target="_blank">@project.displayName</a></td>
<td>@project.displayName</td>
<td><a href="@project.gitHubUrl" target="_blank">View on GitHub</a></td>
</tr>
}
Expand Down
3 changes: 2 additions & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ github.apiUrl = "https://api.github.com"
opencollective.restApiUrl = "https://opencollective.com/"
opencollective.slug = "playframework"

examples.apiUrl = "https://example.lightbend.com/v1/api/templates"
# apiUrl not used currently, we use local json files in conf/playSamples_x.x.x.json currently
#examples.apiUrl = "https://example.lightbend.com/v1/api/templates"
examples.playVersions = [ "2.8.x" ]
examples.cache.ttl = 1 hour

Expand Down
Loading

0 comments on commit ab98d70

Please sign in to comment.