Skip to content

Instantly share code, notes, and snippets.

@pierangeloc
pierangeloc / sttp_zio_logging.scala
Created August 1, 2023 12:27
Sttp with zio and logging backend
//test it with `runMain com.thenewmotion.myr.TestSttpLogging2 http://httpstat.us/400` from within SBT
object TestSttpLogging2 extends ZIOAppDefault {
override val bootstrap: ZLayer[ZIOAppArgs, Any, Environment] =
Runtime.removeDefaultLoggers >>> SLF4J.slf4j
import sttp.client3.quick._
// curl --request GET \
// --url httpstat.us/200 \
@pierangeloc
pierangeloc / build-targets.sc
Created June 13, 2022 13:37
Ammonite script to `tar` all the target files. Useful for caching compilation results in multi stage pipelines. Built for Azure devops, it can be easily modified to cope with other CI platforms
val currentDir= os.pwd
println(os.list(currentDir).toList.map(p => (os.isDir(p), p.relativeTo(currentDir))).mkString("\n"))
def allSubDirectoriesNamed(name: String, p: os.Path): List[os.Path] = {
if (os.isDir(p) && p.last == name) List(p)
else if (os.isDir(p)) {
for {
child <- os.list(p).toList
@pierangeloc
pierangeloc / docker-notes.md
Last active June 2, 2022 22:13
Notes on docker compose and healthchecks

On docker-compose and healthchecks

I'm writing these notes because I spent a significant amount of time on a situation related with how docker and docker-compose work, specifically in integration tests. I've learned a few things about how docker that might be useful to remember at the next occasion.

If you use docker-compose for your integration tests, you might end up in a situation where you have erratic tests because, while tests are run, some containers are not yet doing what they are supposed to do.

For example, you might have a docker-compose.yml that sets up kafka, zookeeper and postgres and then you want to run your application that connects to these systems. Likely your build pipeline will follow these steps:

@pierangeloc
pierangeloc / zioFromScratch.scala
Created October 1, 2021 21:53
ZIO from scratch / Part1
import MyZIO.{Test, ZIO}
import scala.concurrent.Future
import scala.util.{Failure, Success}
/**
* Following this talk https://www.youtube.com/watch?v=wsTIcHxJMeQ
*/
object MyZIO {
@pierangeloc
pierangeloc / AkkaHttpTimeout.scala
Created July 27, 2020 14:49
A small app to test how akka default timeout vs `withRequestTimeout` works
package api
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpRequest, HttpResponse, StatusCodes}
import akka.stream.ActorMaterializer
import scala.concurrent.Future
import scala.concurrent.duration._
import akka.http.scaladsl.server.Directives._
@pierangeloc
pierangeloc / rekentuin.scala
Created March 8, 2020 13:40
Simple app for sommen/keersommen. In Dutch
import zio.console.Console
import zio.{App, ZIO}
import zio.{console, random}
object Main extends App {
override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, Int] =
prg.forever
val prg = for {
_ <- console.putStrLn("Hier zijn twee nummers, jij moet de som geven")
@pierangeloc
pierangeloc / doobie_newtype.scala
Last active November 23, 2021 13:25
Doobie instances for NewType
import io.estatico.newtype.ops._
import io.estatico.newtype.macros.newtype
import eu.timepit.refined.api.Refined
import doobie.implicits._
import doobie.postgres.implicits._
import doobie.refined.implicits._
/**
* Derive a Put (so it can be interpolated in doobie sql expressions) for any newtype backed by a type that is
* supported by doobie (e.g. any wrapper of String, UUID, Boolean etc)
@pierangeloc
pierangeloc / FutureTraverse.scala
Last active June 12, 2019 21:32
Don't use scala Future
trait TimedExecution {
implicit val materializer: Materializer
def delayed[A, B](f: A => B)(delay: FiniteDuration): A => Future[B] = a => Source.tick(delay, delay, a).take(1).map(f).runWith(Sink.head)
def delayedValue[A](a: => A)(delay: FiniteDuration): Future[A] = delayed[Unit, A](_ => a)(delay)(())
}
@pierangeloc
pierangeloc / java-resources-access.md
Last active January 6, 2019 23:33
On resources access from packaged jvm apps
def loadResources(onBehalfOf: Class[_])(path: String): List[Path] = {
    val url: URL = onBehalfOf.getClassLoader.getResource(path)
    if (url.toURI.getScheme.contains("jar")) {
      val jar: URL = onBehalfOf.getProtectionDomain.getCodeSource.getLocation
      val jarFile: Path = Paths.get(jar.toString.substring("file:".length))
      val fs: FileSystem = FileSystems.newFileSystem(jarFile, null)
      val directoryStream: DirectoryStream[Path] = Files.newDirectoryStream(fs.getPath(path))
      directoryStream.asScala.toList
 } else {
@pierangeloc
pierangeloc / Kafka-cmd.md
Last active March 11, 2019 16:42
Useful Kafka console commands

Kafka 101

We use the reference topic named test, and the consumer group piero-group. Zookeeper runs on localhost:9092, Kafka bootstrap server is localhost:2181

  • Create a topic named test with 4 partitions, without replication

    ~/tools/kafka_2.11-1.1.0/bin/kafka-topics.sh --create --zookeeper localhost:2181 --partitions 4 --replication-factor 1 --topic test4
    
  • Produce String messages from console and post them on topic