From 651c003d4e4c1348925c1ab6ee5609ebf9aee43e Mon Sep 17 00:00:00 2001 From: Eric Ihli Date: Wed, 23 Jun 2021 10:56:46 -0500 Subject: [PATCH] Add code related to rhyming and ssl --- Makefile | 8 +++- load-balancer/haproxy.cfg | 14 ++++++ util/refresh_keystore.sh | 43 +++++++++++++++++++ web/dev/user.clj | 6 ++- .../com/darklimericks/linguistics/core.clj | 22 ++++++++-- web/src/com/darklimericks/server/handlers.clj | 20 +++++---- .../com/darklimericks/server/limericks.clj | 14 ++---- web/src/com/darklimericks/server/models.clj | 12 ++++++ web/src/com/darklimericks/server/router.clj | 7 ++- web/src/com/darklimericks/server/views.clj | 5 ++- 10 files changed, 122 insertions(+), 29 deletions(-) create mode 100644 load-balancer/haproxy.cfg create mode 100755 util/refresh_keystore.sh create mode 100644 web/src/com/darklimericks/server/models.clj diff --git a/Makefile b/Makefile index fc1e38e..580b26c 100644 --- a/Makefile +++ b/Makefile @@ -10,13 +10,17 @@ darklimericks.jar: $(src) cd web && clj -A:depstar -M:depstar -m hf.depstar.uberjar \ darklimericks.jar -C -m com.darklimericks.server.core +FORCE: + build: FORCE cd web && clj -A:depstar -M:depstar -m hf.depstar.uberjar \ darklimericks.jar -C -m com.darklimericks.server.core -FORCE: - push: FORCE rsync -aP web/darklimericks.jar root@165.227.16.47:/root/darklimericks/web/ +certs: + ssh root@darklimericks.com \ + certbot certonly -d darklimericks.com \ + --webroot --webroot-path /root/darklimericks/web/resources/public --keep # end diff --git a/load-balancer/haproxy.cfg b/load-balancer/haproxy.cfg new file mode 100644 index 0000000..9bc7161 --- /dev/null +++ b/load-balancer/haproxy.cfg @@ -0,0 +1,14 @@ +defaults + timeout connect 10s + timeout client 30s + timeout server 30s + mode http + +frontend http-in + bind :80 + bind *:443 ssl crt /etc/ssl/cert.pem + redirect scheme https if !{ ssl_fc } + default_backend app + +backend app + server darklimericks darklimericks:8000 diff --git a/util/refresh_keystore.sh b/util/refresh_keystore.sh new file mode 100755 index 0000000..7320ca4 --- /dev/null +++ b/util/refresh_keystore.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +KEYSTORE_DOMAIN="${KEYSTORE_DOMAIN:-darklimericks.com}" +KEYSTORE_PASS="${KEYSTORE_PASS:-hunter22}" + +# Create keystore +echo "Refreshing '~/ssl/$KEYSTORE_DOMAIN.keystore'" +openssl pkcs12 -export \ + -in /etc/letsencrypt/live/$KEYSTORE_DOMAIN/cert.pem \ + -inkey /etc/letsencrypt/live/$KEYSTORE_DOMAIN/privkey.pem \ + -out /tmp/$KEYSTORE_DOMAIN.p12 \ + -name $KEYSTORE_DOMAIN \ + -CAfile /etc/letsencrypt/live/$KEYSTORE_DOMAIN/fullchain.pem \ + -caname "Let's Encrypt Authority X3" \ + -password pass:$KEYSTORE_PASS +keytool -importkeystore \ + -deststorepass $KEYSTORE_PASS \ + -destkeypass $KEYSTORE_PASS \ + -deststoretype pkcs12 \ + -srckeystore /tmp/$KEYSTORE_DOMAIN.p12 \ + -srcstoretype PKCS12 \ + -srcstorepass $KEYSTORE_PASS \ + -destkeystore /tmp/$KEYSTORE_DOMAIN.keystore \ + -alias $KEYSTORE_DOMAIN +# Move certificates to other servers +echo "Copy '~/ssl/$KEYSTORE_DOMAIN.keystore' to cluster servers" +cp /tmp/$KEYSTORE_DOMAIN.keystore /root/ssl/$KEYSTORE_DOMAIN.keystore + +# Create truststore +echo "Refreshing '~/ssl/theirdomain.be.keystore'" +rm theirdomain.be.keystore +openssl s_client -connect theirdomain.be:443 -showcerts /dev/null|openssl x509 -outform DER >theirdomain.der +openssl x509 -inform der -in theirdomain.der -out theirdomain.pem +keytool -import \ + -alias theirdomain \ + -keystore theirdomain.be.keystore \ + -file ./theirdomain.pem \ + -storepass theirdomain \ + -noprompt +echo "Copy '~/ssl/theirdomain.be.keystore' to cluster servers" +cp theirdomain.be.keystore /home/admin_jworks/ssl/ +sudo scp ssl/theirdomain.be.keystore cc-backend-node-02:/home/admin_jworks/ssl/ +sudo scp ssl/theirdomain.be.keystore cc-frontend-node-01:/home/admin_jworks/ssl/ diff --git a/web/dev/user.clj b/web/dev/user.clj index a039206..2bb4bb8 100644 --- a/web/dev/user.clj +++ b/web/dev/user.clj @@ -132,7 +132,11 @@ router (state/system :com.darklimericks.server.router/router)] (handler {:params {:rhyme-target "foo"} ::reitit/router router})) - + + (let [router (state/system :com.darklimericks.server.router/router)] + (reitit/match-by-path router "/.well-known/foo.txt")) + + (db.albums/num-albums (-> state/system :database.sql/connection)) diff --git a/web/src/com/darklimericks/linguistics/core.clj b/web/src/com/darklimericks/linguistics/core.clj index 89b5f63..4e70187 100644 --- a/web/src/com/darklimericks/linguistics/core.clj +++ b/web/src/com/darklimericks/linguistics/core.clj @@ -1,16 +1,19 @@ (ns com.darklimericks.linguistics.core (:require [com.owoga.prhyme.data.dictionary :as dict] + [com.owoga.prhyme.core :as prhyme] + [com.darklimericks.server.models :as models] + [com.owoga.corpus.markov :as markov] [clojure.string :as string])) (defn gen-artist [] - (->> [(rand-nth (seq dict/adverbs)) + (->> [(rand-nth (seq dict/adjectives)) (rand-nth (seq dict/nouns))] (map string/capitalize) (string/join " "))) (defn gen-album [] - (->> [(rand-nth (seq dict/adjectives)) - (rand-nth (seq dict/nouns))] + (->> [(rand-nth (seq dict/adverbs)) + (rand-nth (seq dict/verbs))] (map string/capitalize) (string/join " "))) @@ -18,4 +21,15 @@ (defn rhymes "All rhymes. Slightly flexible. Ordered by number of rhyming syllables. Most generic and likely desired rhyming algorithm." - [target]) + [target] + (->> (prhyme/phrase->all-flex-rhyme-tailing-consonants-phones target) + (map first) + (map reverse) + (mapcat (partial markov/rhyme-choices models/rhyme-trie)) + (sort-by (comp - count first)) + (mapcat second))) + +(comment + (rhymes "food") + + ) diff --git a/web/src/com/darklimericks/server/handlers.clj b/web/src/com/darklimericks/server/handlers.clj index 135306b..b3e6f98 100644 --- a/web/src/com/darklimericks/server/handlers.clj +++ b/web/src/com/darklimericks/server/handlers.clj @@ -16,7 +16,8 @@ [com.darklimericks.db.limericks :as db.limericks] [com.darklimericks.db.artists :as db.artists] [com.darklimericks.server.views :as views] - [com.darklimericks.server.limericks :as limericks])) + [com.darklimericks.server.limericks :as limericks] + [com.darklimericks.linguistics.core :as linguistics])) (defmethod ig/init-key ::handler [_ {:keys [router]}] (http/ring-handler @@ -264,10 +265,13 @@ (defn show-rhyme-suggestion [db cache] (fn [request] - {:status 201 - :headers {"Content-Type" "text/html; charset=utf-8"} - :body (views/wrapper - db - request - {} - (views/show-rhyme-suggestion request))})) + (let [suggestions (linguistics/rhymes (:rhyme-target (:params request)))] + {:status 201 + :headers {"Content-Type" "text/html; charset=utf-8"} + :body (views/wrapper + db + request + {} + (views/show-rhyme-suggestion + request + suggestions))}))) diff --git a/web/src/com/darklimericks/server/limericks.clj b/web/src/com/darklimericks/server/limericks.clj index f293a1c..4200312 100644 --- a/web/src/com/darklimericks/server/limericks.clj +++ b/web/src/com/darklimericks/server/limericks.clj @@ -2,6 +2,7 @@ (:require [clojure.string :as string] [clojure.java.io :as io] [reitit.core :as reitit] + [com.darklimericks.server.models :as models] [com.darklimericks.db.artists :as artists] [com.darklimericks.db.albums :as albums] [com.darklimericks.db.limericks :as db.limericks] @@ -12,7 +13,6 @@ [com.owoga.corpus.markov :as markov] [com.owoga.prhyme.data-transform :as data-transform] [com.owoga.tightly-packed-trie.encoding :as encoding] - [taoensso.nippy :as nippy] [taoensso.timbre :as timbre])) (defn parse-scheme-element [[tokens ctx]] @@ -104,12 +104,6 @@ (map string/capitalize) (string/join " "))) -(def database (nippy/thaw-from-resource "models/database.bin")) -(def rhyme-trie (into (trie/make-trie) (nippy/thaw-from-resource "models/rhyme-trie.bin"))) -(def markov-trie (tpt/load-tightly-packed-trie-from-file - (io/resource "models/tpt.bin") - (markov/decode-fn database))) - (comment (->> (markov/rhyme-from-scheme '[[A 9] [A 9] [B 6] [B 6] [A 9]] @@ -127,9 +121,9 @@ (let [{:keys [scheme session-id]} message limerick (->> (markov/rhyme-from-scheme scheme - database - markov-trie - rhyme-trie) + models/database + models/markov-trie + models/rhyme-trie) (map reverse) (map (partial map second)) (map data-transform/untokenize)) diff --git a/web/src/com/darklimericks/server/models.clj b/web/src/com/darklimericks/server/models.clj new file mode 100644 index 0000000..fb0c15b --- /dev/null +++ b/web/src/com/darklimericks/server/models.clj @@ -0,0 +1,12 @@ +(ns com.darklimericks.server.models + (:require [taoensso.nippy :as nippy] + [com.owoga.trie :as trie] + [com.owoga.tightly-packed-trie :as tpt] + [clojure.java.io :as io] + [com.owoga.corpus.markov :as markov])) + +(def database (nippy/thaw-from-resource "models/database.bin")) +(def rhyme-trie (into (trie/make-trie) (nippy/thaw-from-resource "models/rhyme-trie.bin"))) +(def markov-trie (tpt/load-tightly-packed-trie-from-file + (io/resource "models/tpt.bin") + (markov/decode-fn database))) diff --git a/web/src/com/darklimericks/server/router.clj b/web/src/com/darklimericks/server/router.clj index 06cc94d..1ee3d2c 100644 --- a/web/src/com/darklimericks/server/router.clj +++ b/web/src/com/darklimericks/server/router.clj @@ -5,7 +5,8 @@ [reitit.coercion.spec] [reitit.http.interceptors.parameters :refer [parameters-interceptor]] [com.darklimericks.server.handlers :as handlers] - [com.darklimericks.server.interceptors :as interceptors])) + [com.darklimericks.server.interceptors :as interceptors] + [reitit.ring :as ring])) (defmethod ig/init-key ::router [_ {:keys [db cache]}] (let [routes [["/" {:name ::home @@ -36,7 +37,9 @@ ["/wgu" {:name ::wgu :get {:handler (handlers/wgu db cache)} - :post {:handler (handlers/show-rhyme-suggestion db cache)}}]]] + :post {:handler (handlers/show-rhyme-suggestion db cache)}}] + ["/.well-known/*" (ring/create-file-handler + {:root "resources/public/.well-known"})]]] (timbre/info "Starting router.") (http/router routes diff --git a/web/src/com/darklimericks/server/views.clj b/web/src/com/darklimericks/server/views.clj index 5fa8d1d..09f297a 100644 --- a/web/src/com/darklimericks/server/views.clj +++ b/web/src/com/darklimericks/server/views.clj @@ -207,7 +207,8 @@ "Show rhyme suggestions"))]) (defn show-rhyme-suggestion - [request] + [request suggestions] [:div (wgu request) - [:div "Hi"]]) + (for [suggestion suggestions] + [:div suggestion])])