Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
interstar committed Jul 4, 2018
1 parent 61fccbf commit 0964413
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*.log
/target
/*-init.clj
/resources/public/js/compiled
out
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: java $JVM_OPTS -cp target/contascalangos.jar clojure.main -m contascalangos.server
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# contascalangos

A [re-frame](https://github.com/Day8/re-frame) application designed to ... well, that part is up to you.

## Development Mode

### Start Cider from Emacs:

Put this in your Emacs config file:

```
(setq cider-cljs-lein-repl
"(do (require 'figwheel-sidecar.repl-api)
(figwheel-sidecar.repl-api/start-figwheel!)
(figwheel-sidecar.repl-api/cljs-repl))")
```

Navigate to a clojurescript file and start a figwheel REPL with `cider-jack-in-clojurescript` or (`C-c M-J`)

### Run application:

```
lein clean
lein figwheel dev
```

Figwheel will automatically push cljs changes to the browser.

Wait a bit, then browse to [http://localhost:3449](http://localhost:3449).

## Production Build

```
lein clean
lein with-profile prod uberjar
```

That should compile the clojurescript code first, and then create the standalone jar.

When you run the jar you can set the port the ring server will use by setting the environment variable PORT.
If it's not set, it will run on port 3000 by default.

To deploy to heroku, first create your app:

```
heroku create
```

Then deploy the application:

```
git push heroku master
```

To compile clojurescript to javascript:

```
lein clean
lein cljsbuild once min
```
71 changes: 71 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
(defproject contascalangos "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.10.238"]
[reagent "0.7.0"]
[re-frame "0.10.5"]
[secretary "1.2.3"]
[compojure "1.5.0"]
[yogthos/config "0.8"]
[ring "1.4.0"]]

:plugins [[lein-cljsbuild "1.1.7"]]

:min-lein-version "2.5.3"

:source-paths ["src/clj" "src/cljs"]

:clean-targets ^{:protect false} ["resources/public/js/compiled" "target"]

:figwheel {:css-dirs ["resources/public/css"]
:ring-handler contascalangos.handler/dev-handler}

:repl-options {:nrepl-middleware [cider.piggieback/wrap-cljs-repl]}

:profiles
{:dev
{:dependencies [[binaryage/devtools "0.9.10"]
[day8.re-frame/re-frame-10x "0.3.3"]
[day8.re-frame/tracing "0.5.1"]
[figwheel-sidecar "0.5.16"]
[cider/piggieback "0.3.5"]]

:plugins [[lein-figwheel "0.5.16"]]}
:prod { :dependencies [[day8.re-frame/tracing-stubs "0.5.1"]]}}

:cljsbuild
{:builds
[{:id "dev"
:source-paths ["src/cljs"]
:figwheel {:on-jsload "contascalangos.core/mount-root"}
:compiler {:main contascalangos.core
:output-to "resources/public/js/compiled/app.js"
:output-dir "resources/public/js/compiled/out"
:asset-path "js/compiled/out"
:source-map-timestamp true
:preloads [devtools.preload
day8.re-frame-10x.preload]
:closure-defines {"re_frame.trace.trace_enabled_QMARK_" true
"day8.re_frame.tracing.trace_enabled_QMARK_" true}
:external-config {:devtools/config {:features-to-install :all}}
}}

{:id "min"
:source-paths ["src/cljs"]
:jar true
:compiler {:main contascalangos.core
:output-to "resources/public/js/compiled/app.js"
:optimizations :advanced
:closure-defines {goog.DEBUG false}
:pretty-print false}}


]}

:main contascalangos.server

:aot [contascalangos.server]

:uberjar-name "contascalangos.jar"

:prep-tasks [["cljsbuild" "once" "min"] "compile"]
)
13 changes: 13 additions & 0 deletions resources/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset='utf-8'>


</head>
<body>
<div id="app"></div>
<script src="js/compiled/app.js"></script>
<script>contascalangos.core.init();</script>
</body>
</html>
1 change: 1 addition & 0 deletions src/clj/contascalangos/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns contascalangos.core)
13 changes: 13 additions & 0 deletions src/clj/contascalangos/handler.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns contascalangos.handler
(:require [compojure.core :refer [GET defroutes]]
[compojure.route :refer [resources]]
[ring.util.response :refer [resource-response]]
[ring.middleware.reload :refer [wrap-reload]]))

(defroutes routes
(GET "/" [] (resource-response "index.html" {:root "public"}))
(resources "/"))

(def dev-handler (-> #'routes wrap-reload))

(def handler routes)
9 changes: 9 additions & 0 deletions src/clj/contascalangos/server.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns contascalangos.server
(:require [contascalangos.handler :refer [handler]]
[config.core :refer [env]]
[ring.adapter.jetty :refer [run-jetty]])
(:gen-class))

(defn -main [& args]
(let [port (Integer/parseInt (or (env :port) "3000"))]
(run-jetty handler {:port port :join? false})))
4 changes: 4 additions & 0 deletions src/cljs/contascalangos/config.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns contascalangos.config)

(def debug?
^boolean goog.DEBUG)
26 changes: 26 additions & 0 deletions src/cljs/contascalangos/core.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns contascalangos.core
(:require
[reagent.core :as reagent]
[re-frame.core :as re-frame]
[contascalangos.events :as events]
[contascalangos.routes :as routes]
[contascalangos.views :as views]
[contascalangos.config :as config]
))


(defn dev-setup []
(when config/debug?
(enable-console-print!)
(println "dev mode")))

(defn mount-root []
(re-frame/clear-subscription-cache!)
(reagent/render [views/main-panel]
(.getElementById js/document "app")))

(defn ^:export init []
(routes/app-routes)
(re-frame/dispatch-sync [::events/initialize-db])
(dev-setup)
(mount-root))
4 changes: 4 additions & 0 deletions src/cljs/contascalangos/db.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns contascalangos.db)

(def default-db
{:name "re-frame"})
16 changes: 16 additions & 0 deletions src/cljs/contascalangos/events.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns contascalangos.events
(:require
[re-frame.core :as re-frame]
[contascalangos.db :as db]
[day8.re-frame.tracing :refer-macros [fn-traced defn-traced]]
))

(re-frame/reg-event-db
::initialize-db
(fn-traced [_ _]
db/default-db))

(re-frame/reg-event-db
::set-active-panel
(fn-traced [db [_ active-panel]]
(assoc db :active-panel active-panel)))
33 changes: 33 additions & 0 deletions src/cljs/contascalangos/routes.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns contascalangos.routes
(:require-macros [secretary.core :refer [defroute]])
(:import goog.History)
(:require
[secretary.core :as secretary]
[goog.events :as gevents]
[goog.history.EventType :as EventType]
[re-frame.core :as re-frame]
[contascalangos.events :as events]
))

(defn hook-browser-navigation! []
(doto (History.)
(gevents/listen
EventType/NAVIGATE
(fn [event]
(secretary/dispatch! (.-token event))))
(.setEnabled true)))

(defn app-routes []
(secretary/set-config! :prefix "#")
;; --------------------
;; define routes here
(defroute "/" []
(re-frame/dispatch [::events/set-active-panel :home-panel])
)

(defroute "/about" []
(re-frame/dispatch [::events/set-active-panel :about-panel]))


;; --------------------
(hook-browser-navigation!))
13 changes: 13 additions & 0 deletions src/cljs/contascalangos/subs.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns contascalangos.subs
(:require
[re-frame.core :as re-frame]))

(re-frame/reg-sub
::name
(fn [db]
(:name db)))

(re-frame/reg-sub
::active-panel
(fn [db _]
(:active-panel db)))
45 changes: 45 additions & 0 deletions src/cljs/contascalangos/views.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(ns contascalangos.views
(:require
[re-frame.core :as re-frame]
[contascalangos.subs :as subs]
))


;; home

(defn home-panel []
(let [name (re-frame/subscribe [::subs/name])]
[:div
[:h1 (str "Hello from " @name ". This is the Home Page.")]

[:div
[:a {:href "#/about"}
"go to About Page"]]
]))


;; about

(defn about-panel []
[:div
[:h1 "This is the About Page."]

[:div
[:a {:href "#/"}
"go to Home Page"]]])


;; main

(defn- panels [panel-name]
(case panel-name
:home-panel [home-panel]
:about-panel [about-panel]
[:div]))

(defn show-panel [panel-name]
[panels panel-name])

(defn main-panel []
(let [active-panel (re-frame/subscribe [::subs/active-panel])]
[show-panel @active-panel]))

0 comments on commit 0964413

Please sign in to comment.