Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
arttuka committed Mar 24, 2019
2 parents cd55479 + a7d5cae commit 5061837
Show file tree
Hide file tree
Showing 44 changed files with 2,028 additions and 1,309 deletions.
11 changes: 11 additions & 0 deletions mtg-pairings-server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/dev
/target
.project
*.log
*.iml
/.lein-*
/.nrepl-port
.rebel_readline_history
.classpath
.DS_Store
.git
8 changes: 7 additions & 1 deletion mtg-pairings-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ RUN curl -Lo /usr/bin/lein https://raw.githubusercontent.com/technomancy/leining
WORKDIR /app
COPY project.clj .
RUN lein deps
COPY ./scripts ./scripts
COPY ./src ./src
COPY ./env ./env
COPY ./resources ./resources
COPY ./test ./test
COPY ./test-resources ./test-resources
COPY prod.cljs.edn .
RUN lein do clean, uberjar
RUN lein test
RUN lein with-profile provided,prod do clean, garden once, minify-assets, fig:min
RUN ./scripts/tag-assets.sh
RUN lein uberjar

FROM openjdk:11-jre-slim
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion mtg-pairings-server/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ set -euo pipefail
version=$(git log --pretty=format:'%h' -n 1)
tag=arttuka/pairings:$version

docker build . -t $tag
docker build . -t $tag --network mtgsuomi-deployment_default
docker tag $tag arttuka/pairings:latest
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(org.joda.time LocalDate)))

(m/in-cljc-mode)
(timbre/swap-config! (fn [config] (assoc config :ns-whitelist ["mtg-pairings-server.*"])))
(timbre/swap-config! (fn [config] (assoc config :ns-blacklist ["org.eclipse.jetty.*" "io.netty.*" "com.zaxxer.hikari.*"])))
(json-gen/add-encoder LocalDate
(fn [c ^JsonGenerator generator]
(.writeString generator (str c))))
Expand Down
4 changes: 2 additions & 2 deletions mtg-pairings-server/prod.cljs.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
^{:watch-dirs ["src/cljs" "src/cljc" "env/prod/cljs"]}
{:main mtg-pairings-server.prod
:output-to "resources/public/js/prod-main.js"
:output-dir "resources/public/js/compiled"
:output-to "target/js/prod-main.js"
:output-dir "target/js/compiled"
:optimizations :advanced}
33 changes: 15 additions & 18 deletions mtg-pairings-server/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
[yogthos/config "1.1.1"]
[org.clojure/core.cache "0.7.2"]
[aleph "0.4.6"]
[org.clojure/tools.reader "1.3.2"]
[clj-time "0.15.1"]
[korma "0.4.3"]
[korma "0.4.3" :exclusions [com.mchange/c3p0]]
[mount "0.1.16"]
[org.postgresql/postgresql "42.2.5"]
[hikari-cp "2.7.1"]
[metosin/compojure-api "1.1.12"]
[org.flatland/ordered "1.5.7"]
[cheshire "5.8.1"]
Expand All @@ -23,19 +23,18 @@
[com.taoensso/timbre "4.10.0"]
[com.cognitect/transit-clj "0.8.313"]
[com.fzakaria/slf4j-timbre "0.3.12"]]
:plugins [[lein-asset-minifier "0.4.5"]
:plugins [[lein-asset-minifier "0.4.6"]
[lein-cljfmt "0.6.4"]
[lein-ancient "0.6.15"]
[lein-garden "0.3.0" :exclusions [org.apache.commons/commons-compress]]]

:uberjar-name "mtg-pairings.jar"

:clean-targets ^{:protect false} ["resources/public/js"
"resources/public/css/main.min.css"
"target"]
:clean-targets ^{:protect false} ["target"]

:source-paths ["src/clj" "src/cljc" "src/cljs"]
:resource-paths ["resources"]
:test-paths []

:garden {:builds [{:id "main"
:source-paths ["src/clj" "src/cljc"]
Expand All @@ -45,7 +44,7 @@

:minify-assets [[:css {:source ["target/public/css/main.css"
"resources/public/css/slider.css"]
:target "resources/public/css/main.min.css"}]]
:target "target/public/css/main.min.css"}]]

:aliases {"fig" ["trampoline" "run" "-m" "figwheel.main"]
"fig:min" ["run" "-m" "figwheel.main" "-bo" "prod"]}
Expand Down Expand Up @@ -74,6 +73,7 @@
:nrepl-middleware [cider.piggieback/wrap-cljs-repl]}
:source-paths ["dev" "env/dev/clj" "env/dev/cljs"]
:resource-paths ["target"]
:test-paths ["test/clj"]
:dependencies [[org.clojure/tools.namespace "0.2.11"]
[binaryage/devtools "0.9.10"]
[com.bhauman/rebel-readline-cljs "0.1.4" :exclusions [org.clojure/clojurescript]]
Expand All @@ -82,7 +82,9 @@
[prone "1.6.1"]
[hawk "0.2.11"]
[cider/piggieback "0.4.0" :exclusions [org.clojure/clojurescript]]
[re-frisk "0.5.4" :exclusions [org.clojure/clojurescript]]]}
[re-frisk "0.5.4.1" :exclusions [org.clojure/clojurescript]]]}
:test {:source-paths ^:replace ["src/clj" "src/cljc" "src/cljs"]
:resource-paths ["test-resources"]}
:prod {:source-paths ["env/prod/cljs"]}
:provided {:dependencies [[org.clojure/clojurescript "1.10.520"]
[reagent "0.8.1"]
Expand All @@ -94,21 +96,16 @@
[com.cognitect/transit-cljs "0.8.256"]
[com.andrewmcveigh/cljs-time "0.5.2"]
[re-frame "0.10.6" :exclusions [cljsjs/react org.clojure/clojurescript]]
[binaryage/oops "0.6.4"]
[cljsjs/react "16.8.1-0"]
[cljsjs/react-dom "16.8.1-0"]
[cljsjs/react-dom-server "16.8.1-0"]
[binaryage/oops "0.7.0"]
[cljsjs/react "16.8.3-0"]
[cljsjs/react-dom "16.8.3-0"]
[cljsjs/react-dom-server "16.8.3-0"]
[cljs-react-material-ui "0.2.50" :exclusions [org.clojure/clojurescript]]
[cljsjs/prop-types "15.6.2-0"]
[cljsjs/rc-slider "8.6.1-0"]
[garden "1.3.6"]]}
:uberjar {:source-paths ["env/prod/cljs"]
:main mtg-pairings-server.main
:aot :all
:prep-tasks ["compile"
["garden" "once"]
"minify-assets"
"fig:min"]
:uberjar-exclusions [#"public/js/compiled"
#"public/css/slider.css"]
:uberjar-exclusions [#"public/css/slider.css"]
:omit-source true}})
18 changes: 18 additions & 0 deletions mtg-pairings-server/scripts/tag-assets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -euo pipefail

