[back/feat] add CORS header

This commit is contained in:
SouthFox 2024-02-04 22:08:28 +08:00
parent 0b582e2fb0
commit 6326097e01
2 changed files with 12 additions and 9 deletions

View file

@ -50,7 +50,7 @@
(apply str (mapv build-catalog-item catalog)))) (apply str (mapv build-catalog-item catalog))))
(defn process-hu-post (defn process-hu-post
[page {:keys [content-type replace-str wrap-fn]}] [page {:keys [resp-headers replace-str wrap-fn]}]
(let [post-content (.getElementsByClass page "Post-RichTextContainer") (let [post-content (.getElementsByClass page "Post-RichTextContainer")
title (.getElementsByClass page "Post-Title") title (.getElementsByClass page "Post-Title")
post-time (.getElementsByClass page "ContentItem-time")] post-time (.getElementsByClass page "ContentItem-time")]
@ -60,7 +60,7 @@
(render-linkcard post-content) (render-linkcard post-content)
{:status 200 {:status 200
:headers content-type :headers resp-headers
:body (wrap-fn :body (wrap-fn
{:content (.toString post-content) {:content (.toString post-content)
:title (.text title) :title (.text title)
@ -74,7 +74,7 @@
docs (.getElementsByClass page "Post-RichTextContainer")] docs (.getElementsByClass page "Post-RichTextContainer")]
(if (empty? docs) (if (empty? docs)
{:status 404 {:status 404
:headers (:content-type params) :headers (:resp-headers params)
:body (wrap-json {:content "Not Found" :body (wrap-json {:content "Not Found"
:title "Not Found"})} :title "Not Found"})}
(process-hu-post page params)))) (process-hu-post page params))))
@ -89,7 +89,8 @@
[request] [request]
(let [post-id (-> request :path-params :id)] (let [post-id (-> request :path-params :id)]
(fetch-hu-post post-id (fetch-hu-post post-id
{:content-type {"Content-Type" "application/json; charset=utf-8"} {:resp-headers {"Content-Type" "application/json; charset=utf-8"
"Access-Control-Allow-Origin" "*"}
:replace-str "#/hp/" :replace-str "#/hp/"
:wrap-fn wrap-json}))) :wrap-fn wrap-json})))
@ -129,9 +130,9 @@
:updated_time (get m "updatedTime")})) :updated_time (get m "updatedTime")}))
(defn process-hu-questiion (defn process-hu-questiion
[question-json {:keys [content-type wrap-fn]}] [question-json {:keys [resp-headers wrap-fn]}]
{:status 200 {:status 200
:headers content-type :headers resp-headers
:body (wrap-fn question-json)}) :body (wrap-fn question-json)})
(defn fetch-hu-answers (defn fetch-hu-answers
@ -195,7 +196,7 @@
question-error? (= (get question-json "subAppName") "errorpage")] question-error? (= (get question-json "subAppName") "errorpage")]
(if question-error? (if question-error?
{:status 404 {:status 404
:headers (:content-type params) :headers (:resp-headers params)
:body (wrap-json {:content "Not Found" :body (wrap-json {:content "Not Found"
:title "Not Found"})} :title "Not Found"})}
(if (empty? query) (if (empty? query)
@ -213,5 +214,6 @@
[request] [request]
(fetch-hu-question (fetch-hu-question
request request
{:content-type {"Content-Type" "application/json; charset=utf-8"} {:resp-headers {"Content-Type" "application/json; charset=utf-8"
"Access-Control-Allow-Origin" "*"}
:wrap-fn wrap-json})) :wrap-fn wrap-json}))

View file

@ -29,6 +29,7 @@
(let [post-id (-> request :path-params :id)] (let [post-id (-> request :path-params :id)]
(handlers/fetch-hu-post (handlers/fetch-hu-post
post-id post-id
{:content-type {"Content-Type" "text/html; charset=utf-8"} {:resp-headers {"Content-Type" "text/html; charset=utf-8"
"Access-Control-Allow-Origin" "*"}
:replace-str "/hp/" :replace-str "/hp/"
:wrap-fn hugo-post-template}))) :wrap-fn hugo-post-template})))