Skip to content

Commit

Permalink
0.8.0 Update to match FW/1 0.8.x
Browse files Browse the repository at this point in the history
Major overhaul of the template to match latest FW/1
setup (component-based, handler/router with Ring/Compojure, adds display
of version to layout, supports both Jetty and http-kit).
  • Loading branch information
seancorfield committed Oct 30, 2016
1 parent 0396fcb commit c8449a3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.boot
@@ -1,4 +1,4 @@
(def version "0.5.2")
(def version "0.8.0")

(task-options!
pom {:project 'fw1/boot-template
Expand Down
9 changes: 5 additions & 4 deletions src/boot/new/fw1.clj
Expand Up @@ -8,7 +8,8 @@
[name]
(let [data {:name name
:sanitized (name-to-path name)
:body "{{body}}"}]
:body "{{body}}"
:version "{{version}}"}]
(->files data
["README.md" (render "README.md" data)]
["build.boot" (render "build.boot" data)]
Expand All @@ -17,6 +18,6 @@
["src/{{sanitized}}/controllers/main.clj" (render "controller.clj" data)]
["src/{{sanitized}}/layouts/main/default.html" (render "layout.html" data)]
["src/{{sanitized}}/views/main/default.html" (render "view.html" data)]
["src/{{sanitized}}/assets/css/main.css" (render "main.css" data)]
"src/{{sanitized}}/assets/js"
"src/{{sanitized}}/assets/img")))
["resources/public/assets/css/main.css" (render "main.css" data)]
"resources/public/assets/js"
"resources/public/assets/img")))
6 changes: 4 additions & 2 deletions src/boot/new/fw1/README.md
Expand Up @@ -16,10 +16,10 @@ or, to run the server on a different port:
boot run -p 8888
```

You can specify FW/1 configuration options via the `-c` (`--config`) option -- e.g., selecting a different `:home` action:
You can specify application configuration options via the `-c` (`--config`) option -- e.g., to override the default choice of jetty as the server:

```bash
boot run -c home=section.item
boot run -c server=http-kit
```

To build a standalone JAR in the `target` folder:
Expand All @@ -30,6 +30,8 @@ boot build

The standalone JAR will read the `PORT` environment variable to determine the port on which to run.

To modify the FW/1 configuration, edit `src/{{sanitized}}/main.clj` and look at the `my-handler` function.

## License

Copyright (C) 2016 FIXME
Expand Down
7 changes: 5 additions & 2 deletions src/boot/new/fw1/build.boot
Expand Up @@ -8,13 +8,16 @@
jar {:main '{{name}}.main
:file (str "{{name}}-" version "-standalone.jar")})

(set-env! :resource-paths #{"src"}
(set-env! :resource-paths #{"src" "resources"}
;; the org.clojure/clojure dependency here only affects
;; what is bundled in the uberjar via the build task so
;; be careful if it is different to the version you have
;; configured for Boot!
:dependencies '[[org.clojure/clojure "RELEASE"]
[framework-one "RELEASE"]])
[framework-one "RELEASE"]
; comment this out if you don't want
; to use http-kit at all:
[http-kit "RELEASE"]])

(deftask build []
(comp (aot) (pom) (uber) (jar) (target :dir #{"target"})))
Expand Down
14 changes: 13 additions & 1 deletion src/boot/new/fw1/controller.clj
@@ -1,5 +1,17 @@
(ns {{name}}.controllers.main
(:require [framework.one :as fw1]))

(defn default "/main/default handler" [rc]
(defn before
"Run before each controller here."
[rc]
rc)

(defn after
"Run after each controller here."
[rc]
(assoc rc :version (-> rc (fw1/event :config) :version)))

(defn default
"/main/default handler"
[rc]
rc)
2 changes: 1 addition & 1 deletion src/boot/new/fw1/layout.html
Expand Up @@ -6,5 +6,5 @@
<body>
<h1>Framework One</h1>
{{body}}
<p>Added by the default layout.</p>
<p>Added by the default layout. Running FW/1 {{version}}</p>
</body>
48 changes: 44 additions & 4 deletions src/boot/new/fw1/main.clj
@@ -1,15 +1,55 @@
(ns {{name}}.main
(:require [framework.one :as fw1]
[ring.adapter.jetty :refer [run-jetty]])
[com.stuartsierra.component :as component])
(:gen-class))

;; Implement your application's start/stop lifecycle here:
;; See https://github.com/stuartsierra/component
(defrecord Application [config]
component/Lifecycle
(start [this]
this)
(stop [this]
this))

(defn my-application
"Return your application component, fully configured.
We use just a single component here -- Application -- but if you have
subcomponents, you could use system-map here."
[application-config]
(map->Application {:config application-config}))

(defn my-handler
"Given the application component, return a Ring handler.
This is where you configure FW/1 itself."
[application]
(fw1/default-handler application
{:application-key "{{name}}"
:home "main.default"}))

(defn new-system
"Build a default system to run. In the REPL:
(def system (new-system 8888))
;; or
(def system (new-system 8888 :http-kit))
(alter-var-root #'system component/start)"
([port] (new-system port :jetty {}))
([port server] (new-system port server {}))
([port server config-map]
(component/system-map :application (my-application config-map)
:web-server (fw1/web-server #'my-handler port server))))

(defn start
"Entry point from Boot file, also called by -main."
[port config-map]
(let [port (or port (System/getenv "PORT") 8080)
port (cond-> port (string? port) Integer/parseInt)]
(run-jetty (fw1/start (merge {:application-key "{{name}}"} config-map))
{:port port})))
port (cond-> port (string? port) Integer/parseInt)
server (or (:server config-map) "jetty")]
(println "Starting" server "on port" port "...")
(-> (component/start (new-system port (keyword server) config-map))
;; wait for the web server to shutdown
:web-server :shutdown deref)))

(defn -main
"Entry point from uberjar etc. Customize as you wish!
Expand Down

0 comments on commit c8449a3

Please sign in to comment.