FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Rename dynamodb service to redirects · devhttps/frontend@bc6cef2 · GitHub

Commit bc6cef2

Browse files
committed
Rename dynamodb service to redirects
1 parent 76c00ac commit bc6cef2

6 files changed

Lines changed: 35 additions & 27 deletions

File tree

‎admin/app/controllers/RedirectController.scala‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import play.api.mvc.{Action, Controller}
66
import common.Logging
77
import play.api.data._
88
import play.api.data.Forms._
9-
import services.Redirects
9+
import services.RedirectsTemp
1010

1111

1212
case class PageRedirect(from: String, to: String) {
@@ -24,8 +24,8 @@ class RedirectController extends Controller with Logging {
2424
def redirectPost() = Action { implicit request =>
2525

2626
redirectForm.bindFromRequest().get.trim match {
27-
case PageRedirect(from, "") if from.nonEmpty => Redirects.remove(from)
28-
case PageRedirect(from, to) if from.nonEmpty => Redirects.set(from, to)
27+
case PageRedirect(from, "") if from.nonEmpty => RedirectsTemp.remove(from)
28+
case PageRedirect(from, to) if from.nonEmpty => RedirectsTemp.set(from, to)
2929
case _ =>
3030
}
3131

@@ -70,7 +70,7 @@ class RedirectController extends Controller with Logging {
7070
val from = fromAndTo(0).trim
7171
val to = fromAndTo(1).trim
7272
try {
73-
Redirects.set(from, to)
73+
RedirectsTemp.set(from, to)
7474
s"$from -> $to"
7575
} catch {
7676
case e: Exception => s"Error processing $line: ${e.getMessage}"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import conf.Configuration
55
import scala.collection.JavaConversions._
66
import com.amazonaws.services.dynamodbv2.model.AttributeValue
77

8-
object Redirects {
8+
object RedirectsTemp {
99

1010
private lazy val table = if (Configuration.environment.isProd) "redirects" else "redirects-CODE"
1111

‎archive/app/AppLoader.scala‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import play.api.http.{HttpErrorHandler, HttpRequestHandler}
1313
import play.api.mvc.EssentialFilter
1414
import play.api.routing.Router
1515
import play.api._
16-
import play.api.libs.ws.WSClient
17-
import services.{ArchiveMetrics, DynamoDB}
16+
import services.{ArchiveMetrics, Redirects}
1817
import router.Routes
1918

2019
class AppLoader extends FrontendApplicationLoader {
@@ -23,7 +22,7 @@ class AppLoader extends FrontendApplicationLoader {
2322

2423
trait AppComponents extends FrontendComponents {
2524

26-
lazy val dynamoDB = wire[DynamoDB]
25+
lazy val redirects = wire[Redirects]
2726

2827
lazy val healthCheck = wire[HealthCheck]
2928
lazy val archiveController = wire[ArchiveController]

‎archive/app/controllers/ArchiveController.scala‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import campaigns.ShortCampaignCodes
44
import common._
55
import model.Cached.{CacheableResult, WithoutRevalidationResult}
66
import play.api.mvc._
7-
import services.{Archive, Destination, DynamoDB, GoogleBotMetric}
7+
import services.{Redirects, GoogleBotMetric}
88
import java.net.URLDecoder
99
import javax.ws.rs.core.UriBuilder
1010

@@ -13,7 +13,7 @@ import org.apache.http.HttpStatus
1313

1414
import scala.concurrent.Future
1515

16-
class ArchiveController(dynamoDB: DynamoDB) extends Controller with Logging with ExecutionContexts {
16+
class ArchiveController(redirects: Redirects) extends Controller with Logging with ExecutionContexts {
1717

1818
private val R1ArtifactUrl = """www.theguardian.com/(.*)/[0|1]?,[\d]*,(-?\d+),[\d]*(.*)""".r
1919
private val ShortUrl = """^(www\.theguardian\.com/p/[\w\d]+).*$""".r
@@ -92,7 +92,7 @@ class ArchiveController(dynamoDB: DynamoDB) extends Controller with Logging with
9292
}
9393
}
9494

95-
private def destinationFor(path: String): Future[Option[Destination]] = dynamoDB.destinationFor(normalise(path))
95+
private def destinationFor(path: String): Future[Option[Redirects.Destination]] = redirects.destinationFor(normalise(path))
9696

9797
private object Combiner {
9898
def unapply(path: String): Option[String] = {
@@ -142,13 +142,13 @@ class ArchiveController(dynamoDB: DynamoDB) extends Controller with Logging with
142142
}
143143
}
144144

145-
private def lookupPath(path: String) = destinationFor(path).map{ _.flatMap(processLookupDestination(path).lift)}
145+
private def lookupPath(path: String) = destinationFor(path).map{ _.flatMap(processLookupDestination(path).lift) }
146146

147-
def processLookupDestination(path: String) : PartialFunction[Destination, CacheableResult] = {
148-
case services.Redirect(location) if !linksToItself(path, location) =>
147+
def processLookupDestination(path: String) : PartialFunction[Redirects.Destination, CacheableResult] = {
148+
case Redirects.External(location) if !linksToItself(path, location) =>
149149
val locationWithCampaign = retainShortUrlCampaign(path, location)
150150
WithoutRevalidationResult(Redirect(locationWithCampaign, redirectHttpStatus))
151-
case Archive(archivePath) =>
151+
case Redirects.Archive(archivePath) =>
152152
// http://wiki.nginx.org/X-accel
153153
WithoutRevalidationResult(Ok.withHeaders("X-Accel-Redirect" -> s"/s3-archive/$archivePath"))
154154
}

‎archive/test/ArchiveControllerTest.scala‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import play.api.test.Helpers._
66
import org.scalatest.{BeforeAndAfterAll, DoNotDiscover, FlatSpec, Matchers}
77

88
import scala.concurrent.Future
9-
import services.DynamoDB
9+
import services.Redirects
1010

1111
@DoNotDiscover class ArchiveControllerTest
1212
extends FlatSpec
@@ -15,7 +15,7 @@ import services.DynamoDB
1515
with BeforeAndAfterAll
1616
with WithTestWsClient {
1717

18-
lazy val archiveController = new ArchiveController(new DynamoDB(wsClient))
18+
lazy val archiveController = new ArchiveController(new Redirects(wsClient))
1919

2020
it should "return a normalised r1 path" in {
2121
val tests = List(
@@ -203,14 +203,14 @@ import services.DynamoDB
203203
}
204204

205205
it should "redirect short urls with campaign codes and allow for overrides" in {
206-
val shortRedirectWithCMP = services.Redirect("http://www.theguardian.com/p/new?CMP=existing-cmp")
206+
val shortRedirectWithCMP = Redirects.External("http://www.theguardian.com/p/new?CMP=existing-cmp")
207207
val result = archiveController.retainShortUrlCampaign("http://www.theguardian.com/p/old/stw", shortRedirectWithCMP.location)
208208
result should be (shortRedirectWithCMP.location)
209209
}
210210

211211
it should "not perform a redirect loop check on Archive objects" in {
212212
// The archive x-accel goes to s3. So it is irrelevant whether the original path looks like the s3 archive path.
213-
val databaseSaysArchive = services.Archive("http://www.theguardian.com/redirect/path-to-content")
213+
val databaseSaysArchive = Redirects.Archive("http://www.theguardian.com/redirect/path-to-content")
214214
val result = archiveController.processLookupDestination("http://www.theguardian.com/redirect/path-to-content").lift(databaseSaysArchive)
215215
result.map(_.toString).getOrElse("") should include ("""X-Accel-Redirect -> /s3-archive/http://www.theguardian.com/redirect/path-to-content""")
216216
}
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ import com.amazonaws.util.StringInputStream
1111
import scala.concurrent.Future
1212

1313

14-
sealed trait Destination {
15-
def location: String
14+
object Redirects {
15+
sealed trait Destination {
16+
def location: String
17+
}
18+
19+
// External refers to any non-internal redirect - that is, it could be guardian/non-guardian
20+
// address but will be returned in the response Location header along with 3XX status
21+
case class External(location: String) extends Destination
22+
23+
// Archive refers to an internal redirect to an s3 bucket location - that is, it will
24+
// use the X-Accel-Redirect header to instruct nginx to perform the redirect "internally"
25+
case class Archive(location: String) extends Destination
1626
}
17-
case class Redirect(location: String) extends Destination
18-
case class Archive(location: String) extends Destination
1927

20-
class DynamoDB(wsClient: WSClient) extends Logging with ExecutionContexts {
28+
29+
class Redirects(wsClient: WSClient) extends Logging with ExecutionContexts {
2130
private val tableName = if (Configuration.environment.isProd) "redirects" else "redirects-CODE"
2231
private val dynamoDbGet = "DynamoDB_20120810.GetItem"
2332

@@ -26,7 +35,7 @@ class DynamoDB(wsClient: WSClient) extends Logging with ExecutionContexts {
2635

2736
private lazy val credentials = Configuration.aws.credentials
2837

29-
def destinationFor(source: String): Future[Option[Destination]] = {
38+
def destinationFor(source: String): Future[Option[Redirects.Destination]] = {
3039
credentials.map{ credentialsProvider =>
3140
val signer = new AWS4Signer()
3241
def signedHeaders(xAmzTarget: String, bodyContent: String) = {
@@ -56,8 +65,8 @@ class DynamoDB(wsClient: WSClient) extends Logging with ExecutionContexts {
5665
.withHeaders(headers:_*)
5766

5867
asyncRequest.post(bodyContent).map(_.json).map{ json =>
59-
(json \\ "destination").headOption.map(d => Redirect((d \ "S").as[String]))
60-
.orElse((json \\ "archive").headOption.map(a => Archive((a \ "S").as[String])))
68+
(json \\ "destination").headOption.map(d => Redirects.External((d \ "S").as[String]))
69+
.orElse((json \\ "archive").headOption.map(a => Redirects.Archive((a \ "S").as[String])))
6170
}
6271
}.getOrElse(Future.successful(None))
6372
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL