Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gklijs committed May 15, 2019
0 parents commit 5dcc836
Show file tree
Hide file tree
Showing 90 changed files with 4,233 additions and 0 deletions.
102 changes: 102 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Created by .ignore support plugin (hsz.mobi)
### Clojure template
**/pom.xml
pom.xml.asc
/lib/
/classes/
/checkouts/
.lein-deps-sum
.lein-repl-history
.lein-plugins/
.lein-failures
.nrepl-port
### Kotlin template
# Compiled class file

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### Maven template
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties

# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
!/.mvn/wrapper/maven-wrapper.jar

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

.idea/
*.iml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# frontend
frontend/resources/public/js
frontend/resources/public/css
frontend/public/

# Mac
.DS_Store
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "frontend/bulma"]
path = frontend/bulma
url = https://github.com/jgthms/bulma.git
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
dist: xenial
language: clojure
lein: 2.9.1
jdk: openjdk11
addons:
apt:
sources:
- google-chrome
packages:
- chromium-browser=73.0.3683.86-0ubuntu0.16.04.1
- chromium-chromedriver
install:
- cd /opt
- git clone https://github.com/sass/libsass.git
- git clone https://github.com/sass/sassc.git libsass/sassc
- export SASS_LIBSASS_PATH=/opt/libsass
- cd libsass/sassc
- make
- ln -s /opt/libsass/sassc/bin/sassc ~/bin/sassc
before_script:
- cd /home/travis/build/gklijs/open-bank-mark
- "./prepare.sh"
- "./restart.sh"
script: "java -jar test/target/test.jar"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Gerard Klijs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
148 changes: 148 additions & 0 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker-compose -f docker-bank.yml -f docker-cluster.yml -f docker-prep.yml down
2 changes: 2 additions & 0 deletions command-generator/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**
!target/cg-docker.jar
6 changes: 6 additions & 0 deletions command-generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM openjdk:11-jre-slim
MAINTAINER Gerard Klijs <[email protected]>

ADD target/cg-docker.jar /app.jar

CMD ["java", "-jar", "/app.jar"]
8 changes: 8 additions & 0 deletions command-generator/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(defproject nl.openweb/command-generator "0.1.0-SNAPSHOT"
:plugins [[lein-modules "0.3.11"]]
:dependencies [[nl.openweb/topology :version]
[org.clojure/clojure :version]]
:main nl.openweb.command-generator.core
:profiles {:uberjar {:omit-source true
:aot :all
:uberjar-name "cg-docker.jar"}})
93 changes: 93 additions & 0 deletions command-generator/src/nl/openweb/command_generator/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
(ns nl.openweb.command-generator.core
(:require [clj-time.local :as time-local]
[nl.openweb.topology.clients :as clients]
[nl.openweb.topology.value-generator :as vg])
(:import (java.util UUID)
(org.apache.kafka.clients.consumer ConsumerRecord)
(nl.openweb.data AccountCreationConfirmed Uuid Atype ConfirmMoneyTransfer ConfirmAccountCreation))
(:gen-class))

(def heartbeat-topic (or (System/getenv "KAFKA_HEARTBEAT_TOPIC") "heartbeat"))
(def cac-topic (or (System/getenv "KAFKA_CAC_TOPIC") "confirm_account_creation"))
(def acc-topic (or (System/getenv "KAFKA_ACC_TOPIC") "account_creation_confirmed"))
(def cmt-topic (or (System/getenv "KAFKA_CMT_TOPIC") "confirm_money_transfer"))
(def client-id (or (System/getenv "KAFKA_CLIENT_ID") "command-generator"))

