2017-05-17 36 views

回答

0

我使用Luminus框架,並已將此添加Luminus框架的middleware.clj

(defn create-proxy [handler fns] 
    ;; TODO check whether cookies are sent 
    (let [identifier-fn (get fns :identifier-fn identity) 
     host-fn (get fns :host-fn {}) 
     path-fn (get fns :path-fn identity)] 
    (fn [request] 
     (let [request-key (identifier-fn request) 
      host (host-fn request-key) 
      stripped-headers (dissoc (:headers request) "content-length" "host") 
      path (path-fn (:uri request))] 
     (if host 
      (-> 
      {:url    (build-url host (path-fn (:uri request)) (:query-string request)) 
      :method   (:request-method request) 
      :body    (if-let [len (get-in request [:headers "content-length"])] 
           (slurp-binary 
           (:body request) 
           (Integer/parseInt len))) 
      :headers   stripped-headers 
      :follow-redirects true 
      :throw-exceptions false 
      :as    :stream 
      :insecure?  true 
      } 
      clj-http.client/request 
      (update-in [:headers] dissoc "Transfer-Encoding") 
      ) 
      (handler request)) 
     )))) 


(defn wrap-proxy [handler] 
    (-> handler 
     (create-proxy 
     {:identifier-fn :uri 
     :host-fn (proxy-map-2-host-fn proxy-map) 
     :path-fn (fn [^String uri] 
        (subs uri (inc (.indexOf (rest uri) \/))))}))) 

handler.clj改變app-routes這樣:

(def app-routes 
(routes 
    (-> #'home-routes 
     (wrap-routes middleware/wrap-csrf) 
     (wrap-routes middleware/wrap-formats)) 
    (middleware/wrap-proxy 
    (route/not-found 
     (:body 
     (error-page {:status 404 
        :title "page not found"})))))) 

proxy-map可能是這樣的:

(def proxy-map "proxy map" 
    {"/www" "https://www.example.com" 
    "/test" "http://test.example.com" 
    } 
) 
它的工作原理是

。但是,在使用此框架時,我不需要cider-jack-in-clojurescript,並且每當我保存我的.cljs文件時都會重新加載網頁。因此,只需將任何跨站點路線添加到proxy-map並使用相應的密鑰即可訪問。

例如,使用(GET "/www/some-path'")將起作用,如果您要求(GET "https://www.example.com")

順便說一句,這些代碼中的大部分都從https://github.com/tailrecursion/ring-proxy和其他一些類似的回購中被盜。