Skip to content

Commit

Permalink
small pre-release refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
synthomat committed Oct 13, 2022
1 parent de2b07a commit 8c29645
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 69 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).

## [0.3] – UNRELEASED
## [0.3] – 2022-10-13
### Added
* Authentication system!
* Registration and Login now possible!
* Bookmarks are now assigned to users

### Changed
* Myuri is now using the [Bulma CSS](https://bulma.io/) framework – looks much better now!
* IDs were changed from integers to UUID

## [0.2] – 2022-05-09
### Added
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM clojure:temurin-17-focal as build
FROM clojure:temurin-19-focal as build
RUN mkdir -p /opt/build
COPY . /opt/build
WORKDIR /opt/build
RUN clj -T:build uber

FROM eclipse-temurin:17-jre
FROM eclipse-temurin:19-jre
RUN mkdir -p /opt/app
COPY --from=build /opt/build/target/myuri-*.jar /opt/app/myuri.jar

Expand Down
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# synthomat/myuri
MyUri is a simple bookmark manager.
# Myuri
Myuri is a simple bookmark manager.

It allows adding bookmarks using a convenient bookmarklet.
It allows bookmarking websites using a convenient bookmarklet.

![Version 0.2](doc/screenshots/myuri_0.2.jpeg)

## Kick Start with Docker-Compose
The most convenient way of starting the software is by using docker-compose
```bash
$ curl -OJ https://raw.githubusercontent.com/synthomat/myuri/main/docker-compose.yml
$ docker-compose up
```
Then open browser at http://localhost:3000/

## Installation
### Prerequisites
* JRE 8+
Expand All @@ -23,14 +31,14 @@ This will produce a self contained uberjar in the `target/` directory.

Run that uberjar:
```bash
$ DATABASE_URL=jdbc:postgresql://localhost/myuri java -jar target/myuri-0.2.jar
$ DATABASE_URL=jdbc:postgresql://localhost/myuri java -jar target/myuri-0.3.jar
```

| Environment | Default | Example | Explanation |
|--------------|--------|-----------------------------------|---------------------------------------------------------------------------------------------------------|
| DATABASE_URL || jdbc:postgresql://localhost/myuri | Database connection string |
| COOKIE_SECRET | JS9D2GG6ytgOLx06 | PAWXt2maWB8bpBCR | 16-bytes encryption key for the Cookie Session store. **This is critical** – please change the default! |
| PORT | 3000 | 3000 | Web App HTTP Port |
| Environment | Default | Example | Explanation |
|---------------|------------------|-----------------------------------|---------------------------------------------------------------------------------------------------------|
| DATABASE_URL | | jdbc:postgresql://localhost/myuri | Database connection string |
| COOKIE_SECRET | JS9D2GG6ytgOLx06 | PAWXt2maWB8bpBCR | 16-bytes encryption key for the Cookie Session store. **This is critical** – please change the default! |
| PORT | 3000 | 3000 | Web App HTTP Port |



Expand All @@ -48,7 +56,7 @@ $ docker-compose up -d

## Build Docker image
```bash
$ docker build -t myuri:0.2 .
$ docker build -t myuri:0.3 .
```

## Dev environment
Expand Down
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(:require [org.corfield.build :as bb]))

