Skip to content

Instantly share code, notes, and snippets.

@rlander
Forked from ian-moore/dev_server.clj
Created January 21, 2023 00:08
Show Gist options
  • Save rlander/88cfa39294a15173796c3415490fb816 to your computer and use it in GitHub Desktop.
Save rlander/88cfa39294a15173796c3415490fb816 to your computer and use it in GitHub Desktop.
shadow-cljs dev http server with conditional proxy requests
(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)}))))
{...
: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