[hugo/feat] add static frontend support

This commit is contained in:
SouthFox 2023-08-29 00:24:06 +08:00
parent 10d2732f5a
commit 271b82999b
3 changed files with 24 additions and 6 deletions

View file

@ -1,14 +1,17 @@
(ns backend.core (ns backend.core
(:require [org.httpkit.server :as server] (:require [org.httpkit.server :as server]
[reitit.ring :as ring] [reitit.ring :as ring]
[backend.handlers :as handlers]) [backend.handlers :as handlers]
[backend.hugo.site :as hugo])
(:gen-class)) (:gen-class))
(def app (def app
(ring/ring-handler (ring/ring-handler
(ring/router (ring/router
[["/api" [["/hp/:id" {:parameters {:path {:id int?}}
:get {:handler hugo/build-hu-post}}]
["/api"
["/hp/:id" {:parameters {:path {:id int?}} ["/hp/:id" {:parameters {:path {:id int?}}
:get {:handler handlers/fetch-hu-post}}]]]) :get {:handler handlers/fetch-hu-post}}]]])
(ring/routes (ring/routes

View file

@ -41,7 +41,7 @@
(defn fetch-hu-post (defn fetch-hu-post
[request] [request & {:keys [hugo]}]
(let [id (-> request :path-params :id) (let [id (-> request :path-params :id)
post-url (str/join ["https://zhuanlan.zhihu.com/p/" id]) post-url (str/join ["https://zhuanlan.zhihu.com/p/" id])
page (-> (client/get post-url) :body Jsoup/parse) page (-> (client/get post-url) :body Jsoup/parse)
@ -54,6 +54,8 @@
(let [content {:content (.toString docs) (let [content {:content (.toString docs)
:title (.text title) :title (.text title)
:time (first (str/split (.text post-time) #"・"))}] :time (first (str/split (.text post-time) #"・"))}]
{:status 200 (if hugo
:headers {"Content-Type" "application/json; charset=utf-8"} (.toString docs)
:body (wrap-json content)}))) {:status 200
:headers {"Content-Type" "application/json; charset=utf-8"}
:body (wrap-json content)}))))

View file

@ -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})]]))})