Created
May 24, 2016 22:05
-
-
Save seamusv/64c731f82c894a05e34c0274867bf88a to your computer and use it in GitHub Desktop.
This file contains 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
package http | |
import java.util.UUID | |
import akka.actor.ActorSystem | |
import akka.event.Logging | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.model._ | |
import akka.http.scaladsl.server._ | |
import Directives._ | |
import StatusCodes._ | |
import akka.stream.ActorMaterializer | |
import com.typesafe.config.ConfigFactory | |
import scala.collection.mutable | |
object WebServer extends App { | |
implicit val system = ActorSystem("web-server") | |
implicit val executor = system.dispatcher | |
implicit val materializer = ActorMaterializer() | |
val requestMap = mutable.HashMap[UUID, String]() | |
val route = | |
logRequestResult("web-server") { | |
pathPrefix("api") { | |
path("generate" / LongNumber) { attachmentId => | |
get { | |
val id = UUID.randomUUID() | |
requestMap += (id -> s"This is a valid request for attachment ID $attachmentId") | |
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, s"""{"url":"http://localhost:9000/download/${id.toString}"}""")) | |
} | |
} | |
} ~ | |
pathPrefix("download" / JavaUUID) { requestId => | |
pathEnd { | |
requestMap.get(requestId) match { | |
case Some(attachment) => | |
requestMap -= requestId | |
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, attachment)) | |
case None => | |
complete(HttpResponse(BadRequest, entity = "Invalid download")) | |
} | |
} | |
} | |
} | |
val config = ConfigFactory.load() | |
val logger = Logging(system, getClass) | |
val bindings = Http() | |
.bindAndHandle(route, config.getString("http.interface"), config.getInt("http.port")) | |
println("AWS Server") | |
println(s"=-=[ Listening on ${config.getString("http.interface")}:${config.getInt("http.port")} ]=-=") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment