Created
November 27, 2019 20:10
-
-
Save ian-moore/ddf9019f713e10dc5b62e065d0fcc8fc to your computer and use it in GitHub Desktop.
shadow-cljs dev http server with conditional proxy requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns my-shadow-app.dev-server | |
(:require [clj-http.client :as client] | |
[clojure.string :as string] | |
[shadow.http.push-state :as push-state]) | |
(:import [org.apache.http NoHttpResponseException])) | |
(defn handler | |
[{:keys [uri http-config body headers request-method] :as request}] | |
(if-not (string/starts-with? uri "/api") | |
(push-state/handle request) | |
(try (client/request | |
{:method request-method | |
:url (str (:dev-server/proxy http-config) uri) | |
:body body | |
:headers (dissoc headers "content-length") | |
:throw-exceptions false}) | |
(catch NoHttpResponseException e | |
{:status 504 | |
:body (.getMessage e)})))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{... | |
:dev-http | |
{9000 | |
{... | |
:handler my-shadow-app.dev-server/handler | |
:dev-server/proxy "http://localhost:8000"}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment