add text render api route
This commit is contained in:
parent
7df24726e4
commit
2e9cca8e4c
|
@ -156,6 +156,9 @@
|
|||
|
||||
(defonce fonts (atom {}))
|
||||
|
||||
(defn get-font [name]
|
||||
(get @fonts name))
|
||||
|
||||
(defn load-all! []
|
||||
(->> flf-files
|
||||
(reduce
|
||||
|
|
39
src/toascii/routes/api/text.clj
Normal file
39
src/toascii/routes/api/text.clj
Normal file
|
@ -0,0 +1,39 @@
|
|||
(ns toascii.routes.api.text
|
||||
(:require [clojure.string :as str]
|
||||
[liberator.core :refer [defresource]]
|
||||
[compojure.core :refer [ANY]]
|
||||
[clj-figlet.core :as figlet]
|
||||
[toascii.route-utils :refer [register-routes]]
|
||||
[toascii.models.flf :refer [get-font]]))
|
||||
|
||||
(defresource render-text [{:keys [s font format] :as params}]
|
||||
:media-type-available?
|
||||
(fn [ctx]
|
||||
(let [type (condp = format
|
||||
"html" "text/html"
|
||||
"text" "text/plain"
|
||||
"text/plain")]
|
||||
{:representation {:media-type type}}))
|
||||
:malformed?
|
||||
(fn [_]
|
||||
(str/blank? s))
|
||||
:exists?
|
||||
(fn [ctx]
|
||||
(let [font-name (or font "standard")]
|
||||
(if-let [flf (get-font font-name)]
|
||||
{:flf flf})))
|
||||
:handle-ok
|
||||
(fn [ctx]
|
||||
(let [rendered (figlet/render-to-string (:flf ctx) s)]
|
||||
(if (= "text/html" (get-in ctx [:representation :media-type]))
|
||||
(str "<pre>" rendered "</pre>")
|
||||
rendered)))
|
||||
:handle-malformed
|
||||
(fn [_]
|
||||
"Missing text to render.")
|
||||
:handle-not-found
|
||||
(fn [_]
|
||||
"Font not found."))
|
||||
|
||||
(register-routes api-text-routes
|
||||
(ANY "/api/text" {params :params} (render-text params)))
|
Reference in a new issue