Created
August 21, 2014 16:43
-
-
Save chbrown/5a731b6a1eee0219a1e3 to your computer and use it in GitHub Desktop.
`scala GrasswireUrlShortener.scala` result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:3: error: object apache is not a member of package org | |
| import org.apache.commons.validator.routines.UrlValidator | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:4: error: not found: value spray | |
| import spray.http.StatusCodes | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:5: error: not found: value spray | |
| import spray.routing._ | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:7: error: not found: value scalaz | |
| import scalaz.concurrent._ | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:9: error: not found: type SimpleRoutingApp | |
| object GrasswireUrlShortener extends App with SimpleRoutingApp { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:11: error: not found: value scredis | |
| implicit val redis = scredis.Redis(ConfigFactory.load(), "redis") | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:13: error: not found: value startServer | |
| startServer("0.0.0.0", 8080) { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:14: error: not found: value path | |
| path(Rest) { r => | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:14: error: not found: value Rest | |
| path(Rest) { r => | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:23: error: not found: type RequestContext | |
| def redirectShortUrl(path: String)(implicit redis: scredis.Redis) = (ctx: RequestContext) => { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:23: error: not found: value scredis | |
| def redirectShortUrl(path: String)(implicit redis: scredis.Redis) = (ctx: RequestContext) => { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:24: error: not found: value Future | |
| Future.now { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:15: error: not found: value get | |
| get { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:17: error: not found: value post | |
| } ~ post { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:30: error: not found: type RequestContext | |
| def createShortUrl(path: String)(implicit redis: scredis.Redis) = (ctx: RequestContext) => { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:30: error: not found: value scredis | |
| def createShortUrl(path: String)(implicit redis: scredis.Redis) = (ctx: RequestContext) => { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:31: error: not found: value Task | |
| Task { | |
| ^ | |
| /Users/chbrown/Desktop/GrasswireUrlShortener.scala:32: error: not found: type UrlValidator | |
| val validator = new UrlValidator(List("http","https").toArray) | |
| ^ | |
| 18 errors found |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import akka.actor.ActorSystem | |
| import com.typesafe.config.ConfigFactory | |
| import org.apache.commons.validator.routines.UrlValidator | |
| import spray.http.StatusCodes | |
| import spray.routing._ | |
| import scala.util.Random | |
| import scalaz.concurrent._ | |
| object GrasswireUrlShortener extends App with SimpleRoutingApp { | |
| implicit val system = ActorSystem("grasswire-url-shortener") | |
| implicit val redis = scredis.Redis(ConfigFactory.load(), "redis") | |
| startServer("0.0.0.0", 8080) { | |
| path(Rest) { r => | |
| get { | |
| redirectShortUrl(r) | |
| } ~ post { | |
| createShortUrl(r) | |
| } | |
| } | |
| } | |
| def redirectShortUrl(path: String)(implicit redis: scredis.Redis) = (ctx: RequestContext) => { | |
| Future.now { | |
| redis.withClient(_.get[String](path)) | |
| }.runAsync(_.map(ctx.redirect(_, StatusCodes.MovedPermanently)) | |
| .getOrElse(ctx.complete(StatusCodes.NotFound))) | |
| } | |
| def createShortUrl(path: String)(implicit redis: scredis.Redis) = (ctx: RequestContext) => { | |
| Task { | |
| val validator = new UrlValidator(List("http","https").toArray) | |
| if(validator.isValid(path)) { | |
| val random = Random.alphanumeric.take(7).mkString | |
| redis.withClient(_.set(random, path)) | |
| random | |
| } else { | |
| throw new Exception("The supplied url is invalid.") | |
| } | |
| }.runAsync(_.fold(l => ctx.reject(ValidationRejection("Invalid url provided", Some(l))), r => ctx.complete(s"http://mydomain.com/$r"))) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment