diff --git a/src/main/backend/core.clj b/src/main/backend/core.clj index 2787ef8..84a8e46 100644 --- a/src/main/backend/core.clj +++ b/src/main/backend/core.clj @@ -1,12 +1,28 @@ (ns backend.core - (:use ring.adapter.jetty)) + (:require [ring.adapter.jetty :refer [run-jetty]] + [reitit.ring :as ring] + [backend.handlers :as handlers]) + (:gen-class)) -(defn handler [request] - {:status 200 - :headers {"Content-Type" "text/plain"} - :body "Hello World"}) -(defn -main - [& args] - (run-jetty handler {:port 3000 - :join? false})) +(def app + (ring/ring-handler + (ring/router + [["/hp/:id" {:parameters {:path {:id int?}} + :get {:handler handlers/fetch-hu-post}}] + ]) + + (ring/create-default-handler + {:not-found (constantly {:status 404 :body "Not found"})}))) + +(defn -main [& args] + (run-jetty #'app {:port 3000 + :join? false})) + +(def server (run-jetty #'app {:port 3000 + :join? false})) + +(comment + (app {:request-method :get + :uri "/hp/431038004" + })) diff --git a/src/main/backend/handlers.clj b/src/main/backend/handlers.clj new file mode 100644 index 0000000..cb2b44a --- /dev/null +++ b/src/main/backend/handlers.clj @@ -0,0 +1,17 @@ +(ns backend.handlers + (:require [clojure.string :as str] + [babashka.http-client :as client]) + (:import [org.jsoup Jsoup])) + + +(defn fetch-hu-post [request] + (let [id (-> request :path-params :id) + post-url (str/join ["https://zhuanlan.zhihu.com/p/" id])] + + {:status 200 + :headers {"Content-Type" "text/html; charset=utf-8"} + :body (-> (client/get post-url) + :body + Jsoup/parse + (.getElementsByClass "Post-RichTextContainer") + (.toString))}))