Skip to content

Commit

Permalink
chore: Apply standard-clj fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
goshatch committed Nov 6, 2024
1 parent dcc2cea commit 206eff3
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 246 deletions.
7 changes: 3 additions & 4 deletions build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

(ns build
(:require
[clojure.tools.build.api :as build-api]
[clojure.pprint :as pprint]))
[clojure.pprint :as pprint]
[clojure.tools.build.api :as build-api]))

;; ---------------------------------------------------------
;; Project configuration
Expand Down Expand Up @@ -53,10 +53,9 @@
Checks that `.` and `/` directories are not deleted"
[directory]
(when
(not (contains? #{"." "/"} directory))
(not (contains? #{"." "/"} directory))
(build-api/delete {:path (or (:path directory) "target")})))


(defn uberjar
"Create an archive containing Clojure and the build of the project
Merge command line configuration to the default project config"
Expand Down
6 changes: 4 additions & 2 deletions cljs/tools/ifs/parts/core.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns tools.ifs.parts.core
(:require ["d3" :as d3]
["htmx.org" :default htmx]))
(:require
["d3" :as d3]
["htmx.org"]
[htmx]))

(def node-data
[{:type "exile"}
Expand Down
20 changes: 10 additions & 10 deletions dev/mulog_events.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

(ns mulog-events
(:require
[com.brunobonacci.mulog :as mulog]
[com.brunobonacci.mulog :as mulog]
[com.brunobonacci.mulog.buffer :as mulog-buffer]))

;; ---------------------------------------------------------
;; Set event global context
;; - information added to every event for REPL workflow
(mulog/set-global-context! {:app-name "parts Service",
(mulog/set-global-context! {:app-name "parts Service"
:version "0.1.0", :env "dev"})
;; ---------------------------------------------------------

Expand All @@ -23,13 +23,13 @@

(deftype TapPublisher
[buffer transform]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_] buffer)
(publish-delay [_] 200)
(publish [_ buffer]
(doseq [item (transform (map second (mulog-buffer/items buffer)))]
(tap> item))
(mulog-buffer/clear buffer)))
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_] buffer)
(publish-delay [_] 200)
(publish [_ buffer]
(doseq [item (transform (map second (mulog-buffer/items buffer)))]
(tap> item))
(mulog-buffer/clear buffer)))

#_{:clj-kondo/ignore [:unused-private-var]}
(defn ^:private tap-events
Expand All @@ -41,7 +41,7 @@
and other tap sources
`mulog-tap-publisher` to stop publisher"
(mulog/start-publisher!
{:type :custom, :fqn-function "mulog-events/tap-events"}))
{:type :custom, :fqn-function "mulog-events/tap-events"}))

#_{:clj-kondo/ignore [:unused-public-var]}
(defn stop
Expand Down
1 change: 0 additions & 1 deletion dev/portal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
;; Data inspector
[portal.api :as inspect]))


;; ---------------------------------------------------------
;; Start Portal and capture all evaluation results

Expand Down
19 changes: 9 additions & 10 deletions dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@
;; - alias included in the Emacs `.dir-locals.el` file
;; ---------------------------------------------------------


(ns user
"Tools for REPL Driven Development"
(:require
[clojure.tools.namespace.repl :as namespace]
[com.brunobonacci.mulog :as mulog] ; Global context & Tap publisher
;; REPL Workflow
[mulog-events] ; Event Logging
[com.brunobonacci.mulog :as mulog] ; Global context & Tap publisher
[mulog-events] ; Event Logging
[portal]
[portal.api :as inspect] ; Data inspector
[portal.api :as inspect] ; Data inspector
[tools.ifs.parts :as parts]
[tools.ifs.parts.config :as config]
[tools.ifs.parts.db :as db]
[tools.ifs.parts.api.auth :as auth]
[tools.ifs.parts.api.account :as account]
[tools.ifs.parts.api.auth :as auth]
[tools.ifs.parts.api.middleware :as middleware]
[clojure.tools.namespace.repl :as namespace]))
[tools.ifs.parts.config :as config]
[tools.ifs.parts.db :as db]))

;; ---------------------------------------------------------
;; Help
Expand Down Expand Up @@ -69,8 +68,8 @@
;; Avoid reloading `dev` code
;; - code in `dev` directory should be evaluated if changed to reload into repl
(println
"Set REPL refresh directories to "
(namespace/set-refresh-dirs "src" "resources"))
"Set REPL refresh directories to "
(namespace/set-refresh-dirs "src" "resources"))
;; ---------------------------------------------------------

