From add243af59b54daa642e862136bd6f60f3bb2609 Mon Sep 17 00:00:00 2001 From: eltrac Date: Wed, 7 Oct 2026 17:54:20 +0800 Subject: handler: add /api/count #48 --- src/weepinbell/webserver/counter.clj | 12 +++++++ src/weepinbell/webserver/facade.clj | 7 ++-- src/weepinbell/webserver/handler.clj | 63 ++++++++++++++++++++---------------- 3 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 src/weepinbell/webserver/counter.clj (limited to 'src') diff --git a/src/weepinbell/webserver/counter.clj b/src/weepinbell/webserver/counter.clj new file mode 100644 index 0000000..80b7541 --- /dev/null +++ b/src/weepinbell/webserver/counter.clj @@ -0,0 +1,12 @@ +(ns weepinbell.webserver.counter + "Service returning count of webmention lists, parallel to retriever." + (:require [weepinbell.webserver.retriever :as ret])) + +(defn- reduce-to-count [resp] + (assoc resp :body {:count (-> resp :body count str)})) + +(defn by-url [url store opts] + (reduce-to-count (ret/by-url url nil store opts))) + +(defn by-domain [domain store opts] + (reduce-to-count (ret/by-domain domain nil store opts))) diff --git a/src/weepinbell/webserver/facade.clj b/src/weepinbell/webserver/facade.clj index 23d5689..4a7bead 100644 --- a/src/weepinbell/webserver/facade.clj +++ b/src/weepinbell/webserver/facade.clj @@ -6,7 +6,8 @@ ;; services [weepinbell.webserver.receiver :as receiver] [weepinbell.webserver.retriever :as retriever] - [weepinbell.webserver.feed :as feed])) + [weepinbell.webserver.feed :as feed] + [weepinbell.webserver.counter :as counter])) (s/def ::server-meta (s/keys :req-un [::wmc ::store ::config])) @@ -18,7 +19,9 @@ :retrieve-by-domain retriever/by-domain :retrieve-by-url retriever/by-url :retrieve-by-id retriever/by-id - :retrieve-by-pair retriever/by-st-pair}) + :retrieve-by-pair retriever/by-st-pair + :count-by-domain counter/by-domain + :count-by-url counter/by-url}) (defn start-webserver "Starts web server. Returns a function that stops the server." diff --git a/src/weepinbell/webserver/handler.clj b/src/weepinbell/webserver/handler.clj index b1d6990..ff8f87b 100644 --- a/src/weepinbell/webserver/handler.clj +++ b/src/weepinbell/webserver/handler.clj @@ -8,11 +8,12 @@ [ring.middleware.resource :refer [wrap-resource]] [ring.middleware.json :refer [wrap-json-response]])) -(s/def ::services +(s/def ::services (s/and (s/keys :req-un [::receive ::retrieve-by-url ::retrieve-by-domain - ::retrieve-by-id ::retrieve-by-pair ::gen-feed]) + ::retrieve-by-id ::retrieve-by-pair ::gen-feed + ::count-by-domain ::count-by-url]) #(every? fn? (vals %)))) -(s/def ::api-meta +(s/def ::api-meta (s/keys :req-un [::wmc ::store ::config ::services])) (defn- make-api @@ -23,39 +24,45 @@ :post [sequential?]} (let [{:keys [wmc store config services]} api-meta {:keys [receive retrieve-by-url retrieve-by-domain - retrieve-by-id retrieve-by-pair - gen-feed]} services] + retrieve-by-id retrieve-by-pair + count-by-domain count-by-url gen-feed]} services] [;; receiver endpoint - (POST (:endpoint config) [source target] - (receive {:s source :t target} wmc config)) + (POST (:endpoint config) [source target] + (receive {:s source :t target} wmc config)) ;; query API routes - (context "/api" [] + (context "/api" [] ;; query webmention list - (context "/mentions" [page microformats mftype] - (GET "/" [url] - (retrieve-by-url url page store - {:microformats microformats - :type mftype})) - (GET "/:domain" [domain] - (retrieve-by-domain domain page store - {:microformats microformats - :type mftype}))) + (context "/mentions" [page microformats mftype] + (GET "/" [url] + (retrieve-by-url url page store + {:microformats microformats + :type mftype})) + (GET "/:domain" [domain] + (retrieve-by-domain domain page store + {:microformats microformats + :type mftype}))) + ;; query count of webmention list + (context "/count" [mftype] + (GET "/" [url] + (count-by-url url store {:type mftype})) + (GET "/:domain" [domain] + (count-by-domain domain store {:type mftype}))) ;; query one specific webmention - (context "/mention" [microformats] - (GET "/:id" [id] (retrieve-by-id id store - {:microformats microformats})) - (GET "/" [source target] (retrieve-by-pair source target store - {:microformats microformats})))) + (context "/mention" [microformats] + (GET "/:id" [id] (retrieve-by-id id store + {:microformats microformats})) + (GET "/" [source target] (retrieve-by-pair source target store + {:microformats microformats})))) ;; RSS feed - (GET "/feed/:domain" [domain] (gen-feed domain store config)) + (GET "/feed/:domain" [domain] (gen-feed domain store config)) ;; host proxied avatars (microformats photos) - (route/files "/avatars/" {:root "data/avatars"}) + (route/files "/avatars/" {:root "data/avatars"}) ;; frontend - (when-let [fe (:frontend config)] - (when (= (:source fe) :disk) - (route/files "/" {:root (:dir fe)}))) + (when-let [fe (:frontend config)] + (when (= (:source fe) :disk) + (route/files "/" {:root (:dir fe)}))) ;; 404 - (route/not-found "Not Found")])) + (route/not-found "Not Found")])) (defn- make-handler "Receive a vector of handlers. Remove nil elements. -- cgit v1.2.3