├── LICENSE ├── README.md ├── todo-app ├── .gitignore ├── README.md ├── cheats │ ├── 01 │ │ ├── handler.clj │ │ └── project.clj │ ├── 02 │ │ └── domain.clj │ └── 03 │ │ └── handler.clj ├── next ├── project.clj ├── resources │ └── public │ │ └── splendor.css ├── src │ └── todo_app │ │ ├── core.clj │ │ ├── domain.clj │ │ └── handler.clj ├── test │ └── todo_app │ │ └── 01_tests.clj └── tests │ ├── 01_tests.clj │ ├── 02_tests.clj │ └── 03_tests.clj └── tutorial ├── .gitignore ├── README.md ├── chapters ├── 00_instructions.md ├── 01_instructions.md ├── 02_instructions.md ├── 03_instructions.md └── 04_instructions.md ├── project.clj ├── resources └── public │ └── splendor.min.css ├── run_tutorial ├── src └── tutorial │ ├── core.clj │ └── handler.clj └── test └── tutorial └── handler_test.clj /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 innoQ 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A Tutorial for creating Web Applications in Clojure 2 | 3 | ## Prerequisites 4 | 5 | The tutorial uses the Clojure build tool [leiningen](https://github.com/technomancy/leiningen), so this needs to be installed before running the tutorial. 6 | 7 | Theoretically, you can use any text editor to create Clojure code. There are many diehard Emacs fans in the community. 8 | 9 | However, for starting out with using Clojure, I recommend downloading [Nightcode](https://sekao.net/nightcode/) which provides a Clojure IDE with the basic functionality needed to get your app up and running. This IDE also comes built in with the [parinfer](https://shaunlebron.github.io/parinfer/) plugin to help with setting the parentheses correctly. 10 | 11 | ## How this tutorial is structured 12 | 13 | The purpose of this tutorial is to help you build your first clojure web application. 14 | 15 | There are two subdirectories it this repository: 16 | 17 | * `todo-app/` Contains the Clojure project that we are going to modify in the course of this tutorial 18 | * `tutorial/` Contains the chapters of the tutorial in Markdown format. This can also be served as HTML as described in the next section 19 | 20 | We've created the bare bones for a web application in the `todo-app` directory. 21 | During this tutorial, we will develop this application using test driven development. There are tests that have been created for each step in the tutorial to test if the task was correctly solved. These tests can be found in the `tests/` directory. If you want to retrieve these automatically, you can execute the `next` script when you want to retrieve the next tests. This will only be successful if the tests for the last task pass. ;) 22 | 23 | If you get stuck, don't despair! We've created the directory `cheats/` which contains namespaces that you can copy into your project in order to get the tests to pass or to get an idea what you can do to fix your code. 24 | 25 | ## The instructions for the tutorial. 26 | 27 | The instructions for the tutorial can be found in `tutorial/chapters/`. 28 | These instructions are written in markdown, so they can be read in a text editor of your choice. 29 | However, we have also written a small clojure web app to generate a website out of the markdown files so that it can be styled and is nicer to read. 30 | 31 | To start the web server: 32 | 33 | cd tutorial 34 | 35 | Then start the server with: 36 | 37 | lein ring server 4000 38 | 39 | OR use the `run_tutorial` script which does this for you: 40 | 41 | ./run_tutorial 42 | 43 | The browser should pop open to the address [http://localhost:4000](http://localhost:4000) and you can see the tutorial 44 | 45 | Once you have started the tutorial, move back into the project root (`cd ..`) and then get started modifying the `todo-app` project. 46 | -------------------------------------------------------------------------------- /todo-app/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /lib 3 | /classes 4 | /checkouts 5 | pom.xml 6 | pom.xml.asc 7 | *.jar 8 | *.class 9 | /.lein-* 10 | /.nrepl-port 11 | -------------------------------------------------------------------------------- /todo-app/README.md: -------------------------------------------------------------------------------- 1 | # todo-app 2 | 3 | FIXME 4 | 5 | ## Prerequisites 6 | 7 | You will need [Leiningen][] 2.0.0 or above installed. 8 | 9 | [leiningen]: https://github.com/technomancy/leiningen 10 | 11 | ## Running 12 | 13 | To start a web server for the application, run: 14 | 15 | lein ring server 16 | 17 | ## License 18 | 19 | Copyright © 2017 FIXME 20 | -------------------------------------------------------------------------------- /todo-app/cheats/01/handler.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.handler 2 | (:require [compojure.core :refer [defroutes GET POST DELETE]] 3 | [compojure.route :as route] 4 | [hiccup.page :refer [doctype include-css]] 5 | [hiccup2.core :refer [html]] 6 | [ring.middleware.defaults :refer [wrap-defaults site-defaults]])) 7 | 8 | (defn page [title & content] 9 | (str 10 | (html 11 | (doctype :html5) 12 | [:html 13 | [:head 14 | [:title title] 15 | (include-css "splendor.css")] 16 | [:body content]]))) 17 | 18 | (defn index [] 19 | (page "TODO App" 20 | [:h1 "TODO App"])) 21 | 22 | (defroutes app-routes 23 | (GET "/" [] (index)) 24 | (route/not-found "Not Found")) 25 | 26 | (def app 27 | (wrap-defaults app-routes site-defaults)) 28 | -------------------------------------------------------------------------------- /todo-app/cheats/01/project.clj: -------------------------------------------------------------------------------- 1 | (defproject todo-app "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :min-lein-version "2.0.0" 5 | :dependencies [[org.clojure/clojure "1.9.0-alpha14"] 6 | [compojure "1.6.0"] 7 | [hiccup "2.0.0-alpha1"] 8 | [ring/ring-core "1.6.1"] 9 | [ring/ring-defaults "0.3.0"] 10 | [ring/ring-jetty-adapter "1.6.1"]] 11 | :plugins [[lein-ring "0.9.7"]] 12 | :main todo-app.core 13 | :ring {:handler todo-app.handler/app} 14 | :profiles 15 | {:dev {:dependencies [[javax.servlet/servlet-api "2.5"] 16 | [ring/ring-mock "0.3.0"]]}}) 17 | -------------------------------------------------------------------------------- /todo-app/cheats/02/domain.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.domain) 2 | 3 | (def ^:private counter (atom 0)) 4 | (def ^:private todos (atom [])) 5 | 6 | (defn gen-id [] (swap! counter inc)) 7 | 8 | (defn add-todo! [text] 9 | (let [new-todo {:id (gen-id) :text text}] 10 | (swap! todos conj new-todo) 11 | new-todo)) 12 | 13 | (defn remove-todo [todos id] 14 | (filterv #(not= id (:id %)) todos)) 15 | 16 | (defn remove-todo! [id] 17 | (swap! todos remove-todo id)) 18 | 19 | (defn all-todos [] @todos) 20 | 21 | (defn todo-by-id [id] 22 | (first (filter #(= id (:id %)) @todos))) -------------------------------------------------------------------------------- /todo-app/cheats/03/handler.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.handler 2 | (:require [compojure.coercions :refer [as-int]] 3 | [compojure.core :refer [defroutes GET POST DELETE]] 4 | [compojure.route :as route] 5 | [hiccup.page :refer [doctype include-css]] 6 | [hiccup2.core :refer [html]] 7 | [hiccup.form :as f] 8 | [hiccup.element :refer [link-to]] 9 | [todo-app.domain :refer [all-todos todo-by-id add-todo! remove-todo!]] 10 | [ring.middleware.defaults :refer [wrap-defaults site-defaults]] 11 | [ring.middleware.anti-forgery :refer [*anti-forgery-token*]] 12 | [ring.util.response :refer [redirect]])) 13 | 14 | (defn page [title & content] 15 | (str 16 | (html 17 | (doctype :html5) 18 | [:html 19 | [:head 20 | [:title title] 21 | (include-css "splendor.css")] 22 | [:body [:h1 title] content]]))) 23 | 24 | (defn anti-forgery-field [] 25 | (f/hidden-field "__anti-forgery-token" *anti-forgery-token*)) 26 | 27 | (defn index [] 28 | (page "TODO App" 29 | [:ul 30 | (for [todo (all-todos)] 31 | [:li (link-to (str "/" (:id todo)) (:text todo))])] 32 | (f/form-to [:post "/"] 33 | (anti-forgery-field) 34 | (f/text-field "todo") 35 | (f/submit-button "Add")))) 36 | 37 | (defn add-todo [todo] 38 | (when (not (clojure.string/blank? todo)) 39 | (add-todo! todo)) 40 | (redirect "/")) 41 | 42 | (defn show-todo [id] 43 | (let [todo (todo-by-id id)] 44 | (when todo 45 | (page (str "TODO " id) 46 | [:h2 (:text todo)] 47 | (f/form-to [:delete (str "/" id)] 48 | (anti-forgery-field) 49 | (f/submit-button "Delete")))))) 50 | 51 | (defn delete-todo [id] 52 | (remove-todo! id) 53 | (redirect "/")) 54 | 55 | (defroutes app-routes 56 | (GET "/" [] (index)) 57 | (POST "/" [todo] (add-todo todo)) 58 | (GET "/:id" [id :<< as-int] (show-todo id)) 59 | (DELETE "/:id" [id :<< as-int] (delete-todo id)) 60 | (route/not-found "Not Found")) 61 | 62 | (def app 63 | (wrap-defaults app-routes site-defaults)) 64 | -------------------------------------------------------------------------------- /todo-app/next: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | current_exercise_no=$(ls test/todo_app/*_tests.clj \ 4 | | grep -oE "test/todo_app/[0-9]{2}_tests.clj" \ 5 | | grep -oE "[0-9]{2}" \ 6 | | sort \ 7 | | tail -n 1) 8 | next_exercise_no="$(printf "%02d" "$(( ${current_exercise_no} + 1 ))")" 9 | 10 | lein test 11 | 12 | rc=$? 13 | if [[ $rc != 0 ]] 14 | then 15 | echo "Try again before going on to the next exercise!" 16 | exit $rc; 17 | fi 18 | 19 | if ! ls tests | grep "${next_exercise_no}_tests.clj" &> /dev/null 20 | then 21 | echo "There are no test for the next exercise!" 22 | echo "You must be done with your web application!" 23 | echo 24 | echo "Congratulations!" 25 | exit 0 26 | fi 27 | 28 | echo "Retrieving tests for $next_exercise_no" 29 | cp tests/${next_exercise_no}_tests.clj test/todo_app 30 | -------------------------------------------------------------------------------- /todo-app/project.clj: -------------------------------------------------------------------------------- 1 | (defproject todo-app "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :min-lein-version "2.0.0" 5 | :dependencies [[org.clojure/clojure "1.9.0-alpha14"] 6 | [compojure "1.6.0"] 7 | [ring/ring-core "1.6.1"] 8 | [ring/ring-defaults "0.3.0"] 9 | [ring/ring-jetty-adapter "1.6.1"]] 10 | :plugins [[lein-ring "0.9.7"]] 11 | :main todo-app.core 12 | :ring {:handler todo-app.handler/app} 13 | :profiles 14 | {:dev {:dependencies [[javax.servlet/servlet-api "2.5"] 15 | [ring/ring-mock "0.3.0"]]}}) 16 | -------------------------------------------------------------------------------- /todo-app/resources/public/splendor.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Splendor CSS was created by [John Otander](http://johnotander.com/) 3 | * [@4lpine](https://twitter.com/4lpine) 4 | * Released under the MIT License: 5 | * https://github.com/markdowncss/splendor 6 | * 7 | * Slightly modified to add support for input fields 8 | */ 9 | @media print { 10 | *, 11 | *:before, 12 | *:after { 13 | background: transparent !important; 14 | color: #000 !important; 15 | box-shadow: none !important; 16 | text-shadow: none !important; 17 | } 18 | 19 | a, 20 | a:visited { 21 | text-decoration: underline; 22 | } 23 | 24 | a[href]:after { 25 | content: " (" attr(href) ")"; 26 | } 27 | 28 | abbr[title]:after { 29 | content: " (" attr(title) ")"; 30 | } 31 | 32 | a[href^="#"]:after, 33 | a[href^="javascript:"]:after { 34 | content: ""; 35 | } 36 | 37 | pre, 38 | blockquote { 39 | border: 1px solid #999; 40 | page-break-inside: avoid; 41 | } 42 | 43 | thead { 44 | display: table-header-group; 45 | } 46 | 47 | tr, 48 | img { 49 | page-break-inside: avoid; 50 | } 51 | 52 | img { 53 | max-width: 100% !important; 54 | } 55 | 56 | p, 57 | h2, 58 | h3 { 59 | orphans: 3; 60 | widows: 3; 61 | } 62 | 63 | h2, 64 | h3 { 65 | page-break-after: avoid; 66 | } 67 | } 68 | 69 | html { 70 | font-size: 12px; 71 | } 72 | 73 | @media screen and (min-width: 32rem) and (max-width: 48rem) { 74 | html { 75 | font-size: 15px; 76 | } 77 | } 78 | 79 | @media screen and (min-width: 48rem) { 80 | html { 81 | font-size: 16px; 82 | } 83 | } 84 | 85 | body { 86 | line-height: 1.85; 87 | } 88 | 89 | p, 90 | .splendor-p { 91 | font-size: 1rem; 92 | margin-bottom: 1.3rem; 93 | } 94 | 95 | h1, 96 | .splendor-h1, 97 | h2, 98 | .splendor-h2, 99 | h3, 100 | .splendor-h3, 101 | h4, 102 | .splendor-h4 { 103 | margin: 1.414rem 0 .5rem; 104 | font-weight: inherit; 105 | line-height: 1.42; 106 | } 107 | 108 | h1, 109 | .splendor-h1 { 110 | margin-top: 0; 111 | font-size: 3.998rem; 112 | } 113 | 114 | h2, 115 | .splendor-h2 { 116 | font-size: 2.827rem; 117 | } 118 | 119 | h3, 120 | .splendor-h3 { 121 | font-size: 1.999rem; 122 | } 123 | 124 | h4, 125 | .splendor-h4 { 126 | font-size: 1.414rem; 127 | } 128 | 129 | h5, 130 | .splendor-h5 { 131 | font-size: 1.121rem; 132 | } 133 | 134 | h6, 135 | .splendor-h6 { 136 | font-size: .88rem; 137 | } 138 | 139 | small, 140 | .splendor-small { 141 | font-size: .707em; 142 | } 143 | 144 | /* https://github.com/mrmrs/fluidity */ 145 | 146 | img, 147 | canvas, 148 | iframe, 149 | video, 150 | svg, 151 | select, 152 | textarea { 153 | max-width: 100%; 154 | } 155 | 156 | @import url(http://fonts.googleapis.com/css?family=Merriweather:300italic,300); 157 | 158 | html { 159 | font-size: 18px; 160 | max-width: 100%; 161 | } 162 | 163 | body { 164 | color: #444; 165 | font-family: 'Merriweather', Georgia, serif; 166 | margin: 0; 167 | max-width: 100%; 168 | } 169 | 170 | /* === A bit of a gross hack so we can have bleeding divs/blockquotes. */ 171 | 172 | p, 173 | *:not(div):not(img):not(body):not(html):not(li):not(blockquote):not(p) { 174 | margin: 1rem auto 1rem; 175 | max-width: 36rem; 176 | padding: .25rem; 177 | } 178 | 179 | div { 180 | width: 100%; 181 | } 182 | 183 | div img { 184 | width: 100%; 185 | } 186 | 187 | blockquote p { 188 | font-size: 1.5rem; 189 | font-style: italic; 190 | margin: 1rem auto 1rem; 191 | max-width: 48rem; 192 | } 193 | 194 | li { 195 | margin-left: 2rem; 196 | } 197 | 198 | /* Counteract the specificity of the gross *:not() chain. */ 199 | 200 | h1 { 201 | padding: 4rem 0 !important; 202 | } 203 | 204 | /* === End gross hack */ 205 | 206 | p { 207 | color: #555; 208 | height: auto; 209 | line-height: 1.45; 210 | } 211 | 212 | pre, 213 | code { 214 | font-family: Menlo, Monaco, "Courier New", monospace; 215 | } 216 | 217 | pre { 218 | background-color: #fafafa; 219 | font-size: .8rem; 220 | overflow-x: scroll; 221 | padding: 1.125em; 222 | } 223 | 224 | a, 225 | a:visited { 226 | color: #3498db; 227 | } 228 | 229 | a:hover, 230 | a:focus, 231 | a:active { 232 | color: #2980b9; 233 | } 234 | 235 | /* FORM ELEMENTS */ 236 | 237 | input[type=text] { 238 | box-sizing: border-box; 239 | border: 1px solid #C2C2C2; 240 | box-shadow: 1px 1px 4px #EBEBEB; 241 | border-radius: 3px; 242 | padding: 7px; 243 | outline: none; 244 | min-width: 150px; 245 | max-width: 350px; 246 | width: 100%; 247 | } 248 | 249 | input[type=submit] { 250 | border: none; 251 | padding: 8px 15px 8px 15px; 252 | background: #3498db; 253 | color: #fff; 254 | box-shadow: 1px 1px 4px #DADADA; 255 | border-radius: 3px; 256 | } -------------------------------------------------------------------------------- /todo-app/src/todo_app/core.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.core 2 | (:require [ring.adapter.jetty :as jetty] 3 | [todo-app.handler :refer [app]])) 4 | 5 | (defn -main [] 6 | (jetty/run-jetty app {:port 3000})) 7 | -------------------------------------------------------------------------------- /todo-app/src/todo_app/domain.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.domain) 2 | 3 | (defn gen-id []) 4 | 5 | (defn add-todo! [text]) 6 | 7 | (defn remove-todo! [id]) 8 | 9 | (defn all-todos []) 10 | 11 | (defn todo-by-id [id]) 12 | -------------------------------------------------------------------------------- /todo-app/src/todo_app/handler.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.handler 2 | (:require [compojure.core :refer [defroutes GET POST DELETE]] 3 | [compojure.route :as route] 4 | [ring.middleware.defaults :refer [wrap-defaults site-defaults]])) 5 | 6 | (defn page [title & content]) 7 | 8 | (defroutes app-routes 9 | (GET "/" [] "Hello World") 10 | (route/not-found "Not Found")) 11 | 12 | (def app 13 | (wrap-defaults app-routes site-defaults)) 14 | -------------------------------------------------------------------------------- /todo-app/test/todo_app/01_tests.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.01-tests 2 | (:require [clojure.test :refer :all] 3 | [clojure.string :as str] 4 | [ring.mock.request :as mock] 5 | [todo-app.handler :refer [page app]])) 6 | 7 | (deftest test-page 8 | (testing "page function" 9 | (let [html (page "Title!" 10 | [:div "Hi!"] 11 | [:ul (for [n [1 2 3]] [:li n])])] 12 | (is (str/includes? html "Title!")) 13 | (is (str/includes? html "
Hi!
")) 14 | (is (str/includes? html ""))))) 15 | 16 | (deftest styles-loaded 17 | (testing "css stylesheet linked" 18 | (let [html (page "Some title")] 19 | (is (str/includes? html ""))))) 20 | 21 | (deftest test-app 22 | (testing "main route" 23 | (let [response (app (mock/request :get "/"))] 24 | (is (= (:status response) 200)) 25 | (is (str/includes? (:body response) "TODO App")) 26 | (is (str/includes? (:body response) "

TODO App

")))) 27 | 28 | (testing "not-found route" 29 | (let [response (app (mock/request :get "/invalid"))] 30 | (is (= (:status response) 404))))) 31 | -------------------------------------------------------------------------------- /todo-app/tests/01_tests.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.01-tests 2 | (:require [clojure.test :refer :all] 3 | [clojure.string :as str] 4 | [ring.mock.request :as mock] 5 | [todo-app.handler :refer [page app]])) 6 | 7 | (deftest test-page 8 | (testing "page function" 9 | (let [html (page "Title!" 10 | [:div "Hi!"] 11 | [:ul (for [n [1 2 3]] [:li n])])] 12 | (is (str/includes? html "Title!")) 13 | (is (str/includes? html "
Hi!
")) 14 | (is (str/includes? html ""))))) 15 | 16 | (deftest styles-loaded 17 | (testing "css stylesheet linked" 18 | (let [html (page "Some title")] 19 | (is (str/includes? html ""))))) 20 | 21 | (deftest test-app 22 | (testing "main route" 23 | (let [response (app (mock/request :get "/"))] 24 | (is (= (:status response) 200)) 25 | (is (str/includes? (:body response) "TODO App")) 26 | (is (str/includes? (:body response) "

TODO App

")))) 27 | 28 | (testing "not-found route" 29 | (let [response (app (mock/request :get "/invalid"))] 30 | (is (= (:status response) 404))))) 31 | -------------------------------------------------------------------------------- /todo-app/tests/02_tests.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.02-tests 2 | (:require [clojure.test :refer :all] 3 | [clojure.spec :as s] 4 | [todo-app.domain :refer [gen-id all-todos todo-by-id 5 | add-todo! remove-todo!]])) 6 | 7 | (deftest test-gen-id 8 | (testing "we want to use an integer as an id" 9 | (is (integer? (gen-id)))) 10 | 11 | (testing "unique id" 12 | (is (not= (gen-id) (gen-id))))) 13 | 14 | (s/def ::id integer?) 15 | (s/def ::text string?) 16 | (s/def :unq/todo (s/keys :req-un [::id ::text])) 17 | 18 | (deftest test-all-todos 19 | (testing "all-todos returns a collection" 20 | (is (coll? (all-todos)))) 21 | 22 | (testing "todo elements conform to the specification {:id integer? :text string?}" 23 | (add-todo! "Make sure todos not empty!") 24 | (s/valid? (s/coll-of :unq/todo) (all-todos)))) 25 | 26 | (deftest test-get-by-id 27 | (testing "Retrieve by id" 28 | (let [todo (add-todo! "Some todo!")] 29 | (is (= (todo-by-id (:id todo)) todo))))) 30 | 31 | (deftest test-add! 32 | (testing "adding todo" 33 | (let [oldcount (count (all-todos)) 34 | newtodo (add-todo! "Do sth cool!")] 35 | (is (= (count (all-todos)) (inc oldcount))) 36 | (is (= (todo-by-id (:id newtodo)) newtodo)) 37 | (is (= (:text newtodo) "Do sth cool!"))))) 38 | 39 | (deftest test-remove! 40 | (testing "removing todo" 41 | (let [oldtodos (all-todos) 42 | newtodo (add-todo! "Do sth cool!") 43 | newtodos (remove-todo! (:id newtodo))] 44 | (is (= (all-todos) oldtodos))))) -------------------------------------------------------------------------------- /todo-app/tests/03_tests.clj: -------------------------------------------------------------------------------- 1 | (ns todo-app.03-tests 2 | (:require [clojure.test :refer :all] 3 | [clojure.string :as str] 4 | [hiccup.core :refer [html]] 5 | [hiccup.element :refer [link-to]] 6 | [ring.mock.request :as mock] 7 | [todo-app.domain :refer [all-todos todo-by-id add-todo!]] 8 | [todo-app.handler :refer [app app-routes]] 9 | [hiccup.form :as f])) 10 | 11 | (deftest test-index 12 | (testing "index" 13 | (let [todos (all-todos) 14 | response (app (mock/request :get "/")) 15 | expected-html (html [:ul (for [todo todos] [:li (link-to (str "/" (:id todo)) (:text todo))])])] 16 | (is (= (:status response) 200)) 17 | (is (str/includes? (:body response) "

TODO App

")) 18 | (is (str/includes? (:body response) expected-html))))) 19 | 20 | (deftest test-add-route 21 | (testing "add route" 22 | (let [oldcount (count (all-todos)) 23 | response (app-routes (-> (mock/request :post "/") 24 | (assoc :params {:todo "Hi!"})))] 25 | (is (= (:status response) 302)) 26 | (is (= (:headers response) {"Location" "/"})) 27 | (is (= (count (all-todos)) (inc oldcount))))) 28 | 29 | (testing "Do not add if todo empty" 30 | (let [oldtodos (all-todos) 31 | response (app-routes (-> (mock/request :post "/") 32 | (assoc :params {:todo ""})))] 33 | (is (= (all-todos) oldtodos)))) 34 | 35 | (testing "Escape todo if evil!" 36 | (let [_ (app-routes (-> (mock/request :post "/") 37 | (assoc :params {:todo ""}))) 38 | response (app-routes (app-routes (mock/request :get "/"))) 39 | responses (map #(app-routes (mock/request :get (str "/" (:id %)))) (all-todos))] 40 | (is (not (str/includes? (:body response) "