Skip to content

Instantly share code, notes, and snippets.

View samuelorji's full-sized avatar

Samuel Orji samuelorji

View GitHub Profile
val route = path("download") {
get {
optionalHeaderValueByName("Range") {
case None =>
// there must always be range
complete(StatusCodes.RequestedRangeNotSatisfiable)
case Some(range) =>
val file = new File("movie.mp4")
val fileSize = file.length()
val rng = range.split("=")(1).split("-")
@samuelorji
samuelorji / streamcorrect.scala
Last active May 3, 2020 15:42
Stream correct
val route = path("download") {
get {
optionalHeaderValueByName("Range") {
case None =>
// there must always be range header
complete(StatusCodes.RequestedRangeNotSatisfiable)
case Some(range) =>
val file = new File("movie.mp4")
val fileSize = file.length()
@samuelorji
samuelorji / stream_no_skip.scala
Last active May 2, 2020 18:08
Streaming Movies
val route = get {
path("download") {
val file = new File("movie.mp4")
respondWithHeaders(RawHeader("Content-Disposition", s"""attachment; filename="movie.mp4"""")) {
complete(HttpEntity(ContentTypes.`application/octet-stream`, FileIO.fromPath(file.toPath)))
}
}
}
trait MarshallingSupport
extends SprayJsonSupport
with DefaultJsonProtocol with Runtime[Unit] { self =>
//implicit marshallers for my return Type A
implicit val todoItemFormatter = jsonFormat2(TodoItem)
implicit val todoNameFormatter = jsonFormat1(TodoName)
sealed trait ErrorToHttpResponse[E] {
def toHttpResponse(value: E): HttpResponse
sealed trait DatabaseDriver {
// //A lot of database config we don't care about
// //Database object
lazy val db = new ConnectionPool(_, _)
}
trait PostgresDatabase extends Database with DatabaseDriver {
private def runQuery(query: String): IO[Throwable, QueryResult] = {
for {
result <- ZIO.fromFuture(_ => db.sendPreparedStatement(query))
sealed trait DatabaseDriver {
//A lot of database config we don't care about
//Database object
private val pool = new ConnectionPool(_, _)
//create an effect from the pool
lazy val db: UIO[ConnectionPool[MySQLConnection]] = UIO.fromFunction(_ => pool)
}
trait PostgresDatabase extends Database with DatabaseDriver { self =>
private def runQuery(query: String): ZIO[DatabaseDriver, Throwable, QueryResult] = {
@samuelorji
samuelorji / build.sbt
Last active December 22, 2019 23:31
build with config file
lazy val fetchConfFile = taskKey[File]("fetch Config file")
fetchConfFile := {
//simulate fetching a remote file
sLog.value.info("fetching file ...... ")
Thread.sleep(1000)
new java.io.File("/tmp/application.conf").createNewFile()
val configFile : File = new java.io.File("/tmp/application.conf")
sLog.value.info("done fetching file .... ")
configFile
@samuelorji
samuelorji / build.sbt
Last active December 23, 2019 13:57
zip task
lazy val zip = taskKey[Unit]("zip files") //define the key
zip := { // assigning a value to the zip key of type Unit
val logger: Logger = sLog.value //get logger from SBT
//where to zip from
val dirFromZip: File = srcFile.value
//where to zip to
val dirToZip: File = baseDirectory.value / "build.sbt.zip"
logger.info(s"zipping files from ${dirFromZip.absolutePath}")
@samuelorji
samuelorji / build.sbt
Created December 18, 2019 10:55
build.sbt with java options
name := "phony"
version := "0.2"
scalaVersion := "2.12.6"
organization := "com.phony"
libraryDependencies += "com.googlecode.libphonenumber" % "libphonenumber" % "8.10.23"
scalacOptions := Seq( // commands passed to the scala compiler
"-feature",
"-unchecked",
"-deprecation",
@samuelorji
samuelorji / build.sbt
Created December 11, 2019 13:36
build.sbt with bumped version
name := "phony"
version := "0.2"
scalaVersion := "2.12.6"
organization := "com.phony"
libraryDependencies += "com.googlecode.libphonenumber" % "libphonenumber" % "8.10.23"