Skip to content

Instantly share code, notes, and snippets.

View johanandren's full-sized avatar
👻
hakking

Johan Andrén johanandren

👻
hakking
View GitHub Profile
@johanandren
johanandren / addressbook.js
Created April 10, 2013 12:51
Min måttligt ambitiösa adressbok för mejslas JavaScript-cirkel 2013
// redefine read so that it returns strings
read = (function() {
var oldRead = read
return function(prompt) {
return oldRead(prompt) + "";
}
})();
// show a menu based on a input list of menu items
// menuItems: [{ key: 'K', action: function, desc: 'Description' }, ...]
@johanandren
johanandren / gist:5361674
Created April 11, 2013 08:20
A version of Tomasz Nurkiewicz (http://nurkiewicz.blogspot.se/2013/04/watchservice-combined-with-akka-actors.html) file system watch actor without using an extra thread and instead poll the watch service from the actor at regular intervals.
package markatta
import java.nio.file._
import java.nio.file.StandardWatchEventKinds._
import java.nio.file.attribute.BasicFileAttributes
import collection.JavaConversions._
import concurrent.duration._
import akka.actor.{Cancellable, ActorRef, Actor}
import markatta.FileSystemWatchActor._
@johanandren
johanandren / gist:6547569
Created September 13, 2013 07:14
Different handling for diferent kinds of images
trait ImageResource {
def publicUrl: String
}
case class URLImage(source: URL) extends ImageResource {
def publicUrl = source.url
}
case class CDNImage(bucketName: String, key: String, cdnBaseUrl: String) extends ImageResource {
lazy val cdnUrl = URL(cdnBaseUrl) :+ key
### Keybase proof
I hereby claim:
* I am johanandren on github.
* I am johanandren (https://keybase.io/johanandren) on keybase.
* I have a public key whose fingerprint is 06BA 5A56 D804 4A25 E78C 76A1 14C5 61AB C73E 8B6F
To claim this, I am signing this object:
@johanandren
johanandren / numbers-as-a-service.scala
Last active April 17, 2020 12:17
Sample streaming response with akka http
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshalling.{Marshaller, ToResponseMarshaller}
import akka.http.scaladsl.model.HttpEntity.ChunkStreamPart
import akka.http.scaladsl.model.{HttpEntity, HttpResponse, MediaTypes}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import akka.actor.{ActorSystem, ActorRef}
import akka.pattern.ask
import akka.pattern.AskTimeoutException
import akka.util.Timeout
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.io.StdIn
object AskWithRetry extends App {
import akka.actor.Actor.Receive
import akka.actor._
import akka.cluster.Cluster
import akka.remote.RemoteScope
import com.typesafe.config.ConfigFactory
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.io.StdIn
@johanandren
johanandren / ClusteredRoundRobinPool.scala
Created September 9, 2015 14:30
Simple sample with clustered round robin pool
import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.cluster.Cluster
import akka.routing.FromConfig
import com.typesafe.config.ConfigFactory
import scala.concurrent.{Await, Future}
import scala.io.StdIn
object ClusteredRoundRobinPool extends App {
import akka.actor.ActorSystem
import akka.http.scaladsl._
import akka.http.scaladsl.model.{HttpMethods, Uri, HttpRequest}
import akka.stream.ActorMaterializer
import scala.concurrent.duration._
import scala.concurrent.Await
object Bug18797 {
def main(args: Array[String]): Unit = {
@johanandren
johanandren / SimpleFileUpload.scala
Created December 2, 2015 17:29
Example of accepting a file via post
import java.io.File
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Sink, Source}
import scala.io.StdIn