0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-25 07:58:49 -05:00
penpot/exporter/src/app/http.cljs

79 lines
2.4 KiB
Text
Raw Normal View History

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
2021-04-10 09:43:04 +02:00
;; Copyright (c) UXBOX Labs SL
(ns app.http
(:require
[app.config :as cf]
2020-07-30 15:23:11 +02:00
[app.http.export :refer [export-handler]]
[app.http.export-frames :refer [export-frames-handler]]
2020-07-30 15:23:11 +02:00
[app.http.impl :as impl]
[app.util.transit :as t]
[app.sentry :as sentry]
[cuerdas.core :as str]
[lambdaisland.glogi :as log]
2020-07-30 15:23:11 +02:00
[promesa.core :as p]
[reitit.core :as r]))
(def routes
[["/export-frames" {:handler export-frames-handler}]
["/export" {:handler export-handler}]])
(def instance (atom nil))
(defn- on-error
[error request]
(let [{:keys [type message code] :as data} (ex-data error)]
(sentry/capture-exception error {::sentry/request request
:ex-data data})
(cond
(= :validation type)
(let [header (get-in request [:headers "accept"])]
(if (and (str/starts-with? header "text/html")
(= :spec-validation (:code data)))
{:status 400
:headers {"content-type" "text/html"}
:body (str "<pre style='font-size:16px'>" (:explain data) "</pre>\n")}
{:status 400
:headers {"content-type" "text/html"}
:body (str "<pre style='font-size:16px'>" (:explain data) "</pre>\n")}))
(and (= :internal type)
(= :browser-not-ready code))
{:status 503
:headers {"x-error" (t/encode data)}
:body ""}
:else
(do
(log/error :msg "Unexpected error" :error error)
(js/console.error error)
{:status 500
:headers {"x-error" (t/encode data)}
:body ""}))))
(defn init
[]
2020-07-30 15:23:11 +02:00
(let [router (r/router routes)
handler (impl/router-handler router)
server (impl/server handler on-error)
port (cf/get :http-server-port 6061)]
(.listen server port)
(log/info :msg "welcome to penpot"
:module "exporter"
:version (:full @cf/version))
(log/info :msg "starting http server" :port port)
(reset! instance server)))
(defn stop
[]
(if-let [server @instance]
(p/create (fn [resolve]
(.close server (fn []
(log/info :msg "shutdown http server")
(resolve)))))
(p/resolved nil)))