mkdir -p resources/public/js
mkdir -p resources/public/css

jssha=$(sha1sum target/js/prod-main.js)
jsfile="js/prod-main.${jssha:0:8}.js"
cp target/js/prod-main.js "resources/public/$jsfile"

csssha=$(sha1sum target/public/css/main.min.css)
cssfile="css/main.${csssha:0:8}.css"
cp target/public/css/main.min.css "resources/public/$cssfile"

echo "{:main-css \"/$cssfile\"
:main-js \"/$jsfile\"}
" > resources/config.edn
14 changes: 12 additions & 2 deletions mtg-pairings-server/src/clj/mtg_pairings_server/api/http.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
(ns mtg-pairings-server.api.http
(:require [compojure.api.sweet :refer :all]
[compojure.api.exception :as ex]
[config.core :refer [env]]
[mtg-pairings-server.api.player :refer [player-routes]]
[mtg-pairings-server.api.tournament :refer [tournament-routes]]))
[mtg-pairings-server.api.tournament :refer [tournament-routes]]
[mtg-pairings-server.middleware.error :refer [request-validation-error-handler sql-error-handler]]
[mtg-pairings-server.util.sql :as sql-util]))

(defapi app
{:exceptions {:handlers {::ex/request-validation request-validation-error-handler
::sql-util/assertion sql-error-handler}}}
(swagger-routes
{:ui "/api-docs"
:spec "/swagger.json"
:data {:info {:title "WER pairings backend API"}}})
(context "/api" []
(context "/tournament" [] tournament-routes)
(context "/player" [] player-routes)))
(context "/player" [] player-routes)
(GET "/client-version" []
:no-doc true
{:status 200
:body {:version (env :client-version)}})))
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@
(validate-request sanctionid key
(add-teams sanctionid (:teams teams))
{:status 204}))
(POST "/:sanctionid/reset" []
:path-params [sanctionid :- s/Str]
:summary "Poistaa kaikki turnauksen tiedot lukuunottamatta itse turnausta"
:query-params [key :- String]
(validate-request sanctionid key
(reset-tournament sanctionid)
{:status 204}))
(DELETE "/:sanctionid" []
:path-params [sanctionid :- s/Str]
:summary "Poistaa kaikki turnauksen tiedot"
Expand Down
127 changes: 85 additions & 42 deletions mtg-pairings-server/src/clj/mtg_pairings_server/handler.clj
Original file line number Diff line number Diff line change
@@ -1,70 +1,113 @@
(ns mtg-pairings-server.handler
(:require [compojure.core :refer [GET POST defroutes] :as c]
(:require [compojure.api.sweet :refer :all]
[compojure.route :refer [not-found resources]]
[clojure.string :as str]
[config.core :refer [env]]
[hiccup.page :refer [include-js include-css html5]]
[schema.core :as s]
[taoensso.timbre :as log]
[ring.middleware.anti-forgery :refer [*anti-forgery-token*]]
[ring.middleware.jsonp :refer [wrap-json-with-padding]]
[mtg-pairings-server.api.http :as http-api]
[mtg-pairings-server.middleware :refer [wrap-site-middleware wrap-api-middleware]]
[mtg-pairings-server.middleware :refer [wrap-site-middleware]]
[mtg-pairings-server.middleware.cors :refer [wrap-allow-origin]]
[mtg-pairings-server.middleware.error :refer [wrap-errors]]
[mtg-pairings-server.middleware.log :refer [wrap-request-log]]
[mtg-pairings-server.service.tournament :as tournament]
[mtg-pairings-server.service.player :as player]
[mtg-pairings-server.transit :as transit]
[mtg-pairings-server.util.broadcast :as broadcast]
[mtg-pairings-server.websocket :as ws]))

(defn loading-page []
(let [html (html5
{:lang :fi}
[:head
[:title "Pairings.fi"]
[:meta {:charset "utf-8"}]
[:meta {:name "viewport"
:content "width=device-width, initial-scale=1"}]
(include-css (if (env :dev)
"/css/main.css"
"/css/main.min.css"))
(when (env :dev)
(include-css "/css/slider.css"))]
[:body {:class "body-container"}
[:div#app]
[:script (str "var csrf_token = '" *anti-forgery-token* "';")]
(include-js (if (env :dev)
"/js/dev-main.js"
"/js/prod-main.js"))])]
{:status 200
:body html
:headers {"Content-Type" "text/html"}}))
(defn escape-quotes [s]
(str/escape s {\' "\\'"}))

(defn loading-page
([]
(loading-page {}))
([initial-db]
(let [html (html5
{:lang :fi}
[:head
[:title "Pairings.fi"]
[:meta {:charset "utf-8"}]
[:meta {:name "viewport"
:content "width=device-width, initial-scale=1"}]
(include-css (if (env :dev)
"/css/main.css"
(env :main-css)))
(when (env :dev)
(include-css "/css/slider.css"))]
[:body {:class "body-container"}
[:div#app]
[:script (str "var csrf_token = '" *anti-forgery-token* "'; "
"var initial_db = '" (escape-quotes (transit/write initial-db)) "'; ")]
(include-js (if (env :dev)
"/js/dev-main.js"
(env :main-js)))])]
{:status 200
:body html
:headers {"Content-Type" "text/html"
"Cache-Control" "no-cache"}})))

(def robots-txt
{:status 200
:body "User-agent: *\nDisallow: /\n"
:headers {"Content-Type" "text/plain"}})

(defroutes site-routes
(GET "/" [] (loading-page))
(GET "/tournaments" [] (loading-page))
(GET "/tournaments/:id" [] (loading-page))
(GET "/tournaments/:id/pairings-:round" [] (loading-page))
(GET "/tournaments/:id/standings-:round" [] (loading-page))
(GET "/tournaments/:id/pods-:round" [] (loading-page))
(GET "/tournaments/:id/seatings" [] (loading-page))
(GET "/tournaments/:id/bracket" [] (loading-page))
(GET "/tournaments" []
(loading-page {:page {:page :tournaments}}))
(GET "/tournaments/:id" []
:path-params [id :- s/Int]
(loading-page {:page {:page :tournament, :id id}
:tournaments {id (tournament/client-tournament id)}}))
(GET "/tournaments/:id/pairings-:round" []
:path-params [id :- s/Int
round :- s/Int]
(loading-page {:page {:page :pairings, :id id, :round round}
:tournaments {id (tournament/client-tournament id)}
:pairings {id {round (tournament/get-round id round)}}}))
(GET "/tournaments/:id/standings-:round" []
:path-params [id :- s/Int
round :- s/Int]
(loading-page {:page {:page :standings, :id id, :round round}
:tournaments {id (tournament/client-tournament id)}
:standings {id {round (tournament/standings id round false)}}}))
(GET "/tournaments/:id/pods-:round" []
:path-params [id :- s/Int
round :- s/Int]
(loading-page {:page {:page :pods, :id id, :round round}
:tournaments {id (tournament/client-tournament id)}
:pods {id {round (tournament/pods id round)}}}))
(GET "/tournaments/:id/seatings" []
:path-params [id :- s/Int]
(loading-page {:page {:page :seatings, :id id}
:tournaments {id (tournament/client-tournament id)}
:seatings {id (tournament/seatings id)}}))
(GET "/tournaments/:id/bracket" []
:path-params [id :- s/Int]
(loading-page {:page {:page :bracket, :id id}
:tournaments {id (tournament/client-tournament id)}
:bracket {id (tournament/bracket id)}}))
(GET "/tournaments/:id/organizer" [] (loading-page))
(GET "/tournaments/:id/organizer/menu" [] (loading-page))
(GET "/tournaments/:id/organizer/deck-construction" [] (loading-page))
(GET "/robots.txt" [] robots-txt)
ws/routes)

(defroutes app-routes
(c/routes
(wrap-api-middleware http-api/app)
(wrap-site-middleware site-routes)
(wrap-site-middleware (resources "/"))
(wrap-site-middleware (not-found "Not Found"))))
http-api/app
(wrap-site-middleware
(routes
site-routes
(resources "/")
(not-found "Not Found"))))

(def app (-> app-routes
wrap-json-with-padding
wrap-request-log
wrap-allow-origin
wrap-errors))
wrap-allow-origin))

(defmethod ws/event-handler
:chsk/uidport-open
Expand All @@ -77,7 +120,7 @@
(defmethod ws/event-handler :client/connect
[{:keys [uid]}]
(log/debugf "New connection from %s" uid)
(ws/send! uid [:server/tournaments (tournament/tournaments)]))
(ws/send! uid [:server/tournaments (tournament/client-tournaments)]))

(defmethod ws/event-handler :client/login
[{uid :uid, dci-number :?data}]
Expand All @@ -97,11 +140,11 @@

(defmethod ws/event-handler :client/tournaments
[{:keys [uid]}]
(ws/send! uid [:server/tournaments (tournament/tournaments)]))
(ws/send! uid [:server/tournaments (tournament/client-tournaments)]))

(defmethod ws/event-handler :client/tournament
[{uid :uid, id :?data}]
(ws/send! uid [:server/tournament (tournament/tournament id)]))
(ws/send! uid [:server/tournament (tournament/client-tournament id)]))

(defmethod ws/event-handler :client/pairings
[{uid :uid, [id round] :?data}]
Expand Down
7 changes: 6 additions & 1 deletion mtg-pairings-server/src/clj/mtg_pairings_server/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
(timbre/merge-config! timbre-config)
(timbre/info "Starting mtg-pairings...")
(m/in-cljc-mode)
(m/start))
(try
(m/start)
(catch Throwable t
(timbre/error t "Error while starting server")
(m/stop)
(System/exit 1))))
Loading

0 comments on commit 5061837

Please sign in to comment.