[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
(: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

View file

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

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