(def lib 'net.clojars.synthomat/myuri)
(def version "0.2")
(def version "0.3")
(def main 'myuri.core)

(defn test "Run the tests." [opts]
Expand Down
49 changes: 29 additions & 20 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
ring/ring-core {:mvn/version "1.9.5"}
ring/ring-jetty-adapter {:mvn/version "1.9.5"}
org.postgresql/postgresql {:mvn/version "42.3.6"}
seancorfield/next.jdbc {:mvn/version "1.2.659"}
migratus/migratus {:mvn/version "1.3.6"}
hiccup/hiccup {:mvn/version "1.0.5"}
bidi/bidi {:mvn/version "2.1.6"}
com.stuartsierra/component {:mvn/version "1.1.0"}
aero/aero {:mvn/version "1.1.6"}
ring/ring-defaults {:mvn/version "0.3.3"}
ring/ring-devel {:mvn/version "1.9.5"}
cheshire/cheshire {:mvn/version "5.11.0"}
buddy/buddy-core {:mvn/version "1.10.413"}
:deps {;; Core ------------------------------------------------------------
org.clojure/clojure {:mvn/version "1.11.1"}
com.stuartsierra/component {:mvn/version "1.1.0"} ; DI / Lifecycle Management
aero/aero {:mvn/version "1.1.6"} ; Configuration

;; Web -------------------------------------------------------------
ring/ring-core {:mvn/version "1.9.6"}
ring/ring-jetty-adapter {:mvn/version "1.9.6"}
ring/ring-defaults {:mvn/version "0.3.4"}
ring/ring-devel {:mvn/version "1.9.6"}
ring-cors/ring-cors {:mvn/version "0.1.13"}
bidi/bidi {:mvn/version "2.1.6"} ; Routing
hiccup/hiccup {:mvn/version "1.0.5"} ; HTML Templating

;; Authn & Authz ---------------------------------------------------
buddy/buddy-auth {:mvn/version "3.0.323"}
buddy/buddy-hashers {:mvn/version "1.8.158"}
com.draines/postal {:mvn/version "2.0.5"}
ring-cors/ring-cors {:mvn/version "0.1.13"}
metosin/malli {:mvn/version "0.8.9"}
com.github.seancorfield/honeysql {:mvn/version "2.3.911"}}

;; Database Layer --------------------------------------------------
seancorfield/next.jdbc {:mvn/version "1.2.659"} ; JDBC
org.postgresql/postgresql {:mvn/version "42.5.0"}
migratus/migratus {:mvn/version "1.4.4"} ; DB Migrations
com.github.seancorfield/honeysql {:mvn/version "2.3.928"} ; SQL Abstraction

;; Various Libraries -----------------------------------------------
cheshire/cheshire {:mvn/version "5.11.0"} ; JSON Lib
com.draines/postal {:mvn/version "2.0.5"} ; SMTP Client
metosin/malli {:mvn/version "0.8.9"} ; Validation
}


:aliases {:run-m {:main-opts ["-m" "myuri.core"]}
:dev {:extra-paths ["dev"]
:extra-deps {org.clojure/tools.namespace {:mvn/version "1.3.0"}}}
:build {:deps {io.github.seancorfield/build-clj
{:git/tag "v0.6.3" :git/sha "9b8e09b"
{:git/tag "v0.8.3" :git/sha "7ac1f8d"
;; since we're building an app uberjar, we do not
;; need deps-deploy for clojars.org deployment:
:deps/root "slim"}}
:ns-default build}
:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.1.1"}
io.github.cognitect-labs/test-runner
{:git/tag "v0.5.0" :git/sha "48c3c67"}}}}}
{:git/tag "v0.5.1" :git/sha "dfb30dd"}}}}}
2 changes: 1 addition & 1 deletion dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(def system nil)

(defn init []
(let [config (read-config (clojure.java.io/resource "config.edn"))]
(let [config (read-config (clojure.java.io/resource "config.defaults.edn"))]
(alter-var-root #'system
(constantly (system/new-system config)))))

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/myuri/core.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(ns myuri.core
(:require [com.stuartsierra.component :as comp]
[myuri.system :as system]
[aero.core :refer [read-config]]
[aero.core :as aero]
[clojure.tools.logging :as log])
(:gen-class))

(defn -main
[& args]
(let [config (read-config (clojure.java.io/resource "config.edn"))
(let [config (aero/read-config (clojure.java.io/resource "config.defaults.edn"))
system (system/new-system config)]
(log/info "Starting System")
(comp/start-system system)))
11 changes: 8 additions & 3 deletions src/myuri/web/auth/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[myuri.model :as model]
[myuri.web.auth.views :as av]
[ring.util.response :as resp]
[ring.util.response]))
[ring.util.response]
[myuri.web.utils :refer [is-post?]]))


