You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.7 KiB
105 lines
2.7 KiB
(ns blog.server
|
|
(:require ["express" :as express]
|
|
["express-handlebars" :refer [engine create]]
|
|
["body-parser" :as body-parser]
|
|
[blog.db :as db]
|
|
[blog.env :as env]
|
|
[blog.handlers :as handlers]
|
|
[blog.helpers :as helpers]))
|
|
|
|
(def app (express))
|
|
(def port (env/get-port))
|
|
(def client (db/create-client))
|
|
|
|
(defn register-helpers
|
|
"Register helpers to use them in the handlebars's templates."
|
|
[]
|
|
|
|
(let [hbs (create (clj->js {}))]
|
|
(.registerHelper (.-handlebars hbs)
|
|
"full-timestamp"
|
|
(helpers/full-timestamp-factory))
|
|
|
|
(.registerHelper (.-handlebars hbs)
|
|
"article-timestamp"
|
|
(helpers/article-timestamp-factory))
|
|
|
|
(.registerHelper (.-handlebars hbs)
|
|
"truncatestring"
|
|
(helpers/truncatestring-factory))
|
|
|
|
(.registerHelper (.-handlebars hbs)
|
|
"is-brief"
|
|
(helpers/is-brief-factory))
|
|
|
|
(.registerHelper (.-handlebars hbs)
|
|
"navbar-item-style"
|
|
(helpers/navbar-item-style-factory))))
|
|
|
|
(defn setup-engine
|
|
"Sets the engine to handlebars and simple set up."
|
|
[]
|
|
|
|
; For using .hbs as the file extension instead of .handlebars
|
|
(. app
|
|
engine ".hbs" (engine (-> {:extname ".hbs"
|
|
:defaultLayout "main"}
|
|
(clj->js))))
|
|
|
|
(. app
|
|
set "view engine" ".hbs")
|
|
|
|
(. app
|
|
set "views" "./views")
|
|
|
|
(register-helpers))
|
|
|
|
(defn set-routes
|
|
"Sets the routes for server."
|
|
[]
|
|
|
|
;; need to parse request body for forms.
|
|
(. app use (.urlencoded body-parser
|
|
(-> {:extended false}
|
|
(clj->js))))
|
|
|
|
(. app get "/"
|
|
(handlers/index-page-handler-factory client))
|
|
(fn [req res]
|
|
|
|
(. app get "/admin"
|
|
(handlers/admin-panel-handler-factory client))
|
|
|
|
(. app post "/htmx/search/articles/"
|
|
(handlers/htmx-search-articles-handler-factory client))
|
|
|
|
(. app post "/htmx/articles/"
|
|
(handlers/htmx-create-article-handler-factory client))
|
|
|
|
(. app get "/htmx/articles/:id"
|
|
(handlers/htmx-get-article-handler-factory client))
|
|
|
|
(. app get "/htmx/admin/rows/article/:id/"
|
|
(handlers/htmx-get-article-row-handler-factory client))
|
|
|
|
(. app get "/htmx/admin/modals/article/content/:id/"
|
|
(handlers/htmx-get-article-preview-content-handler-factory client))
|
|
|
|
(. app delete "/htmx/admin/rows/article/:id/"
|
|
(handlers/htmx-delete-article-row-handler-factory client))
|
|
)
|
|
|
|
(defn start
|
|
"Starts server."
|
|
[]
|
|
(setup-engine)
|
|
(set-routes)
|
|
|
|
(db/connect-client client)
|
|
(db/create-tables client)
|
|
|
|
(. app listen port
|
|
(fn []
|
|
(println "Listen on " port))))
|
|
|