Skip to content

Instantly share code, notes, and snippets.

@thebusby
Created June 13, 2013 05:39
Show Gist options
  • Save thebusby/5771476 to your computer and use it in GitHub Desktop.
Save thebusby/5771476 to your computer and use it in GitHub Desktop.
(ns api-tunnel.http
(:require [compojure.route :as route]
[compojure.handler :as handler])
(:use org.httpkit.server
[datomic.api :only [db q] :as d]
))
;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; -
;; Datomic Stuff
;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; - ;; -
(def conn-uri "datomic:mem://mydb")
(def conn (d/connect conn-uri))
(def default-schema-rec-values
{:db/id #db/id [:db.part/db]
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db})
(defn create-schema-record [rec]
(merge default-schema-rec-values rec))
(def app-schema (map create-schema-record
[{:db/ident :app/name
:db/valueType :db.type/string
:db/fulltext true
:db/doc "Application Name"}
]))
(defn create-rec [data]
"Given a map, generates the associated datalog record
and returns [rec-tempid datalog]"
(let [eid (d/tempid :db.part/user)]
[eid (mapv (fn [[k v]]
{:db/id eid k v})
data)]))
(comment
;; start comments
;; Create the DB if it doesn't exist
(d/create-database conn-uri)
;; Apply schema
(d/transact conn app-schema)
;; Create record
(d/transact conn (->> [{:app/name "foo"}
{:app/name "bar"}
{:app/name "baz"}]
(mapcat (comp second create-rec))
vec))
(q '[:find ?n :where [?c app/name ?n]] (db conn))
;; end comments
)
(comment
;; -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
;; START COMMENTS START COMMENTS START COMMENTS START COMMENTS START COMMENTS START COMMENTS START COMMENTS START COMMENTS
;; -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
(run-server (handler #'app) {:port 8080})
(def app
(-> (handler/api app-routes)
(middleware/wrap-json-body)
(middleware/wrap-json-response)))
(defn- edn-request? [request]
(if-let [type (:content-type request)]
(not (empty? (re-find #"^application/(vnd.+)?edn" type)))))
(defroutes app-routes
(context "/documents" [] (defroutes documents-routes
(GET "/" [] (get-all-documents))
(POST "/" {body :body} (create-new-document body))
(context "/:id" [id] (defroutes document-routes
(GET "/" [] (get-document id))
(PUT "/" {body :body} (update-document id body))
(DELETE "/" [] (delete-document id))))))
(route/not-found "Not Found"))
;; -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
;; END COMMENTS END COMMENTS END COMMENTS END COMMENTS END COMMENTS END COMMENTS END COMMENTS END COMMENTS END COMMENTS END COMMENTS
;; -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment