Skip to content

Commit

Permalink
update java to version 21
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmius committed Feb 9, 2024
1 parent 88311bc commit 102f673
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 92 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ jobs:
with:
enable-stack: true
stack-no-global: true
- uses: actions/setup-java@v2
with:
distribution: "zulu"
java-version: "21"
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: bash .github/workflows/setup.sh
id: setup
shell: bash
Expand All @@ -38,9 +45,6 @@ jobs:
path: ${{ steps.setup.outputs.STACK_ROOT }}
- run: bash .github/workflows/build.sh
shell: bash
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: bash .github/workflows/test.sh
shell: bash
- uses: actions/upload-artifact@v4
Expand Down
52 changes: 26 additions & 26 deletions examples/lang/scala/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,42 @@ plugins {
id "scala"
}

def scalaVersion = "2.12"
def http4sVersion = "0.20.6"
def log4CatsVersion = "0.3.0"
def logbackVersion = "1.2.3"
def scalaVersion = "2.13"
def http4sVersion = "0.23.25"
def http4sBlazeVersion = "0.23.16"
def log4CatsVersion = "2.6.0"
def logbackVersion = "1.4.14"

repositories {
jcenter()
mavenCentral()
}

configurations {
scalaCompilerPlugin
}

dependencies {
implementation "org.scala-lang:scala-library:${scalaVersion}.8"
implementation "org.scala-lang:scala-library:${scalaVersion}.12"

implementation "org.typelevel:cats-core_${scalaVersion}:1.6.1"
implementation "org.typelevel:cats-effect_${scalaVersion}:1.3.1"
implementation "org.typelevel:cats-core_${scalaVersion}:2.10.0"
implementation "org.typelevel:cats-effect_${scalaVersion}:3.5.3"

implementation "io.circe:circe-core_${scalaVersion}:0.11.1"
implementation "io.circe:circe-core_${scalaVersion}:0.14.6"

implementation "org.http4s:http4s-core_${scalaVersion}:${http4sVersion}"
implementation "org.http4s:http4s-client_${scalaVersion}:${http4sVersion}"
implementation "org.http4s:http4s-server_${scalaVersion}:${http4sVersion}"
implementation "org.http4s:http4s-circe_${scalaVersion}:${http4sVersion}"
implementation "org.http4s:http4s-blaze-client_${scalaVersion}:${http4sVersion}"
implementation "org.http4s:http4s-blaze-server_${scalaVersion}:${http4sVersion}"
implementation "org.http4s:http4s-blaze-client_${scalaVersion}:${http4sBlazeVersion}"
implementation "org.http4s:http4s-blaze-server_${scalaVersion}:${http4sBlazeVersion}"

implementation "io.chrisdavenport:log4cats-core_${scalaVersion}:${log4CatsVersion}"
implementation "io.chrisdavenport:log4cats-slf4j_${scalaVersion}:${log4CatsVersion}"
implementation "org.typelevel:log4cats-core_${scalaVersion}:${log4CatsVersion}"
implementation "org.typelevel:log4cats-slf4j_${scalaVersion}:${log4CatsVersion}"

implementation "ch.qos.logback:logback-core:${logbackVersion}"
implementation "ch.qos.logback:logback-classic:${logbackVersion}"

scalaCompilerPlugin "org.typelevel:kind-projector_${scalaVersion}:0.10.3"
scalaCompilerPlugin "org.typelevel:kind-projector_2.13.12:0.13.2"
}

sourceSets {
Expand All @@ -47,25 +48,24 @@ sourceSets {
}
}

compileScala {
scalaCompileOptions.deprecation = true
scalaCompileOptions.additionalParameters = [
"-Ypartial-unification",
"-feature",
"-Xplugin:" + configurations.scalaCompilerPlugin.asPath]
}
compileScala {
scalaCompileOptions.deprecation = true
scalaCompileOptions.additionalParameters = [
"-feature",
"-Xplugin:" + configurations.scalaCompilerPlugin.asPath]
}


task runJsonHttpServer(type: JavaExec) {
main = "guguguexamples.jsonhttp.server.JsonHttpServerMain"
tasks.register('runJsonHttpServer', JavaExec) {
mainClass = "guguguexamples.jsonhttp.server.JsonHttpServerMain"
}

task runJsonHttpClient(type: JavaExec) {
main = "guguguexamples.jsonhttp.client.JsonHttpClientMain"
tasks.register('runJsonHttpClient', JavaExec) {
mainClass = "guguguexamples.jsonhttp.client.JsonHttpClientMain"
}


tasks.withType(JavaExec) {
tasks.withType(JavaExec).configureEach {
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package guguguexamples.jsonhttp.client
import cats.effect.IO
import gugugu.lang.scala.runtime.transport._
import guguguexamples.jsonhttp._
import io.chrisdavenport.log4cats.Logger
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import io.circe.Json
import org.http4s.circe._
import org.http4s.client.Client
Expand All @@ -20,27 +20,24 @@ case class HttpClientTransport(http: Client[IO], prefix: Uri)
, encodeA: A => Json, decodeB: Json => B
): IO[WithMeta[B]] = {
val (reqMeta, a) = fa
http.fetch {
for {
r <- IO.delay(encodeA(a))
_ <- logger.info(s"Sending: $r")
} yield {
IO.delay(encodeA(a)).flatMap { r =>
logger.info(s"Sending: $r") *>
http.run {
val hs = metaToHeaders(reqMeta)
val uri = (name.namespace :+ name.name).foldLeft(prefix)(_ / _)
Request[IO](Method.POST, uri)
.withEntity(r)
.putHeaders(hs : _*)
}
} { res =>
for {
r <- res.as[Json]
_ <- logger.info(s"Got data: $r")
b <- IO.delay(decodeB(r))
} yield {
val resMeta = metaFromHeaders(res.headers)
(resMeta, b)
.putHeaders(hs.toSeq)
}.use { res =>
for {
r <- res.as[Json]
_ <- logger.info(s"Got data: $r")
b <- IO.delay(decodeB(r))
} yield {
val resMeta = metaFromHeaders(res.headers)
(resMeta, b)
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package guguguexamples.jsonhttp.client

import java.time.LocalDateTime

import cats.effect.{ContextShift, IO, Resource, Timer}
import cats.effect.unsafe.IORuntime
import cats.effect.{IO, Resource}
import cats.implicits._
import guguguexamples.codec.JsonCodecImpl
import guguguexamples.definitions.hello._
import guguguexamples.definitions.hellotypes._
import guguguexamples.jsonhttp._
import guguguexamples.utils.EnvConfig
import io.chrisdavenport.log4cats.Logger
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger
import org.http4s._
import org.http4s.blaze.client.BlazeClientBuilder
import org.http4s.client.Client
import org.http4s.client.blaze.BlazeClientBuilder
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger

import scala.concurrent.ExecutionContext
import java.time.LocalDateTime

object JsonHttpClientMain {

def main(args: Array[String]): Unit = {
implicit val ioRuntime: IORuntime = IORuntime.builder().build()
run.unsafeRunSync()
}

Expand All @@ -43,15 +43,13 @@ object JsonHttpClientMain {
for {
resWithMeta <- k(reqWithMeta)
(resMeta, res) = resWithMeta
_ <- resMeta.toStream.traverse_ { case (k, v) =>
_ <- resMeta.toList.traverse_ { case (k, v) =>
logger.info(s"Metadata: $k = $v")
}
_ <- logger.info(s"Got response: $res")
} yield ()
}

private implicit val contextShiftIO: ContextShift[IO] = IO.contextShift(ExecutionContext.global)
private implicit val timerIO: Timer[IO] = IO.timer(ExecutionContext.global)
private val logger: Logger[IO] = Slf4jLogger.getLogger

def helloModuleResource: Resource[IO, HelloModule[WithMeta, WithMeta, IO]] = {
Expand All @@ -69,7 +67,7 @@ object JsonHttpClientMain {
}

def httpClientResource: Resource[IO, Client[IO]] = {
BlazeClientBuilder[IO](ExecutionContext.global).resource
BlazeClientBuilder[IO].resource
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package guguguexamples

import org.http4s.{Header, Headers}

import scala.language.higherKinds
import org.typelevel.ci.CIString

package object jsonhttp {

type WithMeta[A] = (Map[String, String], A)

def metaFromHeaders(headers: Headers): Map[String, String] = {
headers.foldLeft(Map.empty[String, String]) { (map, h) =>
map + ((h.name.value, h.value))
headers.headers.foldLeft(Map.empty[String, String]) { (map, h) =>
map + ((h.name.toString, h.value))
}
}

def metaToHeaders(metadata: Map[String, String]): Vector[Header] = {
def metaToHeaders(metadata: Map[String, String]): Vector[Header.Raw] = {
metadata.map { case (k, v) =>
Header(k, v)
Header.Raw(CIString(k), v)
}.toVector
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import cats.effect.IO
import gugugu.lang.scala.runtime.transport._
import guguguexamples.jsonhttp._
import guguguexamples.utils.ContT
import io.chrisdavenport.log4cats.Logger
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import io.circe.Json
import org.http4s._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package guguguexamples.jsonhttp.server

import java.net.InetSocketAddress

import cats.data.{Kleisli, NonEmptyVector}
import cats.effect.{ContextShift, IO, Resource, Timer}
import cats.effect.unsafe.IORuntime
import cats.effect.{IO, Resource}
import gugugu.lang.scala.runtime.transport._
import guguguexamples.codec.JsonCodecImpl
import guguguexamples.definitions.hello._
import guguguexamples.jsonhttp._
import guguguexamples.jsonhttp.server.impl._
import guguguexamples.utils._
import io.chrisdavenport.log4cats.Logger
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger
import io.circe.Json
import org.http4s._
import org.http4s.circe._
import org.http4s.server.Server
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.blaze.server.BlazeServerBuilder
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger

import scala.concurrent.ExecutionContext
import scala.language.higherKinds
import java.net.InetSocketAddress

object JsonHttpServerMain {

def main(args: Array[String]): Unit = {
implicit val ioRuntime: IORuntime = IORuntime.builder().build()
run.unsafeRunSync()
}

Expand All @@ -37,11 +36,9 @@ object JsonHttpServerMain {
}
}

private implicit val contextShiftIO: ContextShift[IO] = IO.contextShift(ExecutionContext.global)
private implicit val timerIO: Timer[IO] = IO.timer(ExecutionContext.global)
private val logger: Logger[IO] = Slf4jLogger.getLogger

def serverResource: Resource[IO, Server[IO]] = {
def serverResource: Resource[IO, Server] = {
val addr = InetSocketAddress.createUnresolved(
EnvConfig.host, EnvConfig.port)
BlazeServerBuilder[IO]
Expand All @@ -64,7 +61,7 @@ object JsonHttpServerMain {
def handleRequest(req: Request[IO]): HandlerF[Response[IO]] = {
(for {
ps <- NonEmptyVector.fromVector {
req.pathInfo.stripPrefix("/").split('/').toVector
req.pathInfo.toString().stripPrefix("/").split('/').toVector
}
qualName = QualName(ps.init, ps.last)
k <- transport.ask(qualName, ContCodecHandler)
Expand All @@ -87,7 +84,7 @@ object JsonHttpServerMain {
val hs = metaToHeaders(resMeta)
Response[IO]()
.withEntity(resR)
.putHeaders(hs: _*)
.putHeaders(hs.toSeq)
}
}).getOrElse(ContT.pure(Response[IO](Status.NotFound)))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package guguguexamples.jsonhttp.server.impl

import java.time.LocalDateTime

import cats.effect.{IO, Timer}
import cats.effect.{IO, Clock}
import cats.implicits._
import guguguexamples.definitions.hello._
import guguguexamples.definitions.hellotypes._
import guguguexamples.jsonhttp.WithMeta
import guguguexamples.jsonhttp.server.HandlerF
import guguguexamples.utils.ContT
import io.chrisdavenport.log4cats.Logger
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger

import scala.concurrent.duration.MILLISECONDS
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger

class HelloImpl(implicit timer: Timer[IO])
class HelloImpl(implicit clock: Clock[IO])
extends HelloModule[WithMeta, WithMeta, HandlerF] {

private val logger: Logger[IO] = Slf4jLogger.getLogger
Expand Down Expand Up @@ -59,13 +56,13 @@ class HelloImpl(implicit timer: Timer[IO])
(k: A => HandlerF[B]): HandlerF[WithMeta[B]] = {
val (metadata, a) = fa
for {
begin <- ContT.lift(timer.clock.realTime(MILLISECONDS))
_ <- metadata.toStream.traverse_[HandlerF, Unit] { case (k, v) =>
begin <- ContT.lift(clock.realTime)
_ <- metadata.toList.traverse_[HandlerF, Unit] { case (k, v) =>
ContT.lift(logger.info(s"Got Metadata: $k = $v"))
}
b <- k(a)
end <- ContT.lift(timer.clock.realTime(MILLISECONDS))
} yield (Map("X-Process-Time" -> s"${end - begin}ms"), b)
end <- ContT.lift(clock.realTime)
} yield (Map("X-Process-Time" -> s"${(end - begin).toMillis}ms"), b)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package guguguexamples.utils

import cats.{Monad, StackSafeMonad}

import scala.language.higherKinds

case class ContT[R, M[_], A](run: (A => M[R]) => M[R]) {

def map[B](f: A => B): ContT[R, M, B] = {
Expand Down

0 comments on commit 102f673

Please sign in to comment.