{"action":"created","comment":{"url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/comments/66120717","id":66120717,"diff_hunk":"@@ -0,0 +1,99 @@\n+(ns puppetlabs.crates-caching-proxy.core\n+  (:import [java.nio.file Files Paths StandardCopyOption Path CopyOption]\n+           [java.nio.file.attribute FileAttribute])\n+  (:require [ring.adapter.jetty :as rj]\n+            [clojure.string :as str]\n+            [puppetlabs.http.client.sync :as hsync]\n+            [ring.util.request :as rreq]\n+            [ring.util.response :as rresp]\n+            [puppetlabs.comidi :as cmdi]\n+            [clojure.tools.logging :as log]\n+            [slingshot.slingshot :refer [throw+ try+]]\n+            [environ.core :as environ]))\n+\n+(def cache-root (delay (get environ/env :cache-root \"proxy-cache\")))\n+\n+(def port (delay (Long/parseLong (get environ/env :crate-proxy-port \"8888\"))))\n+\n+(def crates-io-api-root \"/api/v1/crates\")\n+\n+(def crates-io-url (str \"https://crates.io\" crates-io-api-root))\n+\n+(defn ^Path path-get [^String s & more-strings]\n+  (Paths/get s (into-array String more-strings)))\n+\n+(defn ^Path create-temp-file [^Path dir prefix suffix]\n+  (Files/createTempFile dir prefix suffix (into-array FileAttribute [])))\n+\n+(defn create-file-response [package version package-name]\n+  (rresp/file-response (format \"%s/%s/%s\" package version package-name)\n+                       {:root @cache-root\n+                        :index-files? false\n+                        :allow-symlinks? false}))\n+\n+(defn download-crate [package version]\n+  (let [package-url (format \"%s/%s/%s/download\" crates-io-url package version)\n+        _ (log/infof \"Downloading package using URL '%s'\" package-url)\n+        resp (hsync/get package-url)]\n+\n+    (log/debugf \"Response received from URL '%s' was '%s' with a content length of\"\n+                package-url\n+                (:status resp)\n+                (get-in resp [:headers \"content-length\"]))\n+\n+    (if (= 200 (:status resp))\n+      (:body resp)\n+      (throw+ {:kind ::download-error\n+               :status (:status resp)\n+               :response-body (slurp (:body resp))}\n+              (format \"Error downloading package '%s'\" package-url)))))\n+\n+(defn store-crate [package version package-name]\n+  (let [crate-path (path-get @cache-root package version)\n+        final-crate-path (path-get @cache-root package version package-name)]\n+    (Files/createDirectories crate-path (into-array FileAttribute []))\n+    (let [temp-file (create-temp-file crate-path package \"crate\")]\n+      (with-open [input-stream (download-crate package version)]\n+        (Files/copy input-stream temp-file (into-array CopyOption [StandardCopyOption/REPLACE_EXISTING])))\n+      (Files/move temp-file final-crate-path (into-array CopyOption [StandardCopyOption/ATOMIC_MOVE]))\n+      (create-file-response package version package-name))))\n+\n+(defn find-crate [package version package-name]\n+  (if-let [file-response (create-file-response package version package-name)]\n+    (do\n+      (log/infof \"Found version '%s' of '%s' cached, returning cached version\" version package-name)","path":"src/puppetlabs/crates_caching_proxy/core.clj","position":64,"original_position":64,"commit_id":"24a47fa97a99df208f7fda0cb3d33a6810802657","original_commit_id":"24a47fa97a99df208f7fda0cb3d33a6810802657","user":{"login":"rbrw","id":10120698,"avatar_url":"https://avatars.githubusercontent.com/u/10120698?v=3","gravatar_id":"","url":"https://api.github.com/users/rbrw","html_url":"https://github.com/rbrw","followers_url":"https://api.github.com/users/rbrw/followers","following_url":"https://api.github.com/users/rbrw/following{/other_user}","gists_url":"https://api.github.com/users/rbrw/gists{/gist_id}","starred_url":"https://api.github.com/users/rbrw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rbrw/subscriptions","organizations_url":"https://api.github.com/users/rbrw/orgs","repos_url":"https://api.github.com/users/rbrw/repos","events_url":"https://api.github.com/users/rbrw/events{/privacy}","received_events_url":"https://api.github.com/users/rbrw/received_events","type":"User","site_admin":false},"body":"Wonder about just using (pr-str x) with %s to handle the quotes.  The normal case should look normal, and others will be unambiguous.","created_at":"2016-06-07T17:56:48Z","updated_at":"2016-06-07T17:56:48Z","html_url":"https://github.com/puppetlabs/crates-caching-proxy/pull/1#discussion_r66120717","pull_request_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/comments/66120717"},"html":{"href":"https://github.com/puppetlabs/crates-caching-proxy/pull/1#discussion_r66120717"},"pull_request":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/1","id":72353889,"html_url":"https://github.com/puppetlabs/crates-caching-proxy/pull/1","diff_url":"https://github.com/puppetlabs/crates-caching-proxy/pull/1.diff","patch_url":"https://github.com/puppetlabs/crates-caching-proxy/pull/1.patch","issue_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/issues/1","number":1,"state":"open","locked":false,"title":"(PDB-2678) Initial checkin of the caching proxy","user":{"login":"senior","id":215910,"avatar_url":"https://avatars.githubusercontent.com/u/215910?v=3","gravatar_id":"","url":"https://api.github.com/users/senior","html_url":"https://github.com/senior","followers_url":"https://api.github.com/users/senior/followers","following_url":"https://api.github.com/users/senior/following{/other_user}","gists_url":"https://api.github.com/users/senior/gists{/gist_id}","starred_url":"https://api.github.com/users/senior/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/senior/subscriptions","organizations_url":"https://api.github.com/users/senior/orgs","repos_url":"https://api.github.com/users/senior/repos","events_url":"https://api.github.com/users/senior/events{/privacy}","received_events_url":"https://api.github.com/users/senior/received_events","type":"User","site_admin":false},"body":"The code lazily stores crates from crates.io as they are requested. Storing them locally after retrieving them from the remote server. The cache directory and the port can be changed via environment variables and activity is logged to the logs directory using logback.","created_at":"2016-06-02T14:16:13Z","updated_at":"2016-06-07T17:56:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b5698e12a5e69c1e3e8e837d237b733102f922d2","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/1/commits","review_comments_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/1/comments","review_comment_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/comments{/number}","comments_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/issues/1/comments","statuses_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/statuses/24a47fa97a99df208f7fda0cb3d33a6810802657","head":{"label":"senior:ticket/master/pdb-2678-cargo-mirror","ref":"ticket/master/pdb-2678-cargo-mirror","sha":"24a47fa97a99df208f7fda0cb3d33a6810802657","user":{"login":"senior","id":215910,"avatar_url":"https://avatars.githubusercontent.com/u/215910?v=3","gravatar_id":"","url":"https://api.github.com/users/senior","html_url":"https://github.com/senior","followers_url":"https://api.github.com/users/senior/followers","following_url":"https://api.github.com/users/senior/following{/other_user}","gists_url":"https://api.github.com/users/senior/gists{/gist_id}","starred_url":"https://api.github.com/users/senior/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/senior/subscriptions","organizations_url":"https://api.github.com/users/senior/orgs","repos_url":"https://api.github.com/users/senior/repos","events_url":"https://api.github.com/users/senior/events{/privacy}","received_events_url":"https://api.github.com/users/senior/received_events","type":"User","site_admin":false},"repo":{"id":60269622,"name":"crates-caching-proxy","full_name":"senior/crates-caching-proxy","owner":{"login":"senior","id":215910,"avatar_url":"https://avatars.githubusercontent.com/u/215910?v=3","gravatar_id":"","url":"https://api.github.com/users/senior","html_url":"https://github.com/senior","followers_url":"https://api.github.com/users/senior/followers","following_url":"https://api.github.com/users/senior/following{/other_user}","gists_url":"https://api.github.com/users/senior/gists{/gist_id}","starred_url":"https://api.github.com/users/senior/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/senior/subscriptions","organizations_url":"https://api.github.com/users/senior/orgs","repos_url":"https://api.github.com/users/senior/repos","events_url":"https://api.github.com/users/senior/events{/privacy}","received_events_url":"https://api.github.com/users/senior/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/senior/crates-caching-proxy","description":"Web application that acts as a caching proxy for crates.io","fork":true,"url":"https://api.github.com/repos/senior/crates-caching-proxy","forks_url":"https://api.github.com/repos/senior/crates-caching-proxy/forks","keys_url":"https://api.github.com/repos/senior/crates-caching-proxy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/senior/crates-caching-proxy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/senior/crates-caching-proxy/teams","hooks_url":"https://api.github.com/repos/senior/crates-caching-proxy/hooks","issue_events_url":"https://api.github.com/repos/senior/crates-caching-proxy/issues/events{/number}","events_url":"https://api.github.com/repos/senior/crates-caching-proxy/events","assignees_url":"https://api.github.com/repos/senior/crates-caching-proxy/assignees{/user}","branches_url":"https://api.github.com/repos/senior/crates-caching-proxy/branches{/branch}","tags_url":"https://api.github.com/repos/senior/crates-caching-proxy/tags","blobs_url":"https://api.github.com/repos/senior/crates-caching-proxy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/senior/crates-caching-proxy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/senior/crates-caching-proxy/git/refs{/sha}","trees_url":"https://api.github.com/repos/senior/crates-caching-proxy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/senior/crates-caching-proxy/statuses/{sha}","languages_url":"https://api.github.com/repos/senior/crates-caching-proxy/languages","stargazers_url":"https://api.github.com/repos/senior/crates-caching-proxy/stargazers","contributors_url":"https://api.github.com/repos/senior/crates-caching-proxy/contributors","subscribers_url":"https://api.github.com/repos/senior/crates-caching-proxy/subscribers","subscription_url":"https://api.github.com/repos/senior/crates-caching-proxy/subscription","commits_url":"https://api.github.com/repos/senior/crates-caching-proxy/commits{/sha}","git_commits_url":"https://api.github.com/repos/senior/crates-caching-proxy/git/commits{/sha}","comments_url":"https://api.github.com/repos/senior/crates-caching-proxy/comments{/number}","issue_comment_url":"https://api.github.com/repos/senior/crates-caching-proxy/issues/comments{/number}","contents_url":"https://api.github.com/repos/senior/crates-caching-proxy/contents/{+path}","compare_url":"https://api.github.com/repos/senior/crates-caching-proxy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/senior/crates-caching-proxy/merges","archive_url":"https://api.github.com/repos/senior/crates-caching-proxy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/senior/crates-caching-proxy/downloads","issues_url":"https://api.github.com/repos/senior/crates-caching-proxy/issues{/number}","pulls_url":"https://api.github.com/repos/senior/crates-caching-proxy/pulls{/number}","milestones_url":"https://api.github.com/repos/senior/crates-caching-proxy/milestones{/number}","notifications_url":"https://api.github.com/repos/senior/crates-caching-proxy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/senior/crates-caching-proxy/labels{/name}","releases_url":"https://api.github.com/repos/senior/crates-caching-proxy/releases{/id}","deployments_url":"https://api.github.com/repos/senior/crates-caching-proxy/deployments","created_at":"2016-06-02T14:12:12Z","updated_at":"2016-06-02T13:57:36Z","pushed_at":"2016-06-02T19:26:11Z","git_url":"git://github.com/senior/crates-caching-proxy.git","ssh_url":"git@github.com:senior/crates-caching-proxy.git","clone_url":"https://github.com/senior/crates-caching-proxy.git","svn_url":"https://github.com/senior/crates-caching-proxy","homepage":null,"size":12,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"puppetlabs:master","ref":"master","sha":"5c307b1856abdb60219362537010df91df345eb1","user":{"login":"puppetlabs","id":234268,"avatar_url":"https://avatars.githubusercontent.com/u/234268?v=3","gravatar_id":"","url":"https://api.github.com/users/puppetlabs","html_url":"https://github.com/puppetlabs","followers_url":"https://api.github.com/users/puppetlabs/followers","following_url":"https://api.github.com/users/puppetlabs/following{/other_user}","gists_url":"https://api.github.com/users/puppetlabs/gists{/gist_id}","starred_url":"https://api.github.com/users/puppetlabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/puppetlabs/subscriptions","organizations_url":"https://api.github.com/users/puppetlabs/orgs","repos_url":"https://api.github.com/users/puppetlabs/repos","events_url":"https://api.github.com/users/puppetlabs/events{/privacy}","received_events_url":"https://api.github.com/users/puppetlabs/received_events","type":"Organization","site_admin":false},"repo":{"id":60268474,"name":"crates-caching-proxy","full_name":"puppetlabs/crates-caching-proxy","owner":{"login":"puppetlabs","id":234268,"avatar_url":"https://avatars.githubusercontent.com/u/234268?v=3","gravatar_id":"","url":"https://api.github.com/users/puppetlabs","html_url":"https://github.com/puppetlabs","followers_url":"https://api.github.com/users/puppetlabs/followers","following_url":"https://api.github.com/users/puppetlabs/following{/other_user}","gists_url":"https://api.github.com/users/puppetlabs/gists{/gist_id}","starred_url":"https://api.github.com/users/puppetlabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/puppetlabs/subscriptions","organizations_url":"https://api.github.com/users/puppetlabs/orgs","repos_url":"https://api.github.com/users/puppetlabs/repos","events_url":"https://api.github.com/users/puppetlabs/events{/privacy}","received_events_url":"https://api.github.com/users/puppetlabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/puppetlabs/crates-caching-proxy","description":"Web application that acts as a caching proxy for crates.io","fork":false,"url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy","forks_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/forks","keys_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/teams","hooks_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/hooks","issue_events_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/issues/events{/number}","events_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/events","assignees_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/assignees{/user}","branches_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/branches{/branch}","tags_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/tags","blobs_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/git/refs{/sha}","trees_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/statuses/{sha}","languages_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/languages","stargazers_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/stargazers","contributors_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/contributors","subscribers_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/subscribers","subscription_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/subscription","commits_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/commits{/sha}","git_commits_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/git/commits{/sha}","comments_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/comments{/number}","issue_comment_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/issues/comments{/number}","contents_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/contents/{+path}","compare_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/merges","archive_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/downloads","issues_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/issues{/number}","pulls_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls{/number}","milestones_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/milestones{/number}","notifications_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/labels{/name}","releases_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/releases{/id}","deployments_url":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/deployments","created_at":"2016-06-02T13:57:36Z","updated_at":"2016-06-02T13:57:36Z","pushed_at":"2016-06-02T19:26:11Z","git_url":"git://github.com/puppetlabs/crates-caching-proxy.git","ssh_url":"git@github.com:puppetlabs/crates-caching-proxy.git","clone_url":"https://github.com/puppetlabs/crates-caching-proxy.git","svn_url":"https://github.com/puppetlabs/crates-caching-proxy","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/1"},"html":{"href":"https://github.com/puppetlabs/crates-caching-proxy/pull/1"},"issue":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/issues/1"},"comments":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/puppetlabs/crates-caching-proxy/statuses/24a47fa97a99df208f7fda0cb3d33a6810802657"}}}}
{"actor":{"display_login":"rbrw"}}