私がやろうとしているのは、Play 2.1 アプリケーションで CORS のサポートを構築することです。前提として、/admin のパスで受信するものはすべて、応答で CORS ヘッダーを取得する必要があります。これは、私が StackOverflow でまとめたカット アンド ペースト ソリューションで十分に機能します。各コントローラ アクションは、CORS ヘッダーを応答に押し込むラップされたアクションを利用します。問題は、私の単純な実装が、ルート ファイルに存在しない URL に対する OPTIONS 要求に対して 200 OK を返すようになったことです。URL がルート ファイルに存在することを確認し、ルートが見つからない場合は 404 Not Found を確認するように単純なコントローラーを変更するにはどうすればよいですか?
conf/ルート
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / controllers.Application.index
GET /hb controllers.Application.hb
# Admin API
POST /admin/sessions controllers.admin.Sessions.session
GET /admin/apps controllers.admin.Apps.index
OPTIONS /admin/*url controllers.admin.Admin.options(url: String)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
My Admin Controller は、一般的なオプション処理を提供します
object Admin extends Controller {
val log = LoggerFactory.getLogger(getClass())
def options(url: String) = CORS_Writer {
Action {
log.trace("options( url: {}", url);
/*
* I need to validate that url exists in the routes file here
* (this doesn't compile, but conceptually is what I want).
*/
if(routes(url)) { Ok } else { NotFound }
}
}
}
私が期待する
OPTIONS /admin/sessions HTTP/1.1
HTTP/1.1 200 OK
と
OPTIONS /admin/sessions HTTP/1.1
HTTP/1.1 200 OK
しかし
OPTIONS /admin/bob HTTP/1.1
HTTP/1.1 404 Not Found
私は Scala と Play の両方に不慣れで、ルートの操作の詳細に関して Play 2.1 のドキュメントがひどく不足していることに気づきました。この質問がかなり単純に思える場合は、ご容赦ください。私はこれが可能だと確信しています、私はただ呪文を知りません。