From 0eda5c040c7450054ac00be34d5b63bf315566b9 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Tue, 14 Feb 2017 12:15:51 +0100 Subject: [PATCH] Endpoint for serving recording JSON file --- project.clj | 4 +++- resources/asciinema/system.edn | 6 +++--- src/asciinema/endpoint/asciicasts.clj | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/asciinema/endpoint/asciicasts.clj diff --git a/project.clj b/project.clj index 3cec63c..79a7ed6 100644 --- a/project.clj +++ b/project.clj @@ -7,7 +7,9 @@ [metosin/ring-http-response "0.8.1"] [clj-time "0.12.0"] [duct "0.8.2"] - [compojure "1.5.2"] + [compojure "1.5.1"] + [metosin/compojure-api "1.1.10"] + [prismatic/schema "1.1.3"] [environ "1.1.0"] [ring "1.5.0"] [ring/ring-defaults "0.2.1"] diff --git a/resources/asciinema/system.edn b/resources/asciinema/system.edn index 84aa5df..0f9a1b7 100644 --- a/resources/asciinema/system.edn +++ b/resources/asciinema/system.edn @@ -6,12 +6,12 @@ :file-store #var asciinema.component.s3-file-store/s3-file-store :file-server #var asciinema.component.s3-file-server/s3-file-server} :endpoints - {:example #var asciinema.endpoint.example/example-endpoint} + {:asciicasts #var asciinema.endpoint.asciicasts/asciicasts-endpoint} :dependencies {:http [:app] - :app [:example] + :app [:asciicasts] :ragtime [:db] - :example [:db]} + :asciicasts [:db :file-server]} :config {:app {:middleware diff --git a/src/asciinema/endpoint/asciicasts.clj b/src/asciinema/endpoint/asciicasts.clj new file mode 100644 index 0000000..8674d61 --- /dev/null +++ b/src/asciinema/endpoint/asciicasts.clj @@ -0,0 +1,21 @@ +(ns asciinema.endpoint.asciicasts + (:require [asciinema.boundary + [asciicast-database :as adb] + [file-server :as fserver]] + [asciinema.model.asciicast :as asciicast] + [compojure.api.sweet :refer :all] + [ring.util.http-response :as response] + [schema.core :as s])) + +(defn asciicasts-endpoint [{:keys [db file-server]}] + (api + (context + "/a" [] + (GET "/:token.json" [] + :path-params [token :- String] + :query-params [{dl :- s/Bool false}] + (if-let [asciicast (adb/get-asciicast-by-token db token)] + (let [path (asciicast/json-store-path asciicast) + filename (str "asciicast-" (:id asciicast) ".json")] + (fserver/serve file-server path (when dl {:filename filename}))) + (response/not-found))))))