diff --git a/README.md b/README.md index bbd08c6..a3a9a1f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ # dda-masto-embed Embeds mastodon timline into a html page. Uses JS, no intermediate server required. + + +# Setup + +To setup a basic luminus project: +* lein new luminus shipping-cljs +kee-frame + +Start a leiningen repl and run fighwheel and let it connect to your application: + +1) lein repl + * user=> (start) +2) lein figwheel +3) Open http://localhost:3000/ in Browser +4) figwheel should have connected to your application + +# Development + +lein repl does not seem to work with clojurescript. Lein figwheel is needed to work with clojurescript. + +E.g. in your figwheel repl: + + + +app:cljs.user=> (in-ns 'masto-embed.routing) +app:masto-embed.routing=> routes + +But will lead to Unable to resolve symbol: routes in this context +in lein repl. + diff --git a/masto-embed/.gitignore b/masto-embed/.gitignore new file mode 100644 index 0000000..0d2092a --- /dev/null +++ b/masto-embed/.gitignore @@ -0,0 +1,16 @@ +/target +/lib +/classes +/checkouts +pom.xml +dev-config.edn +test-config.edn +*.jar +*.class +/.lein-* +profiles.clj +/.env +.nrepl-port + +/node_modules +/log diff --git a/masto-embed/.rebel_readline_history b/masto-embed/.rebel_readline_history new file mode 100644 index 0000000..ff8fa05 --- /dev/null +++ b/masto-embed/.rebel_readline_history @@ -0,0 +1,3 @@ +1587644075446:routes +1587644226715:(in-ns 'masto-embed.routing) +1587644229236:routes diff --git a/masto-embed/.vscode/settings.json b/masto-embed/.vscode/settings.json new file mode 100644 index 0000000..b774d23 --- /dev/null +++ b/masto-embed/.vscode/settings.json @@ -0,0 +1,53 @@ +{ + "calva.customREPLCommandSnippets": [ + { + "name": "Start masto-embed Server", + "ns": "user", + "repl": "clj", + "snippet": "(start)" + }, + { + "name": "Stop masto-embed Server", + "ns": "user", + "repl": "clj", + "snippet": "(stop)" + }, + { + "name": "Restart masto-embed Server", + "ns": "user", + "repl": "clj", + "snippet": "(restart)" + } + ], + "calva.replConnectSequences": [ + { + "name": "Server only - masto-embed", + "projectType": "Leiningen", + "afterCLJReplJackInCode": "(start)", + "cljsType": "none", + "menuSelections": { + "leinProfiles": [ + "dev" + ] + } + }, + { + "name": "Server + Client – masto-embed", + "projectType": "Leiningen", + "afterCLJReplJackInCode": "(start)", + "cljsType": { + "dependsOn": "lein-figwheel", + "connectCode": "(do (println (str \"Starting Fighweel. Client URL is http://127.0.0.1:\" (:port (clojure.edn/read-string (slurp \"dev-config.edn\"))))) (use 'figwheel-sidecar.repl-api) (when-not (figwheel-sidecar.repl-api/figwheel-running?) (figwheel-sidecar.repl-api/start-figwheel!)) (figwheel-sidecar.repl-api/cljs-repl))", + "isConnectedRegExp": "To quit, type: :cljs/quit", + "openUrlRegExp": "Client URL is (?\\S+)", + "shouldOpenUrl": true, + "isReadyToStartRegExp": "Prompt will show" + }, + "menuSelections": { + "leinProfiles": [ + "dev" + ] + } + } + ] +} diff --git a/masto-embed/Capstanfile b/masto-embed/Capstanfile new file mode 100644 index 0000000..7715479 --- /dev/null +++ b/masto-embed/Capstanfile @@ -0,0 +1,28 @@ + +# +# Name of the base image. Capstan will download this automatically from +# Cloudius S3 repository. +# +#base: cloudius/osv +base: cloudius/osv-openjdk8 + +# +# The command line passed to OSv to start up the application. +# +cmdline: /java.so -jar /masto-embed/app.jar + +# +# The command to use to build the application. +# You can use any build tool/command (make/rake/lein/boot) - this runs locally on your machine +# +# For Leiningen, you can use: +#build: lein uberjar +# For Boot, you can use: +#build: boot build + +# +# List of files that are included in the generated image. +# +files: + /masto-embed/app.jar: ./target/uberjar/masto-embed.jar + diff --git a/masto-embed/Dockerfile b/masto-embed/Dockerfile new file mode 100644 index 0000000..befbffc --- /dev/null +++ b/masto-embed/Dockerfile @@ -0,0 +1,7 @@ +FROM openjdk:8-alpine + +COPY target/uberjar/masto-embed.jar /masto-embed/app.jar + +EXPOSE 3000 + +CMD ["java", "-jar", "/masto-embed/app.jar"] diff --git a/masto-embed/Procfile b/masto-embed/Procfile new file mode 100644 index 0000000..b1e1020 --- /dev/null +++ b/masto-embed/Procfile @@ -0,0 +1 @@ +web: java -Dclojure.main.report=stderr -cp target/uberjar/masto-embed.jar clojure.main -m masto-embed.core diff --git a/masto-embed/README.md b/masto-embed/README.md new file mode 100644 index 0000000..dfb3fc8 --- /dev/null +++ b/masto-embed/README.md @@ -0,0 +1,21 @@ +# masto-embed + +generated using Luminus version "3.67" + +FIXME + +## Prerequisites + +You will need [Leiningen][1] 2.0 or above installed. + +[1]: https://github.com/technomancy/leiningen + +## Running + +To start a web server for the application, run: + + lein run + +## License + +Copyright © 2020 FIXME diff --git a/masto-embed/env/dev/clj/masto_embed/dev_middleware.clj b/masto-embed/env/dev/clj/masto_embed/dev_middleware.clj new file mode 100644 index 0000000..e970eb3 --- /dev/null +++ b/masto-embed/env/dev/clj/masto_embed/dev_middleware.clj @@ -0,0 +1,11 @@ +(ns masto-embed.dev-middleware + (:require + [ring.middleware.reload :refer [wrap-reload]] + [selmer.middleware :refer [wrap-error-page]] + [prone.middleware :refer [wrap-exceptions]])) + +(defn wrap-dev [handler] + (-> handler + wrap-reload + wrap-error-page + (wrap-exceptions {:app-namespaces ['masto-embed]}))) diff --git a/masto-embed/env/dev/clj/masto_embed/env.clj b/masto-embed/env/dev/clj/masto_embed/env.clj new file mode 100644 index 0000000..3d370a4 --- /dev/null +++ b/masto-embed/env/dev/clj/masto_embed/env.clj @@ -0,0 +1,15 @@ +(ns masto-embed.env + (:require + [selmer.parser :as parser] + [clojure.tools.logging :as log] + [masto-embed.dev-middleware :refer [wrap-dev]])) + +(def defaults + {:init + (fn [] + (parser/cache-off!) + (log/info "\n-=[masto-embed started successfully using the development profile]=-")) + :stop + (fn [] + (log/info "\n-=[masto-embed has shut down successfully]=-")) + :middleware wrap-dev}) diff --git a/masto-embed/env/dev/clj/masto_embed/figwheel.clj b/masto-embed/env/dev/clj/masto_embed/figwheel.clj new file mode 100644 index 0000000..da9f2c9 --- /dev/null +++ b/masto-embed/env/dev/clj/masto_embed/figwheel.clj @@ -0,0 +1,12 @@ +(ns masto-embed.figwheel + (:require [figwheel-sidecar.repl-api :as ra])) + +(defn start-fw [] + (ra/start-figwheel!)) + +(defn stop-fw [] + (ra/stop-figwheel!)) + +(defn cljs [] + (ra/cljs-repl)) + diff --git a/masto-embed/env/dev/clj/user.clj b/masto-embed/env/dev/clj/user.clj new file mode 100644 index 0000000..f8ebb9c --- /dev/null +++ b/masto-embed/env/dev/clj/user.clj @@ -0,0 +1,33 @@ +(ns user + "Userspace functions you can run by default in your local REPL." + (:require + [masto-embed.config :refer [env]] + [clojure.pprint] + [clojure.spec.alpha :as s] + [expound.alpha :as expound] + [mount.core :as mount] + [masto-embed.figwheel :refer [start-fw stop-fw cljs]] + [masto-embed.core :refer [start-app]])) + +(alter-var-root #'s/*explain-out* (constantly expound/printer)) + +(add-tap (bound-fn* clojure.pprint/pprint)) + +(defn start + "Starts application. + You'll usually want to run this on startup." + [] + (mount/start-without #'masto-embed.core/repl-server)) + +(defn stop + "Stops application." + [] + (mount/stop-except #'masto-embed.core/repl-server)) + +(defn restart + "Restarts application." + [] + (stop) + (start)) + + diff --git a/masto-embed/env/dev/cljs/masto_embed/app.cljs b/masto-embed/env/dev/cljs/masto_embed/app.cljs new file mode 100644 index 0000000..e0d84a8 --- /dev/null +++ b/masto-embed/env/dev/cljs/masto_embed/app.cljs @@ -0,0 +1,19 @@ +(ns^:figwheel-no-load masto-embed.app + (:require + [masto-embed.core :as core] + [cljs.spec.alpha :as s] + [expound.alpha :as expound] + [devtools.core :as devtools])) + +(extend-protocol IPrintWithWriter + js/Symbol + (-pr-writer [sym writer _] + (-write writer (str "\"" (.toString sym) "\"")))) + +(set! s/*explain-out* expound/printer) + +(enable-console-print!) + +(devtools/install!) + +(core/init! true) diff --git a/masto-embed/env/dev/resources/config.edn b/masto-embed/env/dev/resources/config.edn new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/masto-embed/env/dev/resources/config.edn @@ -0,0 +1 @@ +{} diff --git a/masto-embed/env/dev/resources/logback.xml b/masto-embed/env/dev/resources/logback.xml new file mode 100644 index 0000000..1ca7490 --- /dev/null +++ b/masto-embed/env/dev/resources/logback.xml @@ -0,0 +1,34 @@ + + + + + + + UTF-8 + %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n + + + + log/masto-embed.log + + log/masto-embed.%d{yyyy-MM-dd}.%i.log + + 100MB + + + 30 + + + UTF-8 + %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n + + + + + + + + + + diff --git a/masto-embed/env/prod/clj/masto_embed/env.clj b/masto-embed/env/prod/clj/masto_embed/env.clj new file mode 100644 index 0000000..4f811f5 --- /dev/null +++ b/masto-embed/env/prod/clj/masto_embed/env.clj @@ -0,0 +1,11 @@ +(ns masto-embed.env + (:require [clojure.tools.logging :as log])) + +(def defaults + {:init + (fn [] + (log/info "\n-=[masto-embed started successfully]=-")) + :stop + (fn [] + (log/info "\n-=[masto-embed has shut down successfully]=-")) + :middleware identity}) diff --git a/masto-embed/env/prod/cljs/masto_embed/app.cljs b/masto-embed/env/prod/cljs/masto_embed/app.cljs new file mode 100644 index 0000000..3d11013 --- /dev/null +++ b/masto-embed/env/prod/cljs/masto_embed/app.cljs @@ -0,0 +1,7 @@ +(ns masto-embed.app + (:require [masto-embed.core :as core])) + +;;ignore println statements in prod +(set! *print-fn* (fn [& _])) + +(core/init! false) diff --git a/masto-embed/env/prod/resources/config.edn b/masto-embed/env/prod/resources/config.edn new file mode 100644 index 0000000..e24ec21 --- /dev/null +++ b/masto-embed/env/prod/resources/config.edn @@ -0,0 +1,2 @@ +{:prod true + :port 3000} diff --git a/masto-embed/env/prod/resources/logback.xml b/masto-embed/env/prod/resources/logback.xml new file mode 100644 index 0000000..c2b68f6 --- /dev/null +++ b/masto-embed/env/prod/resources/logback.xml @@ -0,0 +1,31 @@ + + + + + log/masto-embed.log + + log/masto-embed.%d{yyyy-MM-dd}.%i.log + + 100MB + + + 30 + + + UTF-8 + %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n + + + + + %-5relative %-5level %logger{35} - %msg%n + + + + + + + + + + diff --git a/masto-embed/env/test/resources/config.edn b/masto-embed/env/test/resources/config.edn new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/masto-embed/env/test/resources/config.edn @@ -0,0 +1 @@ +{} diff --git a/masto-embed/env/test/resources/logback.xml b/masto-embed/env/test/resources/logback.xml new file mode 100644 index 0000000..1ca7490 --- /dev/null +++ b/masto-embed/env/test/resources/logback.xml @@ -0,0 +1,34 @@ + + + + + + + UTF-8 + %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n + + + + log/masto-embed.log + + log/masto-embed.%d{yyyy-MM-dd}.%i.log + + 100MB + + + 30 + + + UTF-8 + %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n + + + + + + + + + + diff --git a/masto-embed/project.clj b/masto-embed/project.clj new file mode 100644 index 0000000..7b98233 --- /dev/null +++ b/masto-embed/project.clj @@ -0,0 +1,134 @@ +(defproject masto-embed "0.1.0-SNAPSHOT" + + :description "FIXME: write description" + :url "http://example.com/FIXME" + + :dependencies [[ch.qos.logback/logback-classic "1.2.3"] + [cheshire "5.10.0"] + [cljs-ajax "0.8.0"] + [clojure.java-time "0.3.2"] + [com.cognitect/transit-clj "1.0.324"] + [cprop "0.1.16"] + [day8.re-frame/http-fx "0.1.6"] + [expound "0.8.4"] + [funcool/struct "1.4.0"] + [kee-frame "0.3.3" :exclusions [metosin/reitit-core org.clojure/core.async]] + [luminus-jetty "0.1.9"] + [luminus-transit "0.1.2"] + [luminus/ring-ttl-session "0.3.3"] + [markdown-clj "1.10.2"] + [metosin/muuntaja "0.6.6"] + [metosin/reitit "0.4.2"] + [metosin/ring-http-response "0.9.1"] + [mount "0.1.16"] + [nrepl "0.7.0"] + [org.clojure/clojure "1.10.1"] + [org.clojure/clojurescript "1.10.597" :scope "provided"] + [org.clojure/tools.cli "1.0.194"] + [org.clojure/tools.logging "1.0.0"] + [org.webjars.npm/bulma "0.8.1"] + [org.webjars.npm/material-icons "0.3.1"] + [org.webjars/webjars-locator "0.39"] + [re-frame "0.12.0"] + [reagent "0.10.0"] + [ring-webjars "0.2.0"] + [ring/ring-core "1.8.0"] + [ring/ring-defaults "0.3.2"] + [selmer "1.12.19"]] + + :min-lein-version "2.0.0" + + :source-paths ["src/clj" "src/cljs" "src/cljc"] + :test-paths ["test/clj"] + :resource-paths ["resources" "target/cljsbuild"] + :target-path "target/%s/" + :main ^:skip-aot masto-embed.core + + :plugins [[lein-cljsbuild "1.1.7"]] + :clean-targets ^{:protect false} + [:target-path [:cljsbuild :builds :app :compiler :output-dir] [:cljsbuild :builds :app :compiler :output-to]] + :figwheel + {:http-server-root "public" + :server-logfile "log/figwheel-logfile.log" + :nrepl-port 7002 + :css-dirs ["resources/public/css"] + :nrepl-middleware [cider.piggieback/wrap-cljs-repl]} + + + :profiles + {:uberjar {:omit-source true + :prep-tasks ["compile" ["cljsbuild" "once" "min"]] + :cljsbuild{:builds + {:min + {:source-paths ["src/cljc" "src/cljs" "env/prod/cljs"] + :compiler + {:output-dir "target/cljsbuild/public/js" + :output-to "target/cljsbuild/public/js/app.js" + :source-map "target/cljsbuild/public/js/app.js.map" + :optimizations :advanced + :pretty-print false + :infer-externs true + :closure-warnings + {:externs-validation :off :non-standard-jsdoc :off} + :externs ["react/externs/react.js"]}}}} + + :aot :all + :uberjar-name "masto-embed.jar" + :source-paths ["env/prod/clj" ] + :resource-paths ["env/prod/resources"]} + + :dev [:project/dev :profiles/dev] + :test [:project/dev :project/test :profiles/test] + + :project/dev {:jvm-opts ["-Dconf=dev-config.edn" ] + :dependencies [[binaryage/devtools "1.0.0"] + [cider/piggieback "0.4.2"] + [doo "0.1.11"] + [figwheel-sidecar "0.5.19"] + [pjstadig/humane-test-output "0.10.0"] + [prone "2020-01-17"] + [re-frisk "0.5.5"] + [ring/ring-devel "1.8.0"] + [ring/ring-mock "0.4.0"]] + :plugins [[com.jakemccrary/lein-test-refresh "0.24.1"] + [jonase/eastwood "0.3.5"] + [lein-doo "0.1.11"] + [lein-figwheel "0.5.19"]] + :cljsbuild{:builds + {:app + {:source-paths ["src/cljs" "src/cljc" "env/dev/cljs"] + :figwheel {:on-jsload "masto-embed.core/mount-components"} + :compiler + {:output-dir "target/cljsbuild/public/js/out" + :closure-defines {"re_frame.trace.trace_enabled_QMARK_" true} + :optimizations :none + :preloads [re-frisk.preload] + :output-to "target/cljsbuild/public/js/app.js" + :asset-path "/js/out" + :source-map true + :main "masto-embed.app" + :pretty-print true}}}} + + + :doo {:build "test"} + :source-paths ["env/dev/clj" ] + :resource-paths ["env/dev/resources"] + :repl-options {:init-ns user + :timeout 120000} + :injections [(require 'pjstadig.humane-test-output) + (pjstadig.humane-test-output/activate!)]} + :project/test {:jvm-opts ["-Dconf=test-config.edn" ] + :resource-paths ["env/test/resources"] + :cljsbuild + {:builds + {:test + {:source-paths ["src/cljc" "src/cljs" "test/cljs"] + :compiler + {:output-to "target/test.js" + :main "masto-embed.doo-runner" + :optimizations :whitespace + :pretty-print true}}}} + + } + :profiles/dev {} + :profiles/test {}}) diff --git a/masto-embed/resources/docs/docs.md b/masto-embed/resources/docs/docs.md new file mode 100644 index 0000000..254cc7b --- /dev/null +++ b/masto-embed/resources/docs/docs.md @@ -0,0 +1,82 @@ +

