forked from playframework/playframework.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayExampleProjects.scala
More file actions
126 lines (107 loc) · 3.56 KB
/
Copy pathPlayExampleProjects.scala
File metadata and controls
126 lines (107 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package models
import com.google.inject.AbstractModule
import com.google.inject.Singleton
import org.apache.commons.io.IOUtils
import jakarta.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
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
case class TemplateParameter(
`type`: String,
query: String,
displayName: String,
required: Boolean,
defaultValue: Option[String],
pattern: Option[String],
format: Option[String],
)
object TemplateParameter {
given format: Format[TemplateParameter] = Json.format
}
case class ExampleProject(
displayName: String,
gitHubRepo: String,
gitHubUrl: String,
keywords: Seq[String],
templateName: String,
parameters: Option[Seq[TemplateParameter]],
) {
def hasParams: Boolean = parameters.nonEmpty
def params: Seq[TemplateParameter] = parameters.toSeq.flatten
}
object ExampleProject {
given format: Format[ExampleProject] = Json.format
}
class ExamplesModule extends AbstractModule {
override def configure(): Unit = {
bind(classOf[PlayExampleProjectsService]).asEagerSingleton()
}
}
@Singleton
class PlayExampleProjectsService @Inject() (
configuration: Configuration,
ws: WSClient,
cache: SyncCacheApi,
environment: Environment,
)(using 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")
// NOTE: TTL is really just a safety measure here.
// We should re-deploy when we make major changes to projects
private val examplesCacheTtl =
configuration.getMillis("examples.cache.ttl").milliseconds
private def playQueryString(version: String): Seq[(String, String)] = {
Seq("keyword" -> "play", "keyword" -> version)
}
private def cacheKey(version: String): String = s"example.projects.$version"
private def convertExampleProjects(version: String, json: JsValue): Seq[ExampleProject] = {
Json.fromJson[Seq[ExampleProject]](json) match {
case JsSuccess(allProjects, _) =>
val playProjects = allProjects
if (examplesCacheTtl.length > 0) {
cache.set(cacheKey(version), playProjects, examplesCacheTtl)
}
playProjects
case JsError(errors) =>
logger.error(s"Cannot parse example projects for $version\n$errors")
Seq.empty
}
}
def examples(): Future[Seq[ExampleProject]] = {
Future
.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)
}
}
def cached(): Option[Seq[ExampleProject]] = {
validPlayVersions.foldLeft(Option(Seq.empty[ExampleProject])) { (result, version) =>
result.fold(result) { projects =>
cache.get[Seq[ExampleProject]](cacheKey(version)).map(projects ++ _)
}
}
}
// preload the cache...
examples()
}