;; ---------------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
:source-paths
{:source-paths
["cljs"]

:dependencies
Expand All @@ -18,5 +17,4 @@
:asset-path "/js"
:modules {:main {:init-fn tools.ifs.parts.core/init}}
:devtools {:http-root "resources/public"
:http-port 8020}}}
}
:http-port 8020}}}}
109 changes: 54 additions & 55 deletions src/tools/ifs/parts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@
;; ---------------------------------------------------------

(ns tools.ifs.parts
(:gen-class)
(:require
[com.brunobonacci.mulog :as mulog]
[org.httpkit.server :as server]
[reitit.ring :as ring]
[reitit.coercion.spec]
[reitit.ring :as ring]
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[ring.middleware.json :refer [wrap-json-body wrap-json-response]]
[ring.middleware.params :refer [wrap-params]]
[tools.ifs.parts.api.account :as account]
[tools.ifs.parts.api.auth :as auth]
[tools.ifs.parts.api.middleware :as middleware]
[tools.ifs.parts.db :as db]
[tools.ifs.parts.pages :as pages]
[tools.ifs.parts.api.auth :as auth]
[tools.ifs.parts.api.account :as account]
[tools.ifs.parts.waitlist :as waitlist]))
[tools.ifs.parts.waitlist :as waitlist])
(:gen-class))

;; ---------------------------------------------------------
;; Application

(def prelaunch-app
(middleware/wrap-default-middlewares
(ring/ring-handler
(ring/router
[["/" {:get {:handler #(pages/home-page %)}}]
["/up"
{:get {:handler (fn [_] {:status 200 :body "OK"})}}]
["/waitlist-signup" {:post {:handler #(waitlist/signup %)}}]]
{:data {:middleware [wrap-params
middleware/exception
middleware/logging
middleware/wrap-html-response]}}))))
(ring/ring-handler
(ring/router
[["/" {:get {:handler #(pages/home-page %)}}]
["/up"
{:get {:handler (fn [_] {:status 200 :body "OK"})}}]
["/waitlist-signup" {:post {:handler #(waitlist/signup %)}}]]
{:data {:middleware [wrap-params
middleware/exception
middleware/logging
middleware/wrap-html-response]}}))))

;; TODO: We need to later figure out a way to combine HTML routes and API
;; routes.
Expand All @@ -55,44 +55,44 @@
;; routes will remain.
(def app
(middleware/wrap-default-middlewares
(ring/ring-handler
(ring/router
[["/swagger.json"
{:get {:no-doc true
:swagger {:info {:title "Parts API"
:description "API for Parts"}}
:handler (swagger/create-swagger-handler)}}]
["/api"
["/ping"
{:get {:swagger {:tags ["Utility"]}
:handler (fn [_] {:status 200 :body {:message "Pong!"}})}}]
["/auth" {:swagger {:tags ["Authentication"]}}
["/login"
{:post {:handler auth/login}}]
["/logout"
{:post {:handler auth/logout
:middleware [middleware/jwt-auth]}}]]
["/account" {:swagger {:tags ["Account"]}}
[""
{:get {:handler account/get-account}
:patch {:handler account/update-account}
:delete {:handler account/delete-account}
:middleware [middleware/jwt-auth]}]
["/register"
{:post {:handler account/register-account}}]]]
["/" {:get {:handler #(pages/home-page %)}}]]
{:data {:middleware [wrap-params
middleware/exception
middleware/logging
[wrap-json-body {:keywords? true}]
wrap-json-response
middleware/wrap-jwt-authentication]}})
(ring/routes
(swagger-ui/create-swagger-ui-handler
{:path "/swagger-ui"
:config {:validatorUrl nil
:operationsSorter "alpha"}})
(ring/create-default-handler)))))
(ring/ring-handler
(ring/router
[["/swagger.json"
{:get {:no-doc true
:swagger {:info {:title "Parts API"
:description "API for Parts"}}
:handler (swagger/create-swagger-handler)}}]
["/api"
["/ping"
{:get {:swagger {:tags ["Utility"]}
:handler (fn [_] {:status 200 :body {:message "Pong!"}})}}]
["/auth" {:swagger {:tags ["Authentication"]}}
["/login"
{:post {:handler auth/login}}]
["/logout"
{:post {:handler auth/logout
:middleware [middleware/jwt-auth]}}]]
["/account" {:swagger {:tags ["Account"]}}
[""
{:get {:handler account/get-account}
:patch {:handler account/update-account}
:delete {:handler account/delete-account}
:middleware [middleware/jwt-auth]}]
["/register"
{:post {:handler account/register-account}}]]]
["/" {:get {:handler #(pages/home-page %)}}]]
{:data {:middleware [wrap-params
middleware/exception
middleware/logging
[wrap-json-body {:keywords? true}]
wrap-json-response
middleware/wrap-jwt-authentication]}})
(ring/routes
(swagger-ui/create-swagger-ui-handler
{:path "/swagger-ui"
:config {:validatorUrl nil
:operationsSorter "alpha"}})
(ring/create-default-handler)))))

(defn start-server
"Starts the web server"
Expand All @@ -105,7 +105,7 @@
[& args]
(let [port (or (some-> (first args) Integer/parseInt) 3000)]
(mulog/set-global-context!
{:app-name "Parts" :version "0.1.0-SNAPSHOT"})
{:app-name "Parts" :version "0.1.0-SNAPSHOT"})
(mulog/log ::application-startup :arguments args :port port)
(db/init-db)
(let [stop-fn (start-server port)]
Expand All @@ -116,7 +116,6 @@

;; ---------------------------------------------------------


;; ---------------------------------------------------------
;; Rick Comment
#_{:clj-kondo/ignore [:redefined-var]}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/ifs/parts/api/auth.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns tools.ifs.parts.api.auth
(:require
[tools.ifs.parts.auth :as auth]
[com.brunobonacci.mulog :as mulog]
[ring.util.response :as response]
[com.brunobonacci.mulog :as mulog]))
[tools.ifs.parts.auth :as auth]))

(defn login
[request]
Expand Down
60 changes: 30 additions & 30 deletions src/tools/ifs/parts/api/middleware.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
(ns tools.ifs.parts.api.middleware
(:require [reitit.ring.middleware.exception :as exception]
[com.brunobonacci.mulog :as mulog]
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]
[buddy.auth :refer [authenticated?]]
[ring.util.response :as response]
[clojure.string :as str]
[tools.ifs.parts.auth :as auth]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.middleware.resource :refer [wrap-resource]]
[ring.middleware.content-type :refer [wrap-content-type]]
[ring.middleware.session :refer [wrap-session]]
[ring.middleware.session.cookie :refer [cookie-store]])
(:import (org.sqlite SQLiteException)))
(:require
[buddy.auth :refer [authenticated?]]
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]
[clojure.string :as str]
[com.brunobonacci.mulog :as mulog]
[reitit.ring.middleware.exception :as exception]
[ring.middleware.content-type :refer [wrap-content-type]]
[ring.middleware.defaults :refer [site-defaults wrap-defaults]]
[ring.middleware.resource :refer [wrap-resource]]
[ring.middleware.session :refer [wrap-session]]
[ring.middleware.session.cookie :refer [cookie-store]]
[ring.util.response :as response]
[tools.ifs.parts.auth :as auth])
(:import
(org.sqlite SQLiteException)))

(defn exception-handler
"Generic exceptions handler"
Expand All @@ -34,30 +36,29 @@
(mulog/log ::sqlite-exception :error error-message)
{:status 409
:body {:error (or (some
(fn [[k, v]] (when (str/includes? error-message k) v))
sqlite-errors)
(fn [[k, v]] (when (str/includes? error-message k) v))
sqlite-errors)
"A database constraint was violated")}}))

(def exception
"Middleware handling exceptions"
(exception/create-exception-middleware
(merge
exception/default-handlers
{;; ex-info with :type :validation
:validation (exception-handler "Invalid data" 400)
(merge
exception/default-handlers
{;; ex-info with :type :validation
:validation (exception-handler "Invalid data" 400)

:not-found (exception-handler "Resource not found" 404)
:not-found (exception-handler "Resource not found" 404)

;; SQLite exceptions
SQLiteException sqlite-constraint-violation-handler
;; SQLite exceptions
SQLiteException sqlite-constraint-violation-handler

;; Default
::exception/default
(fn [^Exception e _request]
(mulog/log ::unhandled-exception :error (.getMessage e))
{:status 500
:body {:error "Internal server error"}})
})))
;; Default
::exception/default
(fn [^Exception e _request]
(mulog/log ::unhandled-exception :error (.getMessage e))
{:status 500
:body {:error "Internal server error"}})})))

(defn logging
"Middleware logging each incoming request"
Expand All @@ -84,7 +85,6 @@
(-> (response/response {:error "Unauthorized"})
(response/status 401)))))


(defn wrap-default-middlewares
"Wrap in the middlewares defined by ring-defaults"
[handler]
Expand Down
Loading

0 comments on commit 206eff3

Please sign in to comment.