Congratulations, your Luminus site is ready!

+ +This page will help guide you through the first steps of building your site. + +

Why are you seeing this page?

+ +The `home-routes` handler in the `masto-embed.routes.home` namespace +defines the route that invokes the `home-page` function whenever an HTTP +request is made to the `/` URI using the `GET` method. + +``` +(defroutes home-routes + (GET "/" [] + (home-page)) + (GET "/docs" [] + (-> (response/ok (-> "docs/docs.md" io/resource slurp)) + (response/header "Content-Type" "text/plain; charset=utf-8")))) +``` + +The `home-page` function will in turn call the `masto-embed.layout/render` function +to render the HTML content: + +``` +(defn home-page [] + (layout/render "home.html")) +``` + +The page contains a link to the compiled ClojureScript found in the `target/cljsbuild/public` folder: + +``` +{% script "/js/app.js" %} +``` + +The rest of this page is rendered by ClojureScript found in the `src/cljs/masto_embed/core.cljs` file. + + + +

Organizing the routes

+ +The routes are aggregated and wrapped with middleware in the `masto-embed.handler` namespace: + +``` +(defstate app + :start + (middleware/wrap-base + (routes + (-> #'home-routes + (wrap-routes middleware/wrap-csrf) + (wrap-routes middleware/wrap-formats)) + (route/not-found + (:body + (error-page {:status 404 + :title "page not found"})))))) +``` + +The `app` definition groups all the routes in the application into a single handler. +A default route group is added to handle the `404` case. + +learn more about routing » + +The `home-routes` are wrapped with two middleware functions. The first enables CSRF protection. +The second takes care of serializing and deserializing various encoding formats, such as JSON. + +

