Commit 3d385867 authored by phil's avatar phil

initial commit

parents
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.hgignore
.hg/
This diff is collapsed.
# webhooks
FIXME: description
## Installation
Download from http://example.com/FIXME.
## Usage
FIXME: explanation
$ java -jar webhooks-0.1.0-standalone.jar [args]
## Options
FIXME: listing of options this app accepts.
## Examples
...
### Bugs
...
### Any Other Sections
### That You Think
### Might be Useful
## License
Copyright © 2017 FIXME
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.
# Introduction to webhooks
TODO: write [great documentation](http://jacobian.org/writing/what-to-write/)
(defproject webhooks "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/tools.cli "0.3.5"]
[http-kit "2.2.0"]]
:main ^:skip-aot webhooks.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
(ns webhooks.core
"webhooks --port 9000 --config webhooks.edn"
(:gen-class)
(:require [org.httpkit.server :refer [run-server]]
[clojure.java.shell :refer [sh]]
[clojure.string :refer [split]]
[clojure.pprint :refer [pprint]]
[clojure.tools.cli :refer [parse-opts]]))
(defonce options (atom {:options {:port 9000
:config "webhook.edn"
:help false}}))
(defn load-config []
(read-string (slurp (:config (:options @options)))))
(defn handler [req]
(let [config (load-config)
endpoints (filter #(= (:path %) (:uri req)) (:endpoints config))]
(if-not (empty? endpoints)
(do
;; TODO dump payload in file pass file to sh
(doall (map #(apply sh (split (:exec %) #" ")) endpoints))
{:status 200
:headers {"Content-Type" "text/plain"}
:body "200 OK"})
{:status 404
:body (with-out-str
(println "404 Not found (unknown endpoint)")
(pprint req)
(pprint @options))})))
(defonce server (atom nil))
(defn stop-server []
(when-not (nil? @server)
(@server :timeout 100)
(reset! server nil)))
(defn start-server []
(let [port (:port (:options @options))]
(reset! server (run-server #'handler {:port port}))
(println (str "Accepting connections on port " port "."))))
(def cli-options
[["-p" "--port PORT" "Port number"
:default 9000
:parse-fn #(Integer/parseInt %)
:validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]
["-c" "--config" "Config file"
:default "webhooks.edn"]
["-h" "--help"]])
(defn -main [& args]
(reset! options (parse-opts args cli-options))
(if (:help (:options @options))
(println (:summary @options))
(start-server)))
(comment
(start-server)
(stop-server)
)
(ns webhooks.core-test
(:require [clojure.test :refer :all]
[webhooks.core :refer :all]))
(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))
{:endpoints
[{:path "/gh/page-build"
:exec "/usr/bin/aplay /usr/share/games/wesnoth/1.10/sounds/slider.wav"}]}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment