2021-02-15 12:15:16 +01:00
|
|
|
;; 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
|
2021-02-15 12:15:16 +01:00
|
|
|
|
2020-06-29 16:07:48 +02:00
|
|
|
(ns app.http
|
|
|
|
(:require
|
2021-05-05 09:28:12 +02:00
|
|
|
[app.config :as cf]
|
2020-07-30 15:23:11 +02:00
|
|
|
[app.http.export :refer [export-handler]]
|
2021-08-30 16:54:27 +02:00
|
|
|
[app.http.export-frames :refer [export-frames-handler]]
|
2020-07-30 15:23:11 +02:00
|
|
|
[app.http.impl :as impl]
|
2021-09-21 15:12:46 +02:00
|
|
|
[app.util.transit :as t]
|
2021-09-21 16:08:42 +02:00
|
|
|
[app.sentry :as sentry]
|
2021-09-21 15:12:46 +02:00
|
|
|
[cuerdas.core :as str]
|
2020-06-29 16:07:48 +02:00
|
|
|
[lambdaisland.glogi :as log]
|
2020-07-30 15:23:11 +02:00
|
|
|
[promesa.core :as p]
|
|
|
|
[reitit.core :as r]))
|
2020-06-29 16:07:48 +02:00
|
|
|
|
|
|
|
(def routes
|
2021-08-30 16:54:27 +02:00
|
|
|
[["/export-frames" {:handler export-frames-handler}]
|
|
|
|
["/export" {:handler export-handler}]])
|
2020-06-29 16:07:48 +02:00
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(def instance (atom nil))
|
|
|
|
|
2021-09-21 15:12:46 +02:00
|
|
|
(defn- on-error
|
|
|
|
[error request]
|
|
|
|
(let [{:keys [type message code] :as data} (ex-data error)]
|
2021-09-21 16:08:42 +02:00
|
|
|
(sentry/capture-exception error {::sentry/request request
|
|
|
|
:ex-data data})
|
|
|
|
|
2021-09-21 15:12:46 +02:00
|
|
|
(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 ""}))))
|
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(defn init
|
|
|
|
[]
|
2020-07-30 15:23:11 +02:00
|
|
|
(let [router (r/router routes)
|
2021-09-21 15:12:46 +02:00
|
|
|
handler (impl/router-handler router)
|
|
|
|
server (impl/server handler on-error)
|
2021-05-05 09:28:12 +02:00
|
|
|
port (cf/get :http-server-port 6061)]
|
|
|
|
(.listen server port)
|
2021-09-21 15:12:46 +02:00
|
|
|
(log/info :msg "welcome to penpot"
|
|
|
|
:module "exporter"
|
2021-09-21 16:08:42 +02:00
|
|
|
:version (:full @cf/version))
|
2021-05-05 09:28:12 +02:00
|
|
|
(log/info :msg "starting http server" :port port)
|
|
|
|
(reset! instance server)))
|
2020-06-29 16:07:48 +02:00
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(defn stop
|
|
|
|
[]
|
|
|
|
(if-let [server @instance]
|
|
|
|
(p/create (fn [resolve]
|
|
|
|
(.close server (fn []
|
|
|
|
(log/info :msg "shutdown http server")
|
|
|
|
(resolve)))))
|
|
|
|
(p/resolved nil)))
|