Managing your middleware

+ +Request middleware functions are located under the `masto-embed.middleware` namespace. + +This namespace is reserved for any custom middleware for the application. Some default middleware is +already defined here. The middleware is assembled in the `wrap-base` function. + +Middleware used for development is placed in the `masto-embed.dev-middleware` namespace found in +the `env/dev/clj/` source path. + +learn more about middleware » + + + + +

Need some help?

+ +Visit the [official documentation](http://www.luminusweb.net/docs) for examples +on how to accomplish common tasks with Luminus. The `#luminus` channel on the [Clojurians Slack](http://clojurians.net/) and [Google Group](https://groups.google.com/forum/#!forum/luminusweb) are both great places to seek help and discuss projects with other users. diff --git a/masto-embed/resources/html/error.html b/masto-embed/resources/html/error.html new file mode 100644 index 0000000..1823ff1 --- /dev/null +++ b/masto-embed/resources/html/error.html @@ -0,0 +1,20 @@ + + + + Something Bad Happened + + + + +
+

Error: {{status}}

+
+ {% if title %} +

{{title}}

+ {% endif %} + {% if message %} +

{{message}}

+ {% endif %} +
+ + diff --git a/masto-embed/resources/html/home.html b/masto-embed/resources/html/home.html new file mode 100644 index 0000000..b219c1a --- /dev/null +++ b/masto-embed/resources/html/home.html @@ -0,0 +1,41 @@ + + + + + + Welcome to masto-embed + + + +
+
+
+
+

Welcome to masto-embed

+

If you're seeing this message, that means you haven't yet compiled your ClojureScript!

+

Please run lein figwheel to start the ClojureScript compiler and reload the page.

+

For better ClojureScript development experience in Chrome follow these steps:

+
    +
  • Open DevTools +
  • Go to Settings ("three dots" icon in the upper right corner of DevTools > Menu > Settings F1 > General > Console) +
  • Check-in "Enable custom formatters" +
  • Close DevTools +
  • Open DevTools +
+

See ClojureScript documentation for further details.

+
+
+
+
+ + + {% style "/assets/bulma/css/bulma.min.css" %} + {% style "/assets/material-icons/css/material-icons.min.css" %} + {% style "/css/screen.css" %} + + + {% script "/js/app.js" %} + + diff --git a/masto-embed/resources/public/css/screen.css b/masto-embed/resources/public/css/screen.css new file mode 100644 index 0000000..e90ddb8 --- /dev/null +++ b/masto-embed/resources/public/css/screen.css @@ -0,0 +1,35 @@ +html, +body { + font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; +} +@font-face { + font-family: 'Material Icons'; + font-style: normal; + font-weight: 400; + src: url(/assets/material-icons/iconfont/MaterialIcons-Regular.eot); /* For IE6-8 */ + src: local('Material Icons'), + local('MaterialIcons-Regular'), + url(/assets/material-icons/iconfont/MaterialIcons-Regular.woff2) format('woff2'), + url(/assets/material-icons/iconfont/MaterialIcons-Regular.woff) format('woff'), + url(/assets/material-icons/iconfont/MaterialIcons-Regular.ttf) format('truetype'); +} +.material-icons { + font-family: 'Material Icons'; + font-weight: normal; + font-style: normal; + font-size: 24px; /* Preferred icon size */ + display: inline-block; + line-height: 1; + text-transform: none; + letter-spacing: normal; + word-wrap: normal; + white-space: nowrap; + direction: ltr; + /* Support for all WebKit browsers. */ + -webkit-font-smoothing: antialiased; + /* Support for Safari and Chrome. */ + text-rendering: optimizeLegibility; + /* Support for Firefox. */ + -moz-osx-font-smoothing: grayscale; +} + diff --git a/masto-embed/resources/public/favicon.ico b/masto-embed/resources/public/favicon.ico new file mode 100644 index 0000000..0e50cb2 Binary files /dev/null and b/masto-embed/resources/public/favicon.ico differ diff --git a/masto-embed/resources/public/img/warning_clojure.png b/masto-embed/resources/public/img/warning_clojure.png new file mode 100644 index 0000000..78d59e9 Binary files /dev/null and b/masto-embed/resources/public/img/warning_clojure.png differ diff --git a/masto-embed/src/clj/masto_embed/config.clj b/masto-embed/src/clj/masto_embed/config.clj new file mode 100644 index 0000000..87ac540 --- /dev/null +++ b/masto-embed/src/clj/masto_embed/config.clj @@ -0,0 +1,13 @@ +(ns masto-embed.config + (:require + [cprop.core :refer [load-config]] + [cprop.source :as source] + [mount.core :refer [args defstate]])) + +(defstate env + :start + (load-config + :merge + [(args) + (source/from-system-props) + (source/from-env)])) diff --git a/masto-embed/src/clj/masto_embed/core.clj b/masto-embed/src/clj/masto_embed/core.clj new file mode 100644 index 0000000..b8588f3 --- /dev/null +++ b/masto-embed/src/clj/masto_embed/core.clj @@ -0,0 +1,57 @@ +(ns masto-embed.core + (:require + [masto-embed.handler :as handler] + [masto-embed.nrepl :as nrepl] + [luminus.http-server :as http] + [masto-embed.config :refer [env]] + [clojure.tools.cli :refer [parse-opts]] + [clojure.tools.logging :as log] + [mount.core :as mount]) + (:gen-class)) + +;; log uncaught exceptions in threads +(Thread/setDefaultUncaughtExceptionHandler + (reify Thread$UncaughtExceptionHandler + (uncaughtException [_ thread ex] + (log/error {:what :uncaught-exception + :exception ex + :where (str "Uncaught exception on" (.getName thread))})))) + +(def cli-options + [["-p" "--port PORT" "Port number" + :parse-fn #(Integer/parseInt %)]]) + +(mount/defstate ^{:on-reload :noop} http-server + :start + (http/start + (-> env + (assoc :handler (handler/app)) + (update :port #(or (-> env :options :port) %)))) + :stop + (http/stop http-server)) + +(mount/defstate ^{:on-reload :noop} repl-server + :start + (when (env :nrepl-port) + (nrepl/start {:bind (env :nrepl-bind) + :port (env :nrepl-port)})) + :stop + (when repl-server + (nrepl/stop repl-server))) + + +(defn stop-app [] + (doseq [component (:stopped (mount/stop))] + (log/info component "stopped")) + (shutdown-agents)) + +(defn start-app [args] + (doseq [component (-> args + (parse-opts cli-options) + mount/start-with-args + :started)] + (log/info component "started")) + (.addShutdownHook (Runtime/getRuntime) (Thread. stop-app))) + +(defn -main [& args] + (start-app args)) diff --git a/masto-embed/src/clj/masto_embed/handler.clj b/masto-embed/src/clj/masto_embed/handler.clj new file mode 100644 index 0000000..ede136c --- /dev/null +++ b/masto-embed/src/clj/masto_embed/handler.clj @@ -0,0 +1,35 @@ +(ns masto-embed.handler + (:require + [masto-embed.middleware :as middleware] + [masto-embed.layout :refer [error-page]] + [masto-embed.routes.home :refer [home-routes]] + [reitit.ring :as ring] + [ring.middleware.content-type :refer [wrap-content-type]] + [ring.middleware.webjars :refer [wrap-webjars]] + [masto-embed.env :refer [defaults]] + [mount.core :as mount])) + +(mount/defstate init-app + :start ((or (:init defaults) (fn []))) + :stop ((or (:stop defaults) (fn [])))) + +(mount/defstate app-routes + :start + (ring/ring-handler + (ring/router + [(home-routes)]) + (ring/routes + (ring/create-resource-handler + {:path "/"}) + (wrap-content-type + (wrap-webjars (constantly nil))) + (ring/create-default-handler + {:not-found + (constantly (error-page {:status 404, :title "404 - Page not found"})) + :method-not-allowed + (constantly (error-page {:status 405, :title "405 - Not allowed"})) + :not-acceptable + (constantly (error-page {:status 406, :title "406 - Not acceptable"}))})))) + +(defn app [] + (middleware/wrap-base #'app-routes)) diff --git a/masto-embed/src/clj/masto_embed/layout.clj b/masto-embed/src/clj/masto_embed/layout.clj new file mode 100644 index 0000000..cf10ad1 --- /dev/null +++ b/masto-embed/src/clj/masto_embed/layout.clj @@ -0,0 +1,39 @@ +(ns masto-embed.layout + (:require + [clojure.java.io] + [selmer.parser :as parser] + [selmer.filters :as filters] + [markdown.core :refer [md-to-html-string]] + [ring.util.http-response :refer [content-type ok]] + [ring.util.anti-forgery :refer [anti-forgery-field]] + [ring.middleware.anti-forgery :refer [*anti-forgery-token*]] + [ring.util.response])) + +(parser/set-resource-path! (clojure.java.io/resource "html")) +(parser/add-tag! :csrf-field (fn [_ _] (anti-forgery-field))) +(filters/add-filter! :markdown (fn [content] [:safe (md-to-html-string content)])) + +(defn render + "renders the HTML template located relative to resources/html" + [request template & [params]] + (content-type + (ok + (parser/render-file + template + (assoc params + :page template + :csrf-token *anti-forgery-token*))) + "text/html; charset=utf-8")) + +(defn error-page + "error-details should be a map containing the following keys: + :status - error status + :title - error title (optional) + :message - detailed error message (optional) + + returns a response map with the error page as the body + and the status specified by the status key" + [error-details] + {:status (:status error-details) + :headers {"Content-Type" "text/html; charset=utf-8"} + :body (parser/render-file "error.html" error-details)}) diff --git a/masto-embed/src/clj/masto_embed/middleware.clj b/masto-embed/src/clj/masto_embed/middleware.clj new file mode 100644 index 0000000..7f0080b --- /dev/null +++ b/masto-embed/src/clj/masto_embed/middleware.clj @@ -0,0 +1,48 @@ +(ns masto-embed.middleware + (:require + [masto-embed.env :refer [defaults]] + [cheshire.generate :as cheshire] + [cognitect.transit :as transit] + [clojure.tools.logging :as log] + [masto-embed.layout :refer [error-page]] + [ring.middleware.anti-forgery :refer [wrap-anti-forgery]] + [masto-embed.middleware.formats :as formats] + [muuntaja.middleware :refer [wrap-format wrap-params]] + [masto-embed.config :refer [env]] + [ring-ttl-session.core :refer [ttl-memory-store]] + [ring.middleware.defaults :refer [site-defaults wrap-defaults]]) + ) + +(defn wrap-internal-error [handler] + (fn [req] + (try + (handler req) + (catch Throwable t + (log/error t (.getMessage t)) + (error-page {:status 500 + :title "Something very bad has happened!" + :message "We've dispatched a team of highly trained gnomes to take care of the problem."}))))) + +(defn wrap-csrf [handler] + (wrap-anti-forgery + handler + {:error-response + (error-page + {:status 403 + :title "Invalid anti-forgery token"})})) + + +(defn wrap-formats [handler] + (let [wrapped (-> handler wrap-params (wrap-format formats/instance))] + (fn [request] + ;; disable wrap-formats for websockets + ;; since they're not compatible with this middleware + ((if (:websocket? request) handler wrapped) request)))) + +(defn wrap-base [handler] + (-> ((:middleware defaults) handler) + (wrap-defaults + (-> site-defaults + (assoc-in [:security :anti-forgery] false) + (assoc-in [:session :store] (ttl-memory-store (* 60 30))))) + wrap-internal-error)) diff --git a/masto-embed/src/clj/masto_embed/middleware/formats.clj b/masto-embed/src/clj/masto_embed/middleware/formats.clj new file mode 100644 index 0000000..825d0e6 --- /dev/null +++ b/masto-embed/src/clj/masto_embed/middleware/formats.clj @@ -0,0 +1,15 @@ +(ns masto-embed.middleware.formats + (:require + [cognitect.transit :as transit] + [luminus-transit.time :as time] + [muuntaja.core :as m])) + +(def instance + (m/create + (-> m/default-options + (update-in + [:formats "application/transit+json" :decoder-opts] + (partial merge time/time-deserialization-handlers)) + (update-in + [:formats "application/transit+json" :encoder-opts] + (partial merge time/time-serialization-handlers))))) diff --git a/masto-embed/src/clj/masto_embed/nrepl.clj b/masto-embed/src/clj/masto_embed/nrepl.clj new file mode 100644 index 0000000..2f41069 --- /dev/null +++ b/masto-embed/src/clj/masto_embed/nrepl.clj @@ -0,0 +1,27 @@ +(ns masto-embed.nrepl + (:require + [nrepl.server :as nrepl] + [clojure.tools.logging :as log])) + +(defn start + "Start a network repl for debugging on specified port followed by + an optional parameters map. The :bind, :transport-fn, :handler, + :ack-port and :greeting-fn will be forwarded to + clojure.tools.nrepl.server/start-server as they are." + [{:keys [port bind transport-fn handler ack-port greeting-fn]}] + (try + (log/info "starting nREPL server on port" port) + (nrepl/start-server :port port + :bind bind + :transport-fn transport-fn + :handler handler + :ack-port ack-port + :greeting-fn greeting-fn) + + (catch Throwable t + (log/error t "failed to start nREPL") + (throw t)))) + +(defn stop [server] + (nrepl/stop-server server) + (log/info "nREPL server stopped")) diff --git a/masto-embed/src/clj/masto_embed/routes/home.clj b/masto-embed/src/clj/masto_embed/routes/home.clj new file mode 100644 index 0000000..80da962 --- /dev/null +++ b/masto-embed/src/clj/masto_embed/routes/home.clj @@ -0,0 +1,20 @@ +(ns masto-embed.routes.home + (:require + [masto-embed.layout :as layout] + [clojure.java.io :as io] + [masto-embed.middleware :as middleware] + [ring.util.response] + [ring.util.http-response :as response])) + +(defn home-page [request] + (layout/render request "home.html")) + +(defn home-routes [] + ["" + {:middleware [middleware/wrap-csrf + middleware/wrap-formats]} + ["/" {:get home-page}] + ["/docs" {:get (fn [_] + (-> (response/ok (-> "docs/docs.md" io/resource slurp)) + (response/header "Content-Type" "text/plain; charset=utf-8")))}]]) + diff --git a/masto-embed/src/cljc/masto_embed/validation.cljc b/masto-embed/src/cljc/masto_embed/validation.cljc new file mode 100644 index 0000000..f190e44 --- /dev/null +++ b/masto-embed/src/cljc/masto_embed/validation.cljc @@ -0,0 +1,2 @@ +(ns masto-embed.validation + (:require [struct.core :as st])) diff --git a/masto-embed/src/cljs/masto_embed/ajax.cljs b/masto-embed/src/cljs/masto_embed/ajax.cljs new file mode 100644 index 0000000..175763e --- /dev/null +++ b/masto-embed/src/cljs/masto_embed/ajax.cljs @@ -0,0 +1,30 @@ +(ns masto-embed.ajax + (:require + [ajax.core :as ajax] + [luminus-transit.time :as time] + [cognitect.transit :as transit] + [re-frame.core :as rf])) + +(defn local-uri? [{:keys [uri]}] + (not (re-find #"^\w+?://" uri))) + +(defn default-headers [request] + (if (local-uri? request) + (-> request + (update :headers #(merge {"x-csrf-token" js/csrfToken} %))) + request)) + +;; injects transit serialization config into request options +(defn as-transit [opts] + (merge {:raw false + :format :transit + :response-format :transit + :reader (transit/reader :json time/time-deserialization-handlers) + :writer (transit/writer :json time/time-serialization-handlers)} + opts)) + +(defn load-interceptors! [] + (swap! ajax/default-interceptors + conj + (ajax/to-interceptor {:name "default headers" + :request default-headers}))) diff --git a/masto-embed/src/cljs/masto_embed/core.cljs b/masto-embed/src/cljs/masto_embed/core.cljs new file mode 100644 index 0000000..85e71fc --- /dev/null +++ b/masto-embed/src/cljs/masto_embed/core.cljs @@ -0,0 +1,55 @@ +(ns masto-embed.core + (:require + [kee-frame.core :as kf] + [re-frame.core :as rf] + [ajax.core :as http] + [masto-embed.ajax :as ajax] + [masto-embed.routing :as routing] + [masto-embed.view :as view])) + + +(rf/reg-event-fx + ::load-about-page + (constantly nil)) + +(kf/reg-controller + ::about-controller + {:params (constantly true) + :start [::load-about-page]}) + +(rf/reg-sub + :docs + (fn [db _] + (:docs db))) + +(kf/reg-chain + ::load-home-page + (fn [_ _] + {:http-xhrio {:method :get + :uri "/docs" + :response-format (http/raw-response-format) + :on-failure [:common/set-error]}}) + (fn [{:keys [db]} [_ docs]] + {:db (assoc d#b :docs docs)})) + + +(kf/reg-controller + ::home-controller + {:params (constantly true) + :start [::load-home-page]}) + +;; ------------------------- +;; Initialize app +(defn mount-components + ([] (mount-components true)) + ([debug?] + (rf/clear-subscription-cache!) + (kf/start! {:debug? (boolean debug?) + :routes routing/routes + :hash-routing? true + :initial-db {} + :root-component [view/root-component]}))) + +(defn init! [debug?] + (ajax/load-interceptors!) + (mount-components debug?)) diff --git a/masto-embed/src/cljs/masto_embed/routing.cljs b/masto-embed/src/cljs/masto_embed/routing.cljs new file mode 100644 index 0000000..d8fae7a --- /dev/null +++ b/masto-embed/src/cljs/masto_embed/routing.cljs @@ -0,0 +1,24 @@ +(ns masto-embed.routing + (:require + [re-frame.core :as rf])) + +(def routes + [["/" :home] + ["/about" :about]]) + +(rf/reg-sub + :nav/route + :<- [:kee-frame/route] + identity) + +(rf/reg-event-fx + :nav/route-name + (fn [_ [_ route-name]] + {:navigate-to [route-name]})) + + +(rf/reg-sub + :nav/page + :<- [:nav/route] + (fn [route _] + (-> route :data :name))) diff --git a/masto-embed/src/cljs/masto_embed/view.cljs b/masto-embed/src/cljs/masto_embed/view.cljs new file mode 100644 index 0000000..5132cff --- /dev/null +++ b/masto-embed/src/cljs/masto_embed/view.cljs @@ -0,0 +1,45 @@ +(ns masto-embed.view + (:require + [kee-frame.core :as kf] + [markdown.core :refer [md->html]] + [reagent.core :as r] + [re-frame.core :as rf])) + +(defn nav-link [title page] + [:a.navbar-item + {:href (kf/path-for [page]) + :class (when (= page @(rf/subscribe [:nav/page])) "is-active")} + title]) + +(defn navbar [] + (r/with-let [expanded? (r/atom false)] + [:nav.navbar.is-info>div.container + [:div.navbar-brand + [:a.navbar-item {:href "/" :style {:font-weight :bold}} "masto-embed"] + [:span.navbar-burger.burger + {:data-target :nav-menu + :on-click #(swap! expanded? not) + :class (when @expanded? :is-active)} + [:span][:span][:span]]] + [:div#nav-menu.navbar-menu + {:class (when @expanded? :is-active)} + [:div.navbar-start + [nav-link "Home" :home] + [nav-link "About" :about]]]])) + +(defn about-page [] + [:section.section>div.container>div.content + [:img {:src "/img/warning_clojure.png"}]]) + +(defn home-page [] + [:section.section>div.container>div.content + (when-let [docs @(rf/subscribe [:docs])] + [:div {:dangerouslySetInnerHTML {:__html (md->html docs)}}])]) + +(defn root-component [] + [:div + [navbar] + [kf/switch-route (fn [route] (get-in route [:data :name])) + :home home-page + :about about-page + nil [:div ""]]]) diff --git a/masto-embed/test/clj/masto_embed/test/handler.clj b/masto-embed/test/clj/masto_embed/test/handler.clj new file mode 100644 index 0000000..e3179f2 --- /dev/null +++ b/masto-embed/test/clj/masto_embed/test/handler.clj @@ -0,0 +1,27 @@ +(ns masto-embed.test.handler + (:require + [clojure.test :refer :all] + [ring.mock.request :refer :all] + [masto-embed.handler :refer :all] + [masto-embed.middleware.formats :as formats] + [muuntaja.core :as m] + [mount.core :as mount])) + +(defn parse-json [body] + (m/decode formats/instance "application/json" body)) + +(use-fixtures + :once + (fn [f] + (mount/start #'masto-embed.config/env + #'masto-embed.handler/app-routes) + (f))) + +(deftest test-app + (testing "main route" + (let [response ((app) (request :get "/"))] + (is (= 200 (:status response))))) + + (testing "not-found route" + (let [response ((app) (request :get "/invalid"))] + (is (= 404 (:status response)))))) diff --git a/masto-embed/test/cljs/masto_embed/core_test.cljs b/masto-embed/test/cljs/masto_embed/core_test.cljs new file mode 100644 index 0000000..9d410ea --- /dev/null +++ b/masto-embed/test/cljs/masto_embed/core_test.cljs @@ -0,0 +1,9 @@ +(ns masto-embed.core-test + (:require [cljs.test :refer-macros [is are deftest testing use-fixtures]] + [pjstadig.humane-test-output] + [reagent.core :as reagent :refer [atom]] + [masto-embed.core :as rc])) + +(deftest test-home + (is (= true true))) + diff --git a/masto-embed/test/cljs/masto_embed/doo_runner.cljs b/masto-embed/test/cljs/masto_embed/doo_runner.cljs new file mode 100644 index 0000000..4c9a755 --- /dev/null +++ b/masto-embed/test/cljs/masto_embed/doo_runner.cljs @@ -0,0 +1,6 @@ +(ns masto-embed.doo-runner + (:require [doo.runner :refer-macros [doo-tests]] + [masto-embed.core-test])) + +(doo-tests 'masto-embed.core-test) +