Skip to content

Instantly share code, notes, and snippets.

@nicferrier
Created June 4, 2013 08:55
Show Gist options
  • Select an option

  • Save nicferrier/5704616 to your computer and use it in GitHub Desktop.

Select an option

Save nicferrier/5704616 to your computer and use it in GitHub Desktop.
;;; elnode-proxy.el -- proxying with elnode -*- lexical-binding: t -*-
(require 's)
(require 'web)
(defun elnode/web->elnode-hdr (hdr httpcon)
(apply
'elnode-http-start
httpcon 200
(mapcar
(lambda (hdr-pair)
(cons (symbol-name (car hdr-pair))
(cdr hdr-pair)))
(kvhash->alist hdr))))
(defun elnode-make-proxy (url)
(lambda (httpcon)
(let* ((method (elnode-http-method httpcon))
(path (elnode-http-pathinfo httpcon))
(params (web-to-query-string
(elnode-http-params httpcon)))
(params-alist (list (cons "path" path)
(cons "params" params)))
(web-url (s-format url 'aget params-alist))
hdr-sent)
(process-put
httpcon
:elnode-child-process
(web-http-call
method
(lambda (httpc hdr data)
(unless hdr-sent
(elnode/web->elnode-hdr hdr httpcon)
(setq hdr/sent t))
(if (eq data :done)
(elnode-http-return httpcon)
(elnode-http-send-string httpcon data)))
:mode 'stream
:url web-url)))))
(elnode-start
(elnode-make-proxy "http://localhost:8090${path}")
:port 8091)
;;; elnode-proxy.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment