Skip to content

Instantly share code, notes, and snippets.

@markusjura
Created March 17, 2014 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusjura/9596777 to your computer and use it in GitHub Desktop.
Save markusjura/9596777 to your computer and use it in GitHub Desktop.
package controllers
import play.api._
import play.api.mvc._
import models.Files
import play.api.libs.json.Json
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits._
object FileController extends SecuredController {
def countChars = Action.async {
Future {
val chars = Files.countChars("public/files/chars.txt")
Ok(Json.obj("chars" -> chars))
}
}
def countCharsBlocking = Action {
val chars = Files.countChars("public/files/chars.txt")
Ok(Json.obj("chars" -> chars))
}
}
package models
import scala.io.Source
object Files {
def countChars(filePath: String): Long = {
val source = Source.fromFile(filePath)
var chars = 0L
for (line <- source.getLines) {
chars += line.toCharArray.size
}
source.close()
chars
}
}
GET /count controllers.FileController.countChars()
GET /countBlocking controllers.FileController.countCharsBlocking()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment