Allowing cross-origin requests in Yesod

前端 未结 1 1899
-上瘾入骨i
-上瘾入骨i 2020-12-29 08:44

My application uses a bookmarklet, and I need to allow CORS for MyRouteR so my bookmarklet code can use this route for AJAX requests.

In my first draft

相关标签:
1条回答
  • 2020-12-29 08:49

    UPDATE: Someone else published some generic middleware for this: http://hackage.haskell.org/package/wai-cors.


    I am currently working on the same thing and haven't yet implemented a solution, however I imagine it can be done via a WAI Middleware similar to the sample code on the wiki page Allowing WOFF fonts to be accessed from other domains (CORS). This should allow you from writing the CORS code once without repeating yourself.

    Sample code from the link above to add cross-origin access for WOFF fonts:

    addCORStoWOFF :: W.Middleware
    addCORStoWOFF app = fmap updateHeaders . app
      where
        updateHeaders (W.ResponseFile    status headers fp mpart) = W.ResponseFile    status (new headers) fp mpart
        updateHeaders (W.ResponseBuilder status headers builder)  = W.ResponseBuilder status (new headers) builder
        updateHeaders (W.ResponseSource  status headers src)      = W.ResponseSource  status (new headers) src
        new headers | woff      = cors : headers
                    | otherwise =        headers
          where woff = lookup HT.hContentType headers == Just "application/font-woff"
                cors = ("Access-Control-Allow-Origin", "*")
    
    0 讨论(0)
提交回复
热议问题