(ns blog.server (:require ["express" :as express] ["express-handlebars" :refer [engine create]] ["express-session" :as session] ["body-parser" :as body-parser] ["cookie-parser" :as cookie-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)))) ;; cookie-parser on (. app use (cookie-parser)) ;; cookie-parser on (. app use (session (-> { :secret (env/get-secret)} (clj->js)))) (. app get "/" (handlers/index-page-handler-factory client)) (. app get "/about" (handlers/about-page-handler-factory client)) (. app get "/login" (handlers/login-page-handler-factory)) (. app post "/login" (handlers/login-page-auth-handler-factory)) (. app get "/logout" (handlers/logout-page-handler-factory)) (. app get "/admin" handlers/is-user-authorized (handlers/admin-panel-handler-factory client)) (. app use "/admin" (handlers/user-not-authorized-factory)) (. app post "/htmx/validation/about/" handlers/is-user-authorized (handlers/htmx-about-validation-handler-factory)) (. app use "/htmx/validation/about/" (handlers/user-not-authorized-factory)) (. app patch "/htmx/about/" handlers/is-user-authorized (handlers/htmx-update-about-handler-factory client)) (. app use "/htmx/about/" (handlers/user-not-authorized-factory)) (. app post "/htmx/search/articles/" (handlers/htmx-search-articles-handler-factory client)) (. app post "/htmx/validation/create-title/" handlers/is-user-authorized (handlers/htmx-create-title-validation-handler-factory client)) (. app use "/htmx/validation/create-title/" (handlers/user-not-authorized-factory)) (. app post "/htmx/validation/create-content/" handlers/is-user-authorized (handlers/htmx-create-content-validation-handler-factory)) (. app use "/htmx/validation/create-content/" (handlers/user-not-authorized-factory)) (. app post "/htmx/articles/" handlers/is-user-authorized (handlers/htmx-create-article-handler-factory client)) (. app use "/htmx/articles/" (handlers/user-not-authorized-factory)) (. app get "/htmx/articles/:id" (handlers/htmx-get-article-handler-factory client)) (. app post "/htmx/admin/search/articles/" handlers/is-user-authorized (handlers/htmx-admin-search-articles-handler-factory client)) (. app use "/htmx/admin/search/articles/" (handlers/user-not-authorized-factory)) (. app get "/htmx/admin/rows/article/:id/" handlers/is-user-authorized (handlers/htmx-get-article-row-handler-factory client)) (. app use "/htmx/admin/rows/article/:id/" (handlers/user-not-authorized-factory)) (. app post "/htmx/validation/update-title/" handlers/is-user-authorized (handlers/htmx-update-title-validation-handler-factory)) (. app use "/htmx/validation/update-title/" (handlers/user-not-authorized-factory)) (. app post "/htmx/validation/update-content/" handlers/is-user-authorized (handlers/htmx-update-content-validation-handler-factory)) (. app use "/htmx/validation/update-content/" (handlers/user-not-authorized-factory)) (. app patch "/htmx/admin/rows/article/:id/" handlers/is-user-authorized (handlers/htmx-update-article-handler-factory client)) (. app use "/htmx/admin/rows/article/:id/" (handlers/user-not-authorized-factory)) (. app delete "/htmx/admin/rows/article/:id/" handlers/is-user-authorized (handlers/htmx-delete-article-row-handler-factory client)) (. app use "/htmx/admin/rows/article/:id/" (handlers/user-not-authorized-factory)) (. app get "/htmx/admin/modals/article/content/:id/" handlers/is-user-authorized (handlers/htmx-get-article-preview-content-handler-factory client)) (. app use "/htmx/admin/modals/article/content/:id/" (handlers/user-not-authorized-factory)) (. app get "/htmx/admin/modals/article/edit-content/:id/" handlers/is-user-authorized (handlers/htmx-get-article-edit-content-handler-factory client)) (. app use "/htmx/admin/modals/article/edit-content/:id/" (handlers/user-not-authorized-factory)) ) (defn start "Starts server." [] (setup-engine) (set-routes) (db/connect-client client) (db/create-tables client) (. app listen port (fn [] (println "Listen on " port))))