Skip to content

Commit

Permalink
Merge branch 'main' into update-scala-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo committed Dec 21, 2023
2 parents b411846 + f2f5378 commit 6442cb1
Show file tree
Hide file tree
Showing 88 changed files with 9,338 additions and 1,348 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
run: sbt '++ ${{ matrix.scala }}' zioHttpShadedTests/test

- name: Compress target directories
run: tar cf targets.tar zio-http-cli/target target zio-http/target zio-http-docs/target zio-http-benchmarks/target zio-http-example/target zio-http-testkit/target project/target
run: tar cf targets.tar zio-http-cli/target target zio-http/target zio-http-docs/target zio-http-gen/target zio-http-benchmarks/target zio-http-example/target zio-http-testkit/target project/target

- name: Upload target directories
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 3.7.14
version = 3.7.17
maxColumn = 120

align.preset = more
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ZIO HTTP is a scala library for building http apps. It is powered by ZIO and [Ne
Setup via `build.sbt`:

```scala
libraryDependencies += "dev.zio" %% "zio-http" % "3.0.0-RC3"
libraryDependencies += "dev.zio" %% "zio-http" % "3.0.0-RC4"
```

**NOTES ON VERSIONING:**
Expand Down
16 changes: 15 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BuildHelper._
import Dependencies._
import Dependencies.{scalafmt, _}
import sbt.librarymanagement.ScalaArtifacts.isScala3
import scala.concurrent.duration._

Expand Down Expand Up @@ -118,6 +118,7 @@ lazy val aggregatedProjects: Seq[ProjectReference] =
zioHttp,
zioHttpBenchmarks,
zioHttpCli,
zioHttpGen,
zioHttpExample,
zioHttpTestkit,
docs,
Expand Down Expand Up @@ -237,6 +238,19 @@ lazy val zioHttpExample = (project in file("zio-http-example"))
.settings(libraryDependencies ++= Seq(`jwt-core`))
.dependsOn(zioHttp, zioHttpCli)

lazy val zioHttpGen = (project in file("zio-http-gen"))
.settings(stdSettings("zio-http-gen"))
.settings(publishSetting(true))
.settings(
libraryDependencies ++= Seq(
`zio`,
`zio-test`,
`zio-test-sbt`,
scalafmt.cross(CrossVersion.for3Use2_13),
),
)
.dependsOn(zioHttp)

lazy val zioHttpTestkit = (project in file("zio-http-testkit"))
.enablePlugins(Shading.plugins(): _*)
.settings(stdSettings("zio-http-testkit"))
Expand Down
23 changes: 23 additions & 0 deletions docs/examples/advanced/static-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
id: static-files
title: "Serving Static Files"
sidebar_label: "Static Files"
---

```scala mdoc:silent
import zio._
import zio.http._

object StaticFiles extends ZIOAppDefault {

/**
* Creates an HTTP app that only serves static files from resources via
* "/static". For paths other than the resources directory, see
* [[Middleware.serveDirectory]].
*/
val app = Routes.empty.toHttpApp @@ Middleware.serveResources(Path.empty / "static")

override def run = Server.serve(app).provide(Server.default)
}

```
6 changes: 4 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import sbt.Keys.scalaVersion
object Dependencies {
val JwtCoreVersion = "9.1.1"
val NettyVersion = "4.1.101.Final"
val NettyIncubatorVersion = "0.0.20.Final"
val NettyIncubatorVersion = "0.0.24.Final"
val ScalaCompactCollectionVersion = "2.11.0"
val ZioVersion = "2.0.19"
val ZioCliVersion = "0.5.0"
val ZioSchemaVersion = "0.4.15"
val ZioSchemaVersion = "0.4.16"
val SttpVersion = "3.3.18"

val `jwt-core` = "com.github.jwt-scala" %% "jwt-core" % JwtCoreVersion
val `scala-compact-collection` = "org.scala-lang.modules" %% "scala-collection-compat" % ScalaCompactCollectionVersion

val scalafmt = "org.scalameta" %% "scalafmt-dynamic" % "3.7.17"

val netty =
Seq(
"io.netty" % "netty-codec-http" % NettyVersion,
Expand Down
29 changes: 15 additions & 14 deletions zio-http-cli/src/main/scala/zio/http/endpoint/cli/HttpOptions.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zio.http.endpoint.cli

import java.nio.file.Path

import scala.annotation.tailrec
import scala.language.implicitConversions
import scala.util.Try

Expand Down Expand Up @@ -242,8 +241,8 @@ private[cli] object HttpOptions {
self =>

override val name = pathCodec.segments.map {
case SegmentCodec.Literal(value, _) => value
case _ => ""
case SegmentCodec.Literal(value) => value
case _ => ""
}
.filter(_ != "")
.mkString("-")
Expand Down Expand Up @@ -301,7 +300,7 @@ private[cli] object HttpOptions {
Try(java.util.UUID.fromString(str)).toEither.left.map { error =>
ValidationError(
ValidationErrorType.InvalidValue,
HelpDoc.p(HelpDoc.Span.code(error.getMessage())),
HelpDoc.p(HelpDoc.Span.code(error.getMessage)),
)
},
)
Expand All @@ -313,27 +312,29 @@ private[cli] object HttpOptions {
}

private[cli] def optionsFromSegment(segment: SegmentCodec[_]): Options[String] = {
@tailrec
def fromSegment[A](segment: SegmentCodec[A]): Options[String] =
segment match {
case SegmentCodec.UUID(name, doc) =>
case SegmentCodec.UUID(name) =>
Options
.text(name)
.mapOrFail(str =>
Try(java.util.UUID.fromString(str)).toEither.left.map { error =>
ValidationError(
ValidationErrorType.InvalidValue,
HelpDoc.p(HelpDoc.Span.code(error.getMessage())),
HelpDoc.p(HelpDoc.Span.code(error.getMessage)),
)
},
)
.map(_.toString)
case SegmentCodec.Text(name, doc) => Options.text(name)
case SegmentCodec.IntSeg(name, doc) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.LongSeg(name, doc) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.BoolSeg(name, doc) => Options.boolean(name).map(_.toString)
case SegmentCodec.Literal(value, doc) => Options.Empty.map(_ => value)
case SegmentCodec.Trailing(doc) => Options.none.map(_.toString)
case SegmentCodec.Empty(_) => Options.none.map(_.toString)
case SegmentCodec.Text(name) => Options.text(name)
case SegmentCodec.IntSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.LongSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.BoolSeg(name) => Options.boolean(name).map(_.toString)
case SegmentCodec.Literal(value) => Options.Empty.map(_ => value)
case SegmentCodec.Trailing => Options.none.map(_.toString)
case SegmentCodec.Empty => Options.none.map(_.toString)
case SegmentCodec.Annotated(codec, _) => fromSegment(codec)
}

fromSegment(segment)
Expand Down
23 changes: 13 additions & 10 deletions zio-http-cli/src/test/scala/zio/http/endpoint/cli/CommandGen.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zio.http.endpoint.cli

import zio.ZNothing
import scala.annotation.tailrec

import zio.cli._
import zio.test._

Expand All @@ -9,7 +10,6 @@ import zio.schema._
import zio.http._
import zio.http.codec._
import zio.http.endpoint._
import zio.http.endpoint.cli.AuxGen._
import zio.http.endpoint.cli.CliRepr.HelpRepr
import zio.http.endpoint.cli.EndpointGen._

Expand All @@ -20,17 +20,20 @@ import zio.http.endpoint.cli.EndpointGen._
object CommandGen {

def getSegment(segment: SegmentCodec[_]): (String, String) = {
@tailrec
def fromSegment[A](segment: SegmentCodec[A]): (String, String) =
segment match {
case SegmentCodec.UUID(name, doc) => (name, "text")
case SegmentCodec.Text(name, doc) => (name, "text")
case SegmentCodec.IntSeg(name, doc) => (name, "integer")
case SegmentCodec.LongSeg(name, doc) => (name, "integer")
case SegmentCodec.BoolSeg(name, doc) => (name, "boolean")
case SegmentCodec.Literal(value, doc) => ("", "")
case SegmentCodec.Trailing(doc) => ("", "")
case SegmentCodec.Empty(_) => ("", "")
case SegmentCodec.UUID(name) => (name, "text")
case SegmentCodec.Text(name) => (name, "text")
case SegmentCodec.IntSeg(name) => (name, "integer")
case SegmentCodec.LongSeg(name) => (name, "integer")
case SegmentCodec.BoolSeg(name) => (name, "boolean")
case SegmentCodec.Literal(_) => ("", "")
case SegmentCodec.Trailing => ("", "")
case SegmentCodec.Empty => ("", "")
case SegmentCodec.Annotated(codec, _) => fromSegment(codec)
}

fromSegment(segment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package example
import zio._

import zio.http.Header.Authorization
import zio.http._
import zio.http.codec.{HttpCodec, PathCodec}
import zio.http.endpoint.openapi.{OpenAPIGen, SwaggerUI}
import zio.http.endpoint.{Endpoint, EndpointExecutor, EndpointLocator, EndpointMiddleware}
import zio.http.{int => _, _}

object EndpointExamples extends ZIOAppDefault {
import HttpCodec._
import HttpCodec.query
import PathCodec._

val auth = EndpointMiddleware.auth
Expand Down Expand Up @@ -36,7 +37,9 @@ object EndpointExamples extends ZIOAppDefault {
}
}

val routes = Routes(getUserRoute, getUserPostsRoute)
val openAPI = OpenAPIGen.fromEndpoints(title = "Endpoint Example", version = "1.0", getUser, getUserPosts)

val routes = Routes(getUserRoute, getUserPostsRoute) ++ SwaggerUI.routes("docs" / "openapi", openAPI)

val app = routes.toHttpApp // (auth.implement(_ => ZIO.unit)(_ => ZIO.unit))

Expand Down
17 changes: 17 additions & 0 deletions zio-http-example/src/main/scala/example/StaticFiles.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package example

import zio._

import zio.http._

object StaticFiles extends ZIOAppDefault {

/**
* Creates an HTTP app that only serves static files from resources via
* "/static". For paths other than the resources directory, see
* [[Middleware.serveDirectory]].
*/
val app = Routes.empty.toHttpApp @@ Middleware.serveResources(Path.empty / "static")

override def run = Server.serve(app).provide(Server.default)
}
Loading

0 comments on commit 6442cb1

Please sign in to comment.