Skip to content

Commit

Permalink
Clean up and document installation process
Browse files Browse the repository at this point in the history
  • Loading branch information
bzg committed Jun 1, 2019
1 parent 81bf80e commit 4b776c0
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 110 deletions.
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# License-Filename: LICENSES/EPL-2.0.txt

FROM java:8-alpine
ENV SUBSCRIBE_CONFIG ${SUBSCRIBE_CONFIG}
ADD target/subscribe-0.2.0-standalone.jar /subscribe/subscribe-0.2.0-standalone.jar
CMD ["java", "-jar", "/subscribe/subscribe-0.2.0-standalone.jar"]
ENV MAILGUN_API_KEY ${MAILGUN_API_KEY}
ENV MAILGUN_LOGIN ${MAILGUN_LOGIN}
ENV MAILGUN_PASSWORD ${MAILGUN_PASSWORD}
ENV SUBSCRIBE_PORT ${SUBSCRIBE_PORT}
ENV SUBSCRIBE_BASEURL ${SUBSCRIBE_BASEURL}
ADD target/subscribe-0.3.0-standalone.jar /subscribe/subscribe-0.3.0-standalone.jar
CMD ["java", "-jar", "/subscribe/subscribe-0.3.0-standalone.jar"]
53 changes: 33 additions & 20 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@ webpages where users can (un)subscribe (from)to your mailing lists.

* Configure

1. Set =SUBSCRIBE_CONFIG= in your =~/.profile= file (or any other relevant
file) to the path of your configuration file.
** Setup environment variables

2. Add a configuration in this configuration file:
Add this to your ~./profile~ file:

: {:mailgun-api-key "your_api_key"
: :mailgun-login "[email protected]"
: :mailgun-password "your_mailgun_password"
: :mailgun-from "your_from_address"
: export MAILGUN_API_KEY="your-key"
: export MAILGUN_LOGIN="postmaster@xxx"
: export MAILGUN_PASSWORD="your-password"
: export SUBSCRIBE_PORT=3000
: export SUBSCRIBE_BASEURL="http://yourdomain.com"

** Optionally define more options in ~resources/config.edn~

: {:from "your_from_address"
: :locale "en-GB" ; or "fr-FR" etc.
: :admin-email "[email protected]"
: :base-url "http://yoursite.com"
: :return-url "https://yoursite.com"
: :team "Your team name"
: :port 3000 ; must be an integer
: :db-uri "" ; optional
: :log-file "" ; optional
: :lists-exclude-regexp nil ; A regexp or nil
: :lists-include-regexp nil ; A regexp or nil
: :warn-every-x-subscribers 10}
: :tos-url "https://yoursite.com/tos"
: :port 3000 ; an integer
: :db-uri "" ; local database
: :log-file "" ; log file name
: :lists-exclude-regexp nil ; regexp
: :lists-include-regexp nil ; regexp
: :warn-every-x-subscribers 10 ; an integer}

* Test

Expand All @@ -40,7 +44,19 @@ application.

* Run

** With =lein run=
** With docker

Assuming your environments variables are stored in ~~/.subscribe_envs~
and you want to expose the 3000 port:

: ~$ git clone https://github.com/etalab/subscribe
: ~$ cd subscribe/
: ~$ docker build -t subscribe .
: ~$ docker run -it -p 3000:3000 --env-file=~/.subscribe_envs subscribe

Then go to http://localhost:3000.

** With ~lein run~

: ~$ git clone https://github.com/etalab/subscribe
: ~$ cd subscribe/
Expand All @@ -57,7 +73,7 @@ Then go to http://localhost:3000 or to your custom base URL.
: ~$ lein uberjar
: ~$ java -jar target/subscribe-x.x.x-standalone.jar

* Todo
* TODOs

- [ ] Catch errors before redirecting to the confirmation pages

Expand All @@ -66,9 +82,6 @@ Then go to http://localhost:3000 or to your custom base URL.
If you like Clojure(script), please consider supporting its
development developers by donating to [[https://www.clojuriststogether.org][clojuriststogether.org]].

Thanks!

* License

=subscribe= is licensed under the [[http://www.eclipse.org/legal/epl-v10.html][Eclipse Public License 1.0]], the same
as Clojure.
=subscribe= is licensed under the [[http://www.eclipse.org/legal/epl-v10.html][Eclipse Public License 2.0]].
18 changes: 0 additions & 18 deletions config_example.edn

This file was deleted.

4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; License-Filename: LICENSES/EPL-2.0.txt

(defproject
subscribe "0.2.0"
subscribe "0.3.0"
:url "https://github.com/etalab/subscribe"
:license {:name "Eclipse Public License v2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
Expand All @@ -22,7 +22,7 @@
[com.draines/postal "2.0.3"]
[com.taoensso/timbre "4.10.0"]
[commons-validator "1.6"]]
:description "Subscribe to a mailgun mailing list."
:description "Web app to subscribe to mailgun mailing lists."
:main subscribe.handler
:jvm-opts ["-Xmx500m"]
:profiles {:uberjar {:aot :all}})
11 changes: 11 additions & 0 deletions resources/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{;; :from "postmaster@yourdomain"
;; :team "The name of my team"
;; :locale "gb-EN"
;; :admin-email "[email protected]"
;; :tos-url "http://localhost/tos"
;; :db-uri "datahike:mem:///subscribe"
;; :lists-exclude-regexp #""
;; :lists-include-regexp #""
;; :warn-every-x-subscribers 10
;; :ui-strings {:mailing-lists "Title for this website"}
}
50 changes: 22 additions & 28 deletions src/subscribe/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,30 @@
(ns subscribe.config
(:require [clojure.java.io :as io]))

(defn config []
(if-let [config-file (not-empty (System/getenv "SUBSCRIBE_CONFIG"))]
(if (.exists (io/file config-file))
(read-string (slurp config-file))
(throw (Exception. "Can't read configuration file")))
(throw (Exception. "Missing SUBSCRIBE_CONFIG environment variable"))))

;; Mailgun variables
;; Mailgun constants
(def mailgun-api-url "https://api.mailgun.net/v3")
(def mailgun-host "smtp.mailgun.org")
(def mailgun-lists-endpoint "/lists/pages")
(defn mailgun-subscribe-endpoint [list]
(str "/lists/" list "/members"))

;; Mailgun personal configuration
(def mailgun-api-key (:mailgun-api-key (config)))
(def mailgun-login (:mailgun-login (config)))
(def mailgun-password (:mailgun-password (config)))
(def mailgun-from (or (:mailgun-from (config)) mailgun-login))
;; Mailgun environment variables
(def mailgun-api-key (System/getenv "MAILGUN_API_KEY"))
(def mailgun-login (System/getenv "MAILGUN_LOGIN"))
(def mailgun-password (System/getenv "MAILGUN_PASSWORD"))
(def port (read-string (or (System/getenv "SUBSCRIBE_PORT") "3000")))
(def base-url (or (System/getenv "SUBSCRIBE_BASEURL")
(str "http://localhost:" port)))

;;Configuration from your config file
(def locale (:locale (config)))
(def ui-strings (:ui-strings (config)))
(def team (:team (config)))
(def admin-email (:admin-email (config)))
(def port (or (:port (config)) 3000))
(def base-url (or (:base-url (config)) (str "http://localhost:" port)))
(def return-url (or (:return-url (config)) base-url))
(def warn-every-x-subscribers (or (:warn-every-x-subscribers (config)) 100))
(def lists-exclude-regexp (or (:lists-exclude-regexp (config)) #""))
(def lists-include-regexp (or (:lists-include-regexp (config)) #".*"))
(def db-uri (or (not-empty (:db-uri (config))) "datahike:mem:///subscribe"))
(def log-file (or (not-empty (:log-file (config))) "log.txt"))
;; Configuration from your config.edn file
(def config (read-string (slurp (io/resource "config.edn"))))
(def from (or (:from config) mailgun-login))
(def locale (:locale config))
(def ui-strings (:ui-strings config))
(def team (:team config))
(def return-url (or (:return-url config) base-url))
(def tos-url (:tos-url config))
(def admin-email (or (:admin-email config) from mailgun-login))
(def warn-every-x-subscribers (or (:warn-every-x-subscribers config) 100))
(def lists-exclude-regexp (or (:lists-exclude-regexp config) #""))
(def lists-include-regexp (or (:lists-include-regexp config) #".*"))
(def db-uri (or (not-empty (:db-uri config)) "datahike:mem:///subscribe"))
(def log-file (or (not-empty (:log-file config)) "log.txt"))
13 changes: 8 additions & 5 deletions src/subscribe/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
^{:host config/mailgun-host
:user config/mailgun-login
:pass config/mailgun-password}
{:from config/mailgun-from
{:from config/from
:to config/admin-email})}})

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand All @@ -45,6 +45,9 @@
(d/create-database config/db-uri)
(def db-conn (d/connect config/db-uri))

(defn mailgun-subscribe-endpoint [list]
(str "/lists/" list "/members"))

(defn increment-subscribers
"Increment the count of new subscribers to a mailing list.
Send an email every X new subscribers, X being defined by
Expand Down Expand Up @@ -136,7 +139,7 @@
:port 587
:user config/mailgun-login
:pass config/mailgun-password}
{:from config/mailgun-from
{:from config/from
:to email
:subject subject
:body (str (i18n [:opening])
Expand Down Expand Up @@ -177,7 +180,7 @@
(try
(let [req (http/delete
(str config/mailgun-api-url
(config/mailgun-subscribe-endpoint mailing-list)
(mailgun-subscribe-endpoint mailing-list)
"/" subscriber)
{:basic-auth ["api" config/mailgun-api-key]})]
{:message (:message (json/parse-string (:body req) true))
Expand All @@ -193,7 +196,7 @@
(try
(let [req (http/post
(str config/mailgun-api-url
(config/mailgun-subscribe-endpoint mailing-list))
(mailgun-subscribe-endpoint mailing-list))
{:basic-auth ["api" config/mailgun-api-key]
:form-params {:address subscriber :name name}})]
{:message (:message (json/parse-string (:body req) true))
Expand All @@ -208,7 +211,7 @@
[email-and-list]
(let [subscriber (get email-and-list "subscriber")
mailing-list (get email-and-list "mailing-list")
endpoint (config/mailgun-subscribe-endpoint mailing-list)]
endpoint (mailgun-subscribe-endpoint mailing-list)]
(try
(let [req (http/get
(str config/mailgun-api-url endpoint "/" subscriber)
Expand Down
6 changes: 5 additions & 1 deletion src/subscribe/i18n.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
:unsubscribed-from "Unsubscribed from the %s mailing list"
:mailing-lists "Mailing lists"
:subscribe-button "Subscribe"
:made-with "Made with"
:tos "Terms of service"
:unsubscribe-button "Unsubscribe"
:regenerate-token "The token of %s for %s has been regenerated."
:return-to-site "Click here to return to our website."
Expand Down Expand Up @@ -56,6 +58,8 @@
:subscribed-to "Inscription à la liste %s"
:unsubscribed-from "Désinscription de la liste %s"
:subscribe-button "Inscription"
:made-with "Fait avec"
:tos "Conditions générales d'utilisation"
:unsubscribe-button "Désincription"
:regenerate-token "Le jeton de %s pour la liste %s a été renouvelé."
:return-to-site "Cliquez pour revenir au site."
Expand All @@ -77,7 +81,7 @@
(merge (val locale) config/ui-strings)})
localization)))

(def lang (keyword (or (not-empty (:locale (config/config))) "en-GB")))
(def lang (keyword (or (not-empty (:locale config/config)) "en-GB")))

(def opts {:dict localization-custom})

Expand Down
7 changes: 5 additions & 2 deletions src/subscribe/views.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
content]]]
[:footer {:class "footer"}
[:div {:class "content has-text-centered"}
[:p "Made with " [:a {:href "https://github.com/bzg/subscribe"
:target "new"} "Subscribe"]]]]))
(if config/tos-url
[:p [:a {:href config/tos-url :target "new"} (i18n [:tos])]])
[:p (i18n [:made-with]) " "
[:a {:href "https://github.com/bzg/subscribe"
:target "new"} "Subscribe"]]]]))

(defn error []
(default
Expand Down
50 changes: 19 additions & 31 deletions test/subscribe/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,38 @@
(defn regexp? [re-str]
(= (re-pattern re-str) re-str))

(deftest test-config-exists
(testing "Checking if SUBSCRIBE_CONFIG points to an existing file."
(is (.exists (io/file (System/getenv "SUBSCRIBE_CONFIG"))))))
(deftest test-environment-variables
(testing "Checking if all environment variables contain strings."
(is (and (string? (System/getenv "MAILGUN_API_KEY"))
(string? (System/getenv "MAILGUN_LOGIN"))
(string? (System/getenv "MAILGUN_PASSWORD"))
(string? (System/getenv "SUBSCRIBE_PORT"))
(string? (System/getenv "SUBSCRIBE_BASEURL"))))))

(deftest test-lists-exists
(testing "Checking mailgun connection and existing list(s)."
(is (boolean (not-empty (get-lists-from-server))))))

;; Mandatory configuration keys
(s/def ::mailgun-api-key string?)
(s/def ::mailgun-login string?)
(s/def ::mailgun-password string?)
(s/def ::mailgun-from string?)
(s/def ::base-url valid-url?)
;; Configuration keys
(s/def ::from string?)
(s/def ::return-url valid-url?)
(s/def ::admin-email string?)

;; Optional configuration keys
(s/def ::port int?)
(s/def ::locale string?)
(s/def ::team string?)
(s/def ::db-uri (s/nilable string?))
(s/def ::log-file (s/nilable string?))
(s/def ::lists-exclude-regexp (s/nilable regexp?))
(s/def ::lists-include-regexp (s/nilable regexp?))
(s/def ::warn-every-x-subscribers (s/nilable int?))
(s/def ::tos-url valid-url?)
(s/def ::db-uri string?)
(s/def ::log-file string?)
(s/def ::lists-exclude-regexp regexp?)
(s/def ::lists-include-regexp regexp?)
(s/def ::warn-every-x-subscribers int?)

(s/def ::config
(s/keys :req-un [::mailgun-api-key
::mailgun-login
::mailgun-password
::mailgun-from
::base-url
::return-url
::admin-email]
:opt-un [::locale
::team
::log-file
::port
::db-uri
::lists-exclude-regexp
::lists-include-regexp
(s/keys :opt-un [::from ::return-url ::admin-email ::tos-url
::locale ::team ::log-file ::port ::db-uri
::lists-exclude-regexp ::lists-include-regexp
::warn-every-x-subscribers]))

(deftest test-config-specs
(testing "Checking entries in the configuration map."
(is (s/valid? ::config (config/config)))))
(is (s/valid? ::config config/config))))

0 comments on commit 4b776c0

Please sign in to comment.