From 83be44cd9a718e7126192db772b3175418103eaf Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 4 May 2018 14:21:31 +0200 Subject: [PATCH] Add ip binding and handle errors --- env/dev/resources/config.edn | 3 ++- env/prod/resources/config.edn | 3 ++- src/clj/yenu/core.clj | 38 +++++++++++++++-------------------- src/clj/yenu/routes/core.clj | 21 ++++++++++--------- yenu.properties | 5 +++-- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/env/dev/resources/config.edn b/env/dev/resources/config.edn index 7cb3251..1175fab 100644 --- a/env/dev/resources/config.edn +++ b/env/dev/resources/config.edn @@ -1,5 +1,6 @@ {:dev true :port 3000 + :host "0.0.0.0" :creator-password "creator" :user-password "user" ;; when :nrepl-port is set the application starts the nREPL server on load @@ -10,5 +11,5 @@ :basepath "http://localhost" :authors ["Aaron Fischer"] - :images_per_page 25 + :images-per-page 25 } diff --git a/env/prod/resources/config.edn b/env/prod/resources/config.edn index 7104a45..1a5d661 100644 --- a/env/prod/resources/config.edn +++ b/env/prod/resources/config.edn @@ -2,11 +2,12 @@ :creator-password "xxx" :user-password "yyy" :port 3000 + :host "0.0.0.0" :database-url "jdbc:sqlite:yenu.db" :basepath "http://localhost" :authors ["Aaron Fischer"] - :images_per_page 25 + :images-per-page 25 } diff --git a/src/clj/yenu/core.clj b/src/clj/yenu/core.clj index c34fbf3..f81d511 100644 --- a/src/clj/yenu/core.clj +++ b/src/clj/yenu/core.clj @@ -4,34 +4,29 @@ [luminus.http-server :as http] [luminus-migrations.core :as migrations] [yenu.config :refer [env]] - [clojure.tools.cli :refer [parse-opts]] [clojure.tools.logging :as log] [yenu.helpers.images :as image] [mount.core :as mount]) (:gen-class)) -(def cli-options - [["-p" "--port PORT" "Port number" - :parse-fn #(Integer/parseInt %)]]) +(mount/defstate ^{:on-reload :noop} + http-server + :start + (http/start + (-> env + (assoc :handler (handler/app)) + (update :port #(or (-> env :options :port) %)))) + :stop + (http/stop http-server)) (mount/defstate ^{:on-reload :noop} - http-server - :start - (http/start - (-> env - (assoc :handler (handler/app)) - (update :port #(or (-> env :options :port) %)))) - :stop - (http/stop http-server)) - -(mount/defstate ^{:on-reload :noop} - repl-server - :start - (when-let [nrepl-port (env :nrepl-port)] - (repl/start {:port nrepl-port})) - :stop - (when repl-server - (repl/stop repl-server))) + repl-server + :start + (when-let [nrepl-port (env :nrepl-port)] + (repl/start {:port nrepl-port})) + :stop + (when repl-server + (repl/stop repl-server))) (defn stop-app [] @@ -41,7 +36,6 @@ (defn start-app [args] (doseq [component (-> args - (parse-opts cli-options) mount/start-with-args :started)] (log/info component "started")) diff --git a/src/clj/yenu/routes/core.clj b/src/clj/yenu/routes/core.clj index 7c5e1f0..dda55d9 100644 --- a/src/clj/yenu/routes/core.clj +++ b/src/clj/yenu/routes/core.clj @@ -54,7 +54,7 @@ (page (inc current) (= current num) "disabled" (h/html "Weiter " [:span.fa.fa-arrow-right]))]))) (defn index-page [current request] - (let [image-count (:images_per_page env) + (let [image-count (:images-per-page env) offset (* (dec current) image-count) pages (number-of-pages image-count) images (db/get-all-images {:offset offset :count image-count})] @@ -69,14 +69,17 @@ next-img (db/get-next-image {:image-date (:created_at image)}) prev-img (db/get-prev-image {:image-date (:created_at image)}) saved-author (get-in request [:cookies "author" :value])] - (layout/render-file "detail.html" - {:image image - :flash (:flash request) - :next-image next-img - :prev-image prev-img - :tags (db/get-tags-for-image {:id (:id image)}) - :saved-author saved-author - :comments (db/get-comments-for-image {:image_id (:id image)})}))) + (if (not (nil? image)) + (layout/render-file "detail.html" + {:image image + :flash (:flash request) + :next-image next-img + :prev-image prev-img + :tags (db/get-tags-for-image {:id (:id image)}) + :saved-author saved-author + :comments (db/get-comments-for-image {:image_id (:id image)})}) + (layout/error-page {:status 404 + :title "page not found"})))) (defn add-comment! [image-id request] (let [author (:author (:params request)) diff --git a/yenu.properties b/yenu.properties index 6d307f9..9550a79 100644 --- a/yenu.properties +++ b/yenu.properties @@ -1,4 +1,5 @@ -basepath=http://localhost +basepath=http://127.0.0.1 +host=0.0.0.0 port=3000 # Passwords @@ -6,5 +7,5 @@ user-password=user creator-password=creator # Content stuff -images_per_page=25 +images-per-page=25 authors=Aaron Fischer,Beatrice Fischer