(defn check-user-password
Expand All @@ -19,7 +20,7 @@
(defn login-handler
"docstring"
[{:keys [ds] :as req}]
(if-not (= (:request-method req) :post)
(if-not (is-post? req)
(av/login-view req)
(let [{:keys [username password]} (:params req)]
(if-let [user (check-user-password ds username password)]
Expand Down Expand Up @@ -48,7 +49,7 @@
"docstring"
[{:keys [ds] :as req}]

(if-not (= (:request-method req) :post)
(if-not (is-post? req)
(av/register-view req)
(let [params (-> req :params)
user (-> params
Expand All @@ -61,3 +62,7 @@
(model/create-user ds nil user)
(resp/redirect "/auth/login"))))))

(defn unauthorized-handler
"docstring"
[req _]
(resp/redirect (str "/auth/login")))
53 changes: 26 additions & 27 deletions src/myuri/web/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,27 @@
[myuri.model :as m]
[myuri.web.auth.handler :as ah]
[myuri.web.views :as v]
[ring.middleware.defaults :refer :all]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.middleware.reload :refer [wrap-reload]]
[ring.util.response :as res]
[ring.util.response]
[buddy.auth.backends :as backends]
[buddy.auth :refer [authenticated?]]
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]
[buddy.auth.accessrules :refer [wrap-access-rules]]
[ring.middleware.session.cookie :refer [cookie-store]]
[ring.util.response :as resp]
[myuri.web.utils :refer [user-id]]
[ring.middleware.cors :refer [wrap-cors]])
[myuri.web.utils :refer [user-id is-post?]]
[ring.middleware.cors :refer [wrap-cors]]
[myuri.web.auth.handler :refer [unauthorized-handler]])
(:import (java.time.format DateTimeFormatter)))

(use 'clojure.pprint)

;; Utils ----------------------------------------------------------------------
(defn is-post?
"docstring"
[req]
(= (-> req :request-method) :post))

(defn not-found-handler
"docstring"
[req]
(-> (v/layout req [:h2 {:style "color: red"} "Page not found"])
(-> (v/layout req
[:div.container {:style "margin-top: 20px;"}
[:h3.is-size-3 {:style "color: red"} "Page not found"]])
(res/status 404)))


Expand Down Expand Up @@ -119,35 +114,39 @@
(assoc :ds (:ds opts))
(handler))))

(defn unauthorized-handler
"docstring"
[req _]
(resp/redirect (str "/auth/login?next=" (:uri req))))

(def backend (backends/session {:unauthorized-handler unauthorized-handler}))
(def authn-backend (backends/session {:unauthorized-handler unauthorized-handler}))


(def rules [{:pattern #"^/auth/.*"
:handler (constantly true)}
{:pattern #"^/.*"
:handler authenticated?}])
(def authz-rules [{:pattern #"^/auth/.*"
:handler (constantly true)}
{:pattern #"^/.*"
:handler authenticated?}])

(defn wrap-auth
"docstring"
[handler]
[handler backend rules]
(-> handler
(wrap-access-rules {:rules rules})
(wrap-authentication backend)
(wrap-authorization backend)))

(defn wrap-site-defaults
"docstring"
[handler opts]
(let [cookie-stor (cookie-store {:key (:cookie-secret opts)})
defaults (-> site-defaults
(assoc-in [:session :store] cookie-stor)
(assoc-in [:session :cookie-attrs :same-site] :lax))]
(wrap-defaults handler defaults)))


(defn new-handler
[opts]
(-> (make-handler routes)
(wrap-auth)
(wrap-auth authn-backend authz-rules)
(wrap-system opts)
(wrap-defaults (-> site-defaults
(assoc-in [:session :store] (cookie-store
{:key (:cookie-secret opts)}))
(assoc-in [:session :cookie-attrs :same-site] :lax)))
(wrap-site-defaults opts)
(wrap-cors :access-control-allow-origin #".*"
:access-control-allow-methods [:get :put :post :delete])
(wrap-reload)))
7 changes: 6 additions & 1 deletion src/myuri/web/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
(-> req :identity :id))

(defn parse-int [s]
(Integer. (re-find #"[0-9]*" s)))
(Integer. (re-find #"[0-9]*" s)))

(defn is-post?
"docstring"
[req]
(= (-> req :request-method) :post))
2 changes: 1 addition & 1 deletion src/myuri/web/views.clj
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
[req bookmarks]
(layout req
[:div.container {:style "margin-top: 20px;"}
[:h3.title.is-3 "Bookmarks"]
[:h3.is-size-3 "Bookmarks"]
(bookmarks-table req bookmarks)]

#_(pagination req)))
Expand Down

0 comments on commit 8c29645

Please sign in to comment.