gpt4 book ai didi

http - Clojure 跨域错误 - 完全丢失

转载 作者:行者123 更新时间:2023-12-05 00:47:08 28 4
gpt4 key购买 nike

我在 Clojure 中有以下使用 Compojure 的简单服务器(这是一种环形模式)。开发中的一切都运行良好,现在我处于生产状态,我无法让 CORS 为我的一生工作 - 我有一个 wrap-preflight 功能似乎工作正常,但是我在终端中不断收到 CORS 错误,并且我的评论系统的帖子或获取请求都不起作用。我完全迷失了,非常沮丧,我四处打听,似乎没有人知道。

这是主要的 core.clj 代码 - 如果有人有任何想法告诉我。您可以在 thedailyblech.com 上实时查看错误(不是广告,但可能有助于调试)。

谢谢!

(ns clojure-play.core
(:use org.httpkit.server
[compojure.core :refer :all]
[compojure.route :as route]
[clojure.data.json :as json]
[clojure.tools.logging :only [info]]
[clojure-play.routes :as routes]
[ring.middleware.json :only [wrap-json-body]]
[ring.middleware.cors :refer [wrap-cors]])
(:require [monger.core :as mg]
[monger.collection :as mc]
[clojure.edn :as edn]
[clojure.java.io :as io]
[compojure.handler :as handler])
(:import [org.bson.types ObjectId]
[com.mongodb DB WriteConcern])
(:gen-class))
(println "in the beginning was the command line...")

(defonce channels (atom #{}))

(defn connect! [channel]
(info "channel open")
(swap! channels conj channel))

(defn notify-clients [msg]
(doseq [channel @channels]
(send! channel msg)))

(defn disconnect! [channel status]
(info "channel closed:" status)
(swap! channels #(remove #{channel} %)))


(defn ws-handler [request]
(with-channel request channel
(connect! channel)
(on-close channel (partial disconnect! channel))
(on-receive channel #(notify-clients %))))

(defn my-routes [db]
(routes
(GET "/foo" [] "Hello Foo")
(GET "/bar" [] "Hello Bar")
(GET "/json_example/:name" [] routes/json_example)
(GET "/json_example" [] routes/json_example)
(POST "/email" [] routes/post_email)
(POST "/write_comment" [] (fn [req] (routes/write_comment req db)))
(POST "/update_comment" [] (fn [req] (routes/update_comment req db)))
(GET "/read_comments/:path" [path] (fn [req] (routes/read_comments req db path)))
(GET "/read_comments/:path1/:path2" [path1 path2] (fn [req] (routes/read_comments req db (str path1 "/" path2))))
(GET "/ws" [] ws-handler)))

(defn connectDB []
(defonce connection
(let
[uri "mongodb://somemlabthingy"
{:keys [conn db]} (mg/connect-via-uri uri)]
{:conn conn
:db db}))
{:db (:db connection)
:conn (:conn connection)})

(def cors-headers
"Generic CORS headers"
{"Access-Control-Allow-Origin" "*"
"Access-Control-Allow-Headers" "*"
"Access-Control-Allow-Methods" "GET POST OPTIONS DELETE PUT"})

(defn preflight?
"Returns true if the request is a preflight request"
[request]
(= (request :request-method) :options))

(defn -main
"this is main"
[& args]

(println "hello there main")

(def db (get (connectDB) :db))

(println (read-string (slurp (io/resource "environment/config.edn"))))


(defn wrap-preflight [handler]
(fn [request]
(do
(println "inside wrap-preflight")
(println "value of request")
(println request)
(println "value of handler")
(println handler)
(if (preflight? request)
{:status 200
:headers cors-headers
:body "preflight complete"}
(handler request)))))

(run-server
(wrap-preflight
(wrap-cors
(wrap-json-body
(my-routes db)
{:keywords? true :bigdecimals? true})
:access-control-allow-origin [#"http://www.thedailyblech.com"]
:access-control-allow-methods [:get :put :post :delete :options]
:access-control-allow-headers ["Origin" "X-Requested-With"
"Content-Type" "Accept"]))
{:port 4000}))

最佳答案

CORS 中间件自动处理预检内容——您不需要单独的中间件,也不需要生成自己的 header 等。

你有它包装 routes 这是正确的 - 所以 CORS 检查首先发生,然后是路由。您应该删除您的自定义预检中间件,它应该可以在那时工作。

我们在工作中使用 wrap-cors,我们遇到的唯一问题是允许足够的 header (一些由生产基础设施插入,例如负载平衡器)。我们最终得到了这个:

                           :access-control-allow-headers #{"accept"
"accept-encoding"
"accept-language"
"authorization"
"content-type"
"origin"}

对于它的值(value),以下是我们所拥有的方法:

                           :access-control-allow-methods [:delete :get
:patch :post :put]

(你不需要 :options 在那里)

关于http - Clojure 跨域错误 - 完全丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030830/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com