Skip to content

Commit 321ecf4

Browse files
committed
#84: optionally enable CORS
1 parent 6abd01e commit 321ecf4

File tree

6 files changed

+53
-7
lines changed

6 files changed

+53
-7
lines changed

scalajvm/app/controllers/api/Quotes.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
package controllers.api
22

3-
import helpers.ActionWithTx
3+
import helpers.{ActionWithTx, Cors, RequestWithSession}
44
import models.queries.{QuoteFilter, QuoteOrdering, QuoteQueries}
55
import models.data.Quote
66
import play.api.mvc._
77
import ru.org.codingteam.loglist.dto.QuoteDTO
88
import ru.org.codingteam.loglist.QuoteCount
99

1010
object Quotes extends Controller {
11-
def getQuote(id: Long) = ActionWithTx { request =>
11+
private def action[AnyContent](responseAction: RequestWithSession[AnyContent] => Result) = ActionWithTx { request =>
12+
val response = responseAction(request)
13+
val headers = Cors.headers(request.headers)
14+
response.withHeaders(headers: _*)
15+
}
16+
17+
def getQuote(id: Long) = action { request =>
1218
import request.dbSession
1319
prepareResponse(QuoteQueries().getQuoteById(id))
1420
}
1521

16-
def getRandomQuote = ActionWithTx { request =>
22+
def getRandomQuote = action { request =>
1723
import request.dbSession
1824
prepareResponse(QuoteQueries().getRandomQuote)
1925
}
2026

21-
def getCount(filter: QuoteFilter.Value) =
22-
ActionWithTx { request =>
27+
def getCount(filter: QuoteFilter.Value) = action { request =>
2328
import request.dbSession
2429
val count = QuoteQueries().countQuotes(filter)
2530
val response = QuoteCount(count)
2631
json(upickle.write(response))
2732
}
2833

29-
def getList(limit: Int, page: Int, order: QuoteOrdering.Value, filter: QuoteFilter.Value) =
30-
ActionWithTx { request =>
34+
def getList(limit: Int, page: Int, order: QuoteOrdering.Value, filter: QuoteFilter.Value) = action { request =>
3135
import request.dbSession
3236

3337
val pageSize = if (0 <= limit && limit <= 1000) limit else 50

scalajvm/app/global/Options.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package global
2+
3+
import helpers.Cors
4+
import play.api.mvc._
5+
6+
object Options extends Controller {
7+
def corsSupport(url: String) = Action { request =>
8+
NoContent.withHeaders(Cors.headers(request.headers): _*)
9+
}
10+
}

scalajvm/app/helpers/Cors.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package helpers
2+
3+
import global.Options.ORIGIN
4+
import play.api.Play.current
5+
import play.api.mvc.Headers
6+
7+
object Cors {
8+
private lazy val corsHosts: Set[String] = {
9+
val config = current.configuration.getString("cors.allowedOrigins")
10+
config.map(_.split(' ').toSet).getOrElse(Set())
11+
}
12+
13+
def headers(requestHeaders: Headers): List[(String, String)] = {
14+
requestHeaders.get(ORIGIN) match {
15+
case Some(origin) if corsHosts.contains(origin) =>
16+
List (
17+
"Access-Control-Allow-Origin" -> origin,
18+
"Access-Control-Allow-Methods" -> "GET, POST, OPTIONS, DELETE, PUT",
19+
"Access-Control-Max-Age" -> "3600",
20+
"Access-Control-Allow-Headers" -> "Origin, Content-Type, Accept, Authorization",
21+
"Access-Control-Allow-Credentials" -> "true"
22+
)
23+
case _ => List()
24+
}
25+
}
26+
}

scalajvm/conf/application.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,6 @@ basicAuth.password = ${BASIC_AUTH_PASSWORD}
8282
approval.smtpHost = ${APPROVAL_SMTP_HOST}
8383
approval.email = ${APPROVAL_EMAIL}
8484
approval.emailPassword = ${APPROVAL_EMAIL_PASSWORD}
85+
86+
# CORS Settings (enter multiple origins delimited by space)
87+
# cors.allowedOrigins = "http://codingteam.org.ru http://localhost:9000"

scalajvm/conf/routes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ GET /api/quote/random controllers.api.Quotes.getRando
2424
GET /api/quote/count controllers.api.Quotes.getCount(filter: models.queries.QuoteFilter.Value ?= models.queries.QuoteFilter.None)
2525
GET /api/quote/list controllers.api.Quotes.getList(limit: Int ?= 50, page: Int ?= 0, order: models.queries.QuoteOrdering.Value ?= models.queries.QuoteOrdering.Time, filter: models.queries.QuoteFilter.Value ?= models.queries.QuoteFilter.None)
2626

27+
# CORS support
28+
OPTIONS /api/*url global.Options.corsSupport(url: String)
29+
2730
# Map static resources from the /public folder to the /assets URL path
2831
GET /assets/*file controllers.Assets.at(path="/public", file)

0 commit comments

Comments
 (0)