From 271b82999bb0b587c7a4046eb19ad77daaa92a63 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Tue, 29 Aug 2023 00:24:06 +0800 Subject: [PATCH] [hugo/feat] add static frontend support --- src/main/backend/core.clj | 7 +++++-- src/main/backend/handlers.clj | 10 ++++++---- src/main/backend/hugo/site.clj | 13 +++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 src/main/backend/hugo/site.clj diff --git a/src/main/backend/core.clj b/src/main/backend/core.clj index 645634c..4c64e38 100644 --- a/src/main/backend/core.clj +++ b/src/main/backend/core.clj @@ -1,14 +1,17 @@ (ns backend.core (:require [org.httpkit.server :as server] [reitit.ring :as ring] - [backend.handlers :as handlers]) + [backend.handlers :as handlers] + [backend.hugo.site :as hugo]) (:gen-class)) (def app (ring/ring-handler (ring/router - [["/api" + [["/hp/:id" {:parameters {:path {:id int?}} + :get {:handler hugo/build-hu-post}}] + ["/api" ["/hp/:id" {:parameters {:path {:id int?}} :get {:handler handlers/fetch-hu-post}}]]]) (ring/routes diff --git a/src/main/backend/handlers.clj b/src/main/backend/handlers.clj index 72088a6..30146d0 100644 --- a/src/main/backend/handlers.clj +++ b/src/main/backend/handlers.clj @@ -41,7 +41,7 @@ (defn fetch-hu-post - [request] + [request & {:keys [hugo]}] (let [id (-> request :path-params :id) post-url (str/join ["https://zhuanlan.zhihu.com/p/" id]) page (-> (client/get post-url) :body Jsoup/parse) @@ -54,6 +54,8 @@ (let [content {:content (.toString docs) :title (.text title) :time (first (str/split (.text post-time) #"・"))}] - {:status 200 - :headers {"Content-Type" "application/json; charset=utf-8"} - :body (wrap-json content)}))) + (if hugo + (.toString docs) + {:status 200 + :headers {"Content-Type" "application/json; charset=utf-8"} + :body (wrap-json content)})))) diff --git a/src/main/backend/hugo/site.clj b/src/main/backend/hugo/site.clj new file mode 100644 index 0000000..e3b065a --- /dev/null +++ b/src/main/backend/hugo/site.clj @@ -0,0 +1,13 @@ +(ns backend.hugo.site + (:require [backend.handlers :as handlers] + [hiccup2.core :as hiccup])) + + +(defn build-hu-post + [request] + + {:status 200 + :headers {"Content-Type" "text/html; charset=utf-8"} + :body (str (hiccup/html {:escape-strings? false} + [:html + [:body (handlers/fetch-hu-post request {:hugo true})]]))})