Skip to content

Commit

Permalink
Updating to play3 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegYch authored Dec 9, 2023
1 parent 9b60787 commit bf164c7
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 96 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ jobs:
- uses: actions/checkout@v2.3.4
with:
fetch-depth: 0
- uses: olafurpg/setup-scala@v13
- uses: olafurpg/setup-scala@v14
with:
java-version: adopt@1.11
- uses: coursier/cache-action@v6.3
- run: docker run -d -p 8080:8080 julienrf/endpoints4s-stubserver:1.1.0
- name: Scala 2.12 tests
run: sbt "++ 2.12.15 test"
- name: Scala 3 tests
run: sbt "++ 3.0.2 compile"
- uses: coursier/setup-action@v1
with:
apps: cs
- run: "cs launch org.endpoints4s::stub-server:2.1.0 &"
- name: Scala 2.13 tests
run: sbt "++ 2.13 test"
- name: Scala 3 tests
run: |
sbt "++ 2.13.7 ;coverage; test; coverageReport; coverageAggregate"
sbt "++ 3.3 ;coverage; test; coverageReport; coverageAggregate"
bash <(curl -s https://codecov.io/bash)
- name: Version Policy Check
run: sbt versionPolicyCheck
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ jobs:
- uses: actions/checkout@v2.3.4
with:
fetch-depth: 0
- uses: olafurpg/setup-scala@v13
- uses: olafurpg/setup-scala@v14
with:
java-version: adopt@1.11
- uses: coursier/cache-action@v6.3
- run: sbt versionCheck
- run: sbt ci-release
Expand Down
65 changes: 32 additions & 33 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import xerial.sbt.Sonatype.GitHubHosting
import com.lightbend.paradox.markdown.Writer

val playVersion = "2.8.13"
val akkaActorVersion = "2.6.15"
val circeVersion = "0.14.1"
val playVersion = "3.0.0"
val circeVersion = "0.14.6"
val testkitVersion = "6.0.0"
val pekko = {
val pekkoVersion = "1.0.2"
List(
"org.apache.pekko" %% "pekko-actor" % pekkoVersion,
"org.apache.pekko" %% "pekko-slf4j" % pekkoVersion,
"org.apache.pekko" %% "pekko-serialization-jackson" % pekkoVersion,
"org.apache.pekko" %% "pekko-actor-typed" % pekkoVersion,
)
}

inThisBuild(List(
versionPolicyIntention := Compatibility.BinaryAndSourceCompatible,
versionPolicyIntention := Compatibility.None, //todo revert after release
organization := "org.endpoints4s",
sonatypeProjectHosting := Some(
GitHubHosting("endpoints4s", "play", "julien@richard-foy.fr")
Expand All @@ -18,8 +27,8 @@ inThisBuild(List(
developers := List(
Developer("julienrf", "Julien Richard-Foy", "julien@richard-foy.fr", url("http://julien.richard-foy.fr"))
),
scalaVersion := "2.13.8",
crossScalaVersions := Seq("2.13.8", "3.0.2", "2.12.13"),
scalaVersion := "2.13.12",
crossScalaVersions := Seq(scalaVersion.value, "3.3.1"),
versionPolicyIgnoredInternalDependencyVersions := Some("^\\d+\\.\\d+\\.\\d+\\+\\d+".r)
))

Expand All @@ -28,18 +37,14 @@ val `play-server` =
.in(file("server"))
.settings(
name := "play-server",
publish / skip := scalaVersion.value.startsWith("3"), // Don’t publish Scala 3 artifacts for now because the algebra is not published for Scala 3
libraryDependencies ++= Seq(
("org.endpoints4s" %% "openapi" % "4.1.0").cross(CrossVersion.for3Use2_13),
("com.typesafe.play" %% "play-netty-server" % playVersion).cross(CrossVersion.for3Use2_13),
("org.endpoints4s" %% "algebra-testkit" % "2.0.0" % Test).cross(CrossVersion.for3Use2_13),
("org.endpoints4s" %% "algebra-circe-testkit" % "2.0.0" % Test).cross(CrossVersion.for3Use2_13),
("com.typesafe.play" %% "play-test" % playVersion % Test).cross(CrossVersion.for3Use2_13),
("com.typesafe.play" %% "play-ahc-ws" % playVersion % Test).cross(CrossVersion.for3Use2_13),
// Override transitive dependencies of Play
("com.typesafe.akka" %% "akka-slf4j" % akkaActorVersion % Test).cross(CrossVersion.for3Use2_13),
("com.typesafe.akka" %% "akka-actor-typed" % akkaActorVersion % Test).cross(CrossVersion.for3Use2_13),
("com.typesafe.akka" %% "akka-serialization-jackson" % akkaActorVersion % Test).cross(CrossVersion.for3Use2_13)
libraryDependencies ++= pekko ++ Seq(
"org.endpoints4s" %% "openapi" % "4.4.0",
"org.playframework" %% "play" % playVersion,

"org.playframework" %% "play-netty-server" % playVersion % Test,
"org.endpoints4s" %% "algebra-circe-testkit" % testkitVersion % Test,
"org.playframework" %% "play-test" % playVersion % Test,
"org.playframework" %% "play-ahc-ws" % playVersion % Test,
)
)

Expand All @@ -48,11 +53,10 @@ val `play-server-circe` =
.in(file("server-circe"))
.settings(
name := "play-server-circe",
publish / skip := scalaVersion.value.startsWith("3"), // Don’t publish Scala 3 artifacts for now because the algebra is not published for Scala 3
libraryDependencies ++= Seq(
("io.circe" %% "circe-parser" % circeVersion).cross(CrossVersion.for3Use2_13),
("org.endpoints4s" %% "algebra-circe" % "2.1.0").cross(CrossVersion.for3Use2_13),
("org.endpoints4s" %% "json-schema-circe" % "2.1.0").cross(CrossVersion.for3Use2_13)
"io.circe" %% "circe-parser" % circeVersion,
"org.endpoints4s" %% "algebra-circe" % "2.5.0",
"org.endpoints4s" %% "json-schema-circe" % "2.5.0",
)
)
.dependsOn(`play-server`)
Expand All @@ -62,17 +66,12 @@ val `play-client` =
.in(file("client"))
.settings(
name := "play-client",
publish / skip := scalaVersion.value.startsWith("3"), // Don’t publish Scala 3 artifacts for now because the algebra is not published for Scala 3
libraryDependencies ++= Seq(
("org.endpoints4s" %% "openapi" % "4.1.0").cross(CrossVersion.for3Use2_13),
("com.typesafe.play" %% "play-ahc-ws" % playVersion).cross(CrossVersion.for3Use2_13),
("org.endpoints4s" %% "algebra-testkit" % "2.0.0" % Test).cross(CrossVersion.for3Use2_13),
("org.endpoints4s" %% "algebra-circe-testkit" % "2.0.0" % Test).cross(CrossVersion.for3Use2_13),
("org.endpoints4s" %% "json-schema-generic" % "1.7.0" % Test).cross(CrossVersion.for3Use2_13),
// Override transitive dependencies of Play
("com.typesafe.akka" %% "akka-slf4j" % akkaActorVersion % Test).cross(CrossVersion.for3Use2_13),
("com.typesafe.akka" %% "akka-actor-typed" % akkaActorVersion % Test).cross(CrossVersion.for3Use2_13),
("com.typesafe.akka" %% "akka-serialization-jackson" % akkaActorVersion % Test).cross(CrossVersion.for3Use2_13)
libraryDependencies ++= pekko ++ Seq(
"org.endpoints4s" %% "openapi" % "4.4.0",
"org.playframework" %% "play-ahc-ws" % playVersion,

"org.endpoints4s" %% "algebra-circe-testkit" % testkitVersion % Test,
"org.endpoints4s" %% "json-schema-generic" % "1.11.0" % Test,
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class TestJsonSchemaClient(address: String, wsClient: WSClient)(implicit

class EndpointsJsonSchemaTest extends client.JsonTestSuite[TestJsonSchemaClient] {

val wsClient = new WsTestClient.InternalWSClient("http", stubServerPort)
val wsClient = new WsTestClient.InternalWSClient("http", stubServerPortHTTP)
val client: TestJsonSchemaClient =
new TestJsonSchemaClient(s"http://localhost:$stubServerPort", wsClient)
new TestJsonSchemaClient(s"http://localhost:$stubServerPortHTTP", wsClient)

def call[Req, Resp](
endpoint: client.Endpoint[Req, Resp],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class EndpointsTest
with client.SumTypedEntitiesTestSuite[TestClient]
with client.TextEntitiesTestSuite[TestClient] {

val wsClient = new WsTestClient.InternalWSClient("http", stubServerPort)
val wsClient = new WsTestClient.InternalWSClient("http", stubServerPortHTTP)
val client: TestClient =
new TestClient(s"http://localhost:$stubServerPort", wsClient)
new TestClient(s"http://localhost:$stubServerPortHTTP", wsClient)

def call[Req, Resp](
endpoint: client.Endpoint[Req, Resp],
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.6.1
sbt.version=1.9.7
4 changes: 3 additions & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.10")
addSbtPlugin("ch.epfl.scala" % "sbt-version-policy" % "2.0.1")

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.9.2")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.5")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.4.1")
addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.9.2")
addSbtPlugin("io.github.jonas" % "sbt-paradox-material-theme" % "0.6.0")
addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0")

libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % "always"
4 changes: 2 additions & 2 deletions server/src/main/scala/endpoints4s/play/server/Assets.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package endpoints4s.play.server

import akka.stream.scaladsl.{Source, StreamConverters}
import akka.util.ByteString
import org.apache.pekko.stream.scaladsl.{Source, StreamConverters}
import org.apache.pekko.util.ByteString
import endpoints4s.{Invalid, Valid, Validated, algebra}
import endpoints4s.algebra.Documentation
import play.api.http.{ContentTypes, HttpEntity}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package endpoints4s.play.server
import java.util.Base64
import endpoints4s.algebra.BasicAuthentication.Credentials
import endpoints4s.algebra.Documentation
import endpoints4s.{Tupler, Valid, Validated, algebra}
import endpoints4s.{Invalid, Tupler, Valid, Validated, algebra}
import play.api.http.HeaderNames
import play.api.http.HeaderNames.AUTHORIZATION
import play.api.mvc.{RequestHeader, Results}
Expand Down Expand Up @@ -71,19 +71,29 @@ trait BasicAuthentication extends algebra.BasicAuthentication with EndpointsWith
Valid(tuplerUEHC(tuplerUE(urlData, entityData), tuplerHC(h, credentials)))
}
def matchRequest(requestHeader: RequestHeader): Option[RequestEntity[Out]] = {
matchRequestAndParseHeaders(requestHeader) {
case (_, (_, None /* credentials */ )) =>
requestEntityOf(
Left(
Results.Unauthorized
.withHeaders(HeaderNames.WWW_AUTHENTICATE -> "Basic realm=Realm")
for {
uh <- parseRequestAndHeaders(requestHeader)
credentials = basicAuthenticationHeader(requestHeader.headers)
result = credentials match {
case Valid(Some(credentials)) =>
uh.fold(
{ case (uh, (headersData, _)) =>
requestEntityMap(entity) { entityData =>
tuplerUEHC(tuplerUE(uh, entityData), tuplerHC(headersData, credentials))
}
},
invalid => requestEntityOf(Left(handleClientErrors(Invalid(invalid))))
)
)
case (urlData, (headersData, Some(credentials))) =>
requestEntityMap(entity) { entityData =>
tuplerUEHC(tuplerUE(urlData, entityData), tuplerHC(headersData, credentials))
}
}
case Valid(None) =>
requestEntityOf(
Left(
Results.Unauthorized
.withHeaders(HeaderNames.WWW_AUTHENTICATE -> "Basic realm=Realm")
)
)
case invalid: Invalid => requestEntityOf(Left(handleClientErrors(invalid)))
}
} yield result
}
def urlData(a: Out): U = {
val (ue, hc) = tuplerUEHC.unapply(a)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package endpoints4s.play.server

import akka.NotUsed
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.{Framing => AkkaFraming}
import akka.stream.scaladsl.Source
import akka.util.ByteString
import org.apache.pekko.NotUsed
import org.apache.pekko.stream.scaladsl.Flow
import org.apache.pekko.stream.scaladsl.{Framing => pekkoFraming}
import org.apache.pekko.stream.scaladsl.Source
import org.apache.pekko.util.ByteString
import endpoints4s.algebra
import play.api.http.{ContentTypes, HttpChunk, HttpEntity}
import play.api.libs.streams.Accumulator
Expand Down Expand Up @@ -113,7 +113,7 @@ trait ChunkedJsonEntities

override lazy val newLineDelimiterFraming: Framing = new Framing(
Flow[ByteString].via(
AkkaFraming.delimiter(
pekkoFraming.delimiter(
ByteString("\n"),
maximumFrameLength = Int.MaxValue,
allowTruncation = true
Expand Down
25 changes: 15 additions & 10 deletions server/src/main/scala/endpoints4s/play/server/Endpoints.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,30 @@ trait EndpointsWithCustomErrors
*/
final def encode(a: A): Call = Call(method.value, url.encodeUrl(urlData(a)))

/** @return If the incoming `requestHeader` match this request description (the
* method and URL), validate the request URL parameters and headers.
* Otherwise, returns `None`.
/** @return Parsed and validated request data.
*/
protected final def matchRequestAndParseHeaders(
protected final def parseRequestAndHeaders(
requestHeader: RequestHeader
)(entity: (UrlData, HeadersData) => RequestEntity[A]): Option[RequestEntity[A]] = {
): Option[Validated[(UrlData, HeadersData)]] = {
// Check that the incoming request matches the method and URL of this request description
val maybeValidatedUrlData =
(if (method.matches(requestHeader)) Some(()) else None)
.zip(url.decodeUrl(requestHeader))
.headOption

// If the incoming request matches the method and URL of this request description, parse the headers
val maybeValidatedUrlAndHeadersData: Option[Validated[(UrlData, HeadersData)]] =
maybeValidatedUrlData.map { case (_, validatedUrlData) =>
validatedUrlData.zip(headers(requestHeader.headers))
}
maybeValidatedUrlData.map { case (_, validatedUrlData) =>
validatedUrlData.zip(headers(requestHeader.headers))
}
}

/** @return If the incoming `requestHeader` match this request description (the
* method and URL), validate the request URL parameters and headers.
* Otherwise, returns `None`.
*/
protected final def matchRequestAndParseHeaders(
requestHeader: RequestHeader
)(entity: (UrlData, HeadersData) => RequestEntity[A]): Option[RequestEntity[A]] = {
val maybeValidatedUrlAndHeadersData = parseRequestAndHeaders(requestHeader)
maybeValidatedUrlAndHeadersData.map {
case inv: Invalid => requestEntityOf(Left(handleClientErrors(inv)))
case Valid((urlData, headersData)) => entity(urlData, headersData)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package endpoints4s.play.server

import akka.util.ByteString
import org.apache.pekko.util.ByteString
import endpoints4s.{Codec, Decoder, Encoder, Invalid, algebra}
import play.api.mvc.RequestHeader
import play.api.http.{ContentTypes, Writeable}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package endpoints4s.play.server

import akka.stream.scaladsl.Source
import org.apache.pekko.stream.scaladsl.Source
import endpoints4s.algebra

import scala.concurrent.duration.DurationInt
Expand All @@ -14,7 +14,7 @@ trait ChunkedEntitiesDefinitions extends algebra.ChunkedEntities {

trait ChunkedEntitiesDocs extends ChunkedEntitiesDefinitions with ChunkedEntities {
//#implementation
import akka.stream.scaladsl.FileIO
import org.apache.pekko.stream.scaladsl.FileIO
import java.nio.file.Paths

val logoHandler =
Expand Down
Loading

0 comments on commit bf164c7

Please sign in to comment.