Skip to content

Instantly share code, notes, and snippets.

name := """test-rjs"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala, SbtWeb)
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
jdbc,
@mikesname
mikesname / gist:7baece8eb46e7f8e4a3c
Created February 11, 2015 14:30
Play Slick question
import play.api.db.slick.DB
def allSamplesJson = Action { implicit request =>
request.session.get("l").map {
case "i" => DB.withSession { implicit dbs =>
val all = models.CodeSampleTable.allSamples()
Ok(Json.toJson(all))
}
case _ => Unauthorized(unautStr)
}.getOrElse {
@mikesname
mikesname / FutureCache.scala
Last active August 29, 2015 14:25
Caching a Future[T]
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag
import play.api.cache.CacheApi
object FutureCache {
def getOrElse[A](key: String, expiration: Duration = Duration.Inf)(f: => Future[A])(implicit cache: CacheApi, ct: ClassTag[A], executionContext: ExecutionContext): Future[A] = {
cache.get[A](key) match {
case Some(a) => Future.successful(a)
case _ =>
@mikesname
mikesname / RateLimitingController.scala
Created August 20, 2015 20:14
An extremely naive (doesn't properly handle concurrent accesses) rate limited action builder for POST requests.
trait RateLimitingController {
this: Controller with ControllerHelpers =>
// Abstract cache member
def cache: play.api.cache.CacheApi
// Function to check if current IP exceeds limit for request path
def checkRateLimit[A](implicit request: Request[A]): Boolean = {
val limit: Int = getConfigInt("ratelimit.limit")
val timeoutSecs: Int = getConfigInt("ratelimit.timeout")
package utils.caching
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag
import play.api.cache.CacheApi
object FutureCache {
def getOrElse[A](key: String, expiration: Duration = Duration.Inf)(orElse : => Future[A])(implicit cache: CacheApi, ct: ClassTag[A], executionContext: ExecutionContext): Future[A] = {
cache.get[A](key) match {
2744470 / 2053936
text/RG-50.030.0001_trs_en.txt Warsaw 0.009380871
text/RG-50.030.0001_trs_en.txt 19 0.002966492
text/RG-50.030.0001_trs_en.txt Varka 0.0020976267
text/RG-50.030.0001_trs_en.txt Gesha 0.0020976267
text/RG-50.030.0001_trs_en.txt Slovakia 0.0020976267
text/RG-50.030.0001_trs_en.txt Amy 0.0020976267
text/RG-50.030.0001_trs_en.txt wasa 0.0020976267
text/RG-50.030.0001_trs_en.txt upa 0.0020976267
text/RG-50.030.0002_trs_en.txt Lodz 0.006228336
# First, log into the EHRI staging server
# Actually open a bunch of shells
# In one of them, tail the following file, which will give us some information
# about what goes wrong when something inevitably goes wrong
tail -f /opt/webapps/neo4j-version/data/log/console.log
# Next, in another shell, copy the file(s) to be ingested to the server
# and place them in /opt/webapps/data/import-data/de/de-002409
# (de-002409 is ITS's EHRI ID.)
package controllers;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.google.inject.Inject;
implicit def longMapReads[T](implicit r: Reads[T]): Reads[Map[Long, T]] = Reads[Map[Long, T]] { jv =>
jv.validate[Map[String, T]].flatMap { obj =>
obj.foldLeft[JsResult[Map[Long,T]]](JsSuccess(Map.empty)) {
case (JsSuccess(o, p), (k, v)) =>
try {
JsSuccess(o + (k.toLong -> v))
} catch {
case e: IllegalArgumentException =>
val e = ValidationError(s"Value: '$k' cannot be converted to a Long")
JsError(p, e)
@mikesname
mikesname / scopecontent.py
Created September 20, 2016 12:40
Fetch scope and content data for the EHRI v1 API
#!/usr/bin/env python
import sys, requests
if len(sys.argv) < 1:
print("usage: scopecontent.py <url>")
sys.exit(1)
def scope_content(url):
r = requests.get(url, headers={"Authorization": "Bearer EHRI-Dev"})