(def company-iban {:iban "NL66OPEN0000000000" :token "00000000000000000000"})
(def ah "Albert Heijn BV")
(def cash-amounts (mapv #(* 1000 %) [2 2 2 3 5 5 10 20]))
(def accounts (atom []))

(defn get-iban
[]
(if (not-empty @accounts)
(rand-nth @accounts)
company-iban))

(defn get-type
[]
(let [rand (rand-int 70)]
(cond
(zero? rand) :salary
(< rand 4) :deposit
(< rand 14) :withdraw
(< rand 54) :ah-pin
:else :something-else)))

(def description-map
{:salary "salary"
:deposit "deposit"
:withdraw "withdraw"
:ah-pin "ah pin"
:something-else "transfer"})

(defn get-description
[rand-helper]
(str (rand-helper description-map) " " (time-local/to-local-date-time (time-local/local-now))))

(defn create-transfer-map
([]
(let [some-type (get-type)
to (cond (= some-type :withdraw) "cash"
(= some-type :ah-pin) ah
:else (:iban (get-iban)))]
(create-transfer-map some-type to)))
([some-type to]
(let [from-iban (if (= some-type :salary) company-iban (get-iban))]
(-> {}
(assoc :id (vg/uuid->bytes (UUID/randomUUID)))
(assoc :token (if (= some-type :deposit) "cash" (:token from-iban)))
(assoc :amount (cond
(= some-type :salary) (+ 220000 (rand-int 50000))
(or (= some-type :deposit) (= some-type :withdraw)) (rand-nth cash-amounts)
:else (+ 1000 (rand-int 10000))))
(assoc :from (if (= some-type :deposit) "cash" (:iban from-iban)))
(assoc :to to)
(assoc :description (get-description some-type))))))

(defn get-cmt
[t]
(ConfirmMoneyTransfer. (Uuid. (:id t)) (:token t) (:amount t) (:from t) (:to t) (:description t)))

(defn add-account
[producer ^ConsumerRecord consumer-record]
(let [^AccountCreationConfirmed value (.value consumer-record)]
(when (= Atype/AUTO (.getAType value))
(swap! accounts conj (-> {}
(assoc :token (.getToken value))
(assoc :iban (.getIban value))))
(clients/produce producer cmt-topic (get-cmt (create-transfer-map :salary (.getIban value)))))))

(defn generate-command
[producer consumer-record]
(if-let [beat (.getBeat (.value consumer-record))]
(if
(or (< beat 20) (zero? (mod beat 200)))
(clients/produce producer cac-topic (ConfirmAccountCreation. (Uuid. (vg/uuid->bytes (UUID/randomUUID))) Atype/AUTO))
(clients/produce producer cmt-topic (get-cmt (create-transfer-map))))))

(defn -main
[]
(let [producer (clients/get-producer client-id)]
(clients/consume (str client-id "-hb") client-id heartbeat-topic (partial generate-command producer))
(clients/consume (str client-id "-acc") client-id acc-topic (partial add-account producer))))
2 changes: 2 additions & 0 deletions command-handler/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**
!target/ch-docker.jar
6 changes: 6 additions & 0 deletions command-handler/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM openjdk:11-jre-slim
MAINTAINER Gerard Klijs <[email protected]>

ADD target/ch-docker.jar /app.jar

CMD ["java", "-jar", "/app.jar"]
15 changes: 15 additions & 0 deletions command-handler/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(defproject nl.openweb/command-handler "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:plugins [[lein-modules "0.3.11"]]
:dependencies [[nl.openweb/topology :version]
[org.clojure/clojure :version]
[org.postgresql/postgresql :version]
[seancorfield/next.jdbc :version]
[hikari-cp/hikari-cp :version]]
:main nl.openweb.command-handler.core
:profiles {:uberjar {:omit-source true
:aot :all
:uberjar-name "ch-docker.jar"}})
60 changes: 60 additions & 0 deletions command-handler/src/nl/openweb/command_handler/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
(ns nl.openweb.command-handler.core
(:require [nl.openweb.command-handler.db :as db]
[nl.openweb.topology.clients :as clients]
[nl.openweb.topology.value-generator :as vg])
(:import (nl.openweb.data ConfirmAccountCreation AccountCreationConfirmed AccountCreationFailed MoneyTransferFailed ConfirmMoneyTransfer MoneyTransferConfirmed BalanceChanged))
(:gen-class))

(def cac-topic (or (System/getenv "KAFKA_CAC_TOPIC") "confirm_account_creation"))
(def acc-topic (or (System/getenv "KAFKA_ACC_TOPIC") "account_creation_confirmed"))
(def acf-topic (or (System/getenv "KAFKA_ACF_TOPIC") "account_creation_failed"))

(def cmt-topic (or (System/getenv "KAFKA_CMT_TOPIC") "confirm_money_transfer"))
(def mtc-topic (or (System/getenv "KAFKA_MTC_TOPIC") "money_transfer_confirmed"))
(def mtf-topic (or (System/getenv "KAFKA_MTF_TOPIC") "money_transfer_failed"))

(def bc-topic (or (System/getenv "KAFKA_BC_TOPIC") "balance_changed"))

(def client-id (or (System/getenv "KAFKA_CLIENT_ID") "command-handler"))

(defn handle-cac
[producer]
(fn [consumer-record]
(let [cac (.value consumer-record)
result (db/get-account (-> (.getId cac)
.bytes
vg/bytes->uuid) (.toString (.getAType cac)))]
(if (:reason result)
(clients/produce producer acf-topic (AccountCreationFailed. (.getId cac) (:reason result)))
(clients/produce producer acc-topic (AccountCreationConfirmed. (.getId cac) (:iban result) (:token result) (.getAType cac)))))))

(defn ->bc
[from ^ConfirmMoneyTransfer cmt balance-row]
(BalanceChanged.
(:balance/iban balance-row)
(:balance/amount balance-row)
(if from (- (.getAmount cmt)) (.getAmount cmt))
(if from (.getTo cmt) (.getFrom cmt))
(.getDescription cmt)))

(defn handle-cmt
[producer]
(fn [consumer-record]
(let [cmt (.value consumer-record)
result (db/transfer cmt)]
(if-let [from (:from result)]
(clients/produce producer bc-topic (->bc true cmt from)))
(if-let [to (:to result)]
(clients/produce producer bc-topic (->bc false cmt to)))
(if-let [reason (:reason result)]
(clients/produce producer mtf-topic (MoneyTransferFailed. (.getId cmt) reason))
(clients/produce producer mtc-topic (MoneyTransferConfirmed. (.getId cmt)))
))))

(defn -main
[]
(db/init)
(let [producer (clients/get-producer client-id)]
(clients/consume (str client-id "-cac") client-id cac-topic (handle-cac producer))
(clients/consume (str client-id "-cmt") client-id cmt-topic (handle-cmt producer))))

Loading

0 comments on commit 5dcc836

Please sign in to comment.