├── README.md ├── criando-api-clojure └── hello-api-clojure │ ├── .gitignore │ ├── Capstanfile │ ├── Dockerfile │ ├── README.md │ ├── config │ └── logback.xml │ ├── project.clj │ ├── src │ └── hello_api_clojure │ │ ├── server.clj │ │ └── service.clj │ └── test │ └── hello_api_clojure │ └── service_test.clj ├── criando-funcoes └── criando-funcoes.clj ├── criando-projeto-clojure └── hello-clojure │ ├── .gitignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── doc │ └── intro.md │ ├── project.clj │ ├── src │ └── hello_clojure │ │ ├── core.clj │ │ └── using-core.clj │ └── test │ └── hello_clojure │ └── core_test.clj ├── criando-web-application-clojure └── hello-web-app │ ├── .gitignore │ ├── Capstanfile │ ├── Dockerfile │ ├── Procfile │ ├── README.md │ ├── env │ ├── dev │ │ ├── clj │ │ │ ├── hello_web_app │ │ │ │ ├── dev_middleware.clj │ │ │ │ └── env.clj │ │ │ └── user.clj │ │ └── resources │ │ │ ├── config.edn │ │ │ └── logback.xml │ ├── prod │ │ ├── clj │ │ │ └── hello_web_app │ │ │ │ └── env.clj │ │ └── resources │ │ │ ├── config.edn │ │ │ └── logback.xml │ └── test │ │ └── resources │ │ ├── config.edn │ │ └── logback.xml │ ├── project.clj │ ├── resources │ ├── docs │ │ └── docs.md │ ├── public │ │ ├── css │ │ │ └── screen.css │ │ ├── favicon.ico │ │ └── img │ │ │ └── warning_clojure.png │ └── templates │ │ ├── about.html │ │ ├── base.html │ │ ├── error.html │ │ ├── hello.html │ │ └── home.html │ ├── src │ └── clj │ │ └── hello_web_app │ │ ├── config.clj │ │ ├── core.clj │ │ ├── handler.clj │ │ ├── layout.clj │ │ ├── middleware.clj │ │ └── routes │ │ └── home.clj │ └── test │ └── clj │ └── hello_web_app │ └── test │ └── handler.clj ├── funcoes-clojure └── funcoes-clojure.clj ├── hello-world └── hello-world.clj ├── import-java-clojure ├── Example.java └── import-java-clojure.clj └── todo ├── .gitignore ├── .idea ├── misc.xml ├── modules.xml └── workspace.xml ├── README.md ├── project.clj ├── src └── todo │ ├── database.clj │ ├── handler.clj │ └── query.clj ├── test └── todo │ └── handler_test.clj └── todo.iml /README.md: -------------------------------------------------------------------------------- 1 | Repository used to store codes described in Clojure articles. 2 | Let's try to follow this sequence: 3 | * Hello World; 4 | * Creating functions; 5 | * Using native Clojure functions; 6 | * Create web project; 7 | * Use Java classes in Clojure functions; 8 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/.gitignore: -------------------------------------------------------------------------------- 1 | # Project related 2 | 3 | # Java related 4 | pom.xml 5 | pom.xml.asc 6 | *jar 7 | *.class 8 | 9 | # Leiningen 10 | classes/ 11 | lib/ 12 | native/ 13 | checkouts/ 14 | target/ 15 | .lein-* 16 | repl-port 17 | .nrepl-port 18 | .repl 19 | 20 | # Temp Files 21 | *.orig 22 | *~ 23 | .*.swp 24 | .*.swo 25 | *.tmp 26 | *.bak 27 | 28 | # OS X 29 | .DS_Store 30 | 31 | # Logging 32 | *.log 33 | /logs/ 34 | 35 | # Builds 36 | out/ 37 | build/ 38 | 39 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/Capstanfile: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # Name of the base image. Capstan will download this automatically from 4 | # Cloudius S3 repository. 5 | # 6 | #base: cloudius/osv 7 | base: cloudius/osv-openjdk8 8 | 9 | # 10 | # The command line passed to OSv to start up the application. 11 | # 12 | #cmdline: /java.so -cp /hello-api-clojure/app.jar clojure.main -m hello-api-clojure 13 | cmdline: /java.so -jar /hello-api-clojure/app.jar 14 | 15 | # 16 | # The command to use to build the application. 17 | # You can use any build tool/command (make/rake/lein/boot) - this runs locally on your machine 18 | # 19 | # For Leiningen, you can use: 20 | #build: lein uberjar 21 | # For Boot, you can use: 22 | #build: boot build 23 | 24 | # 25 | # List of files that are included in the generated image. 26 | # 27 | files: 28 | /hello-api-clojure/app.jar: ./target/hello-api-clojure-0.0.1-SNAPSHOT-standalone.jar 29 | 30 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM java:8-alpine 2 | MAINTAINER Your Name 3 | 4 | ADD target/hello-api-clojure-0.0.1-SNAPSHOT-standalone.jar /hello-api-clojure/app.jar 5 | 6 | EXPOSE 8080 7 | 8 | CMD ["java", "-jar", "/hello-api-clojure/app.jar"] 9 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/README.md: -------------------------------------------------------------------------------- 1 | # hello-api-clojure 2 | 3 | FIXME 4 | 5 | ## Getting Started 6 | 7 | 1. Start the application: `lein run` 8 | 2. Go to [localhost:8080](http://localhost:8080/) to see: `Hello World!` 9 | 3. Read your app's source code at src/hello_api_clojure/service.clj. Explore the docs of functions 10 | that define routes and responses. 11 | 4. Run your app's tests with `lein test`. Read the tests at test/hello_api_clojure/service_test.clj. 12 | 5. Learn more! See the [Links section below](#links). 13 | 14 | 15 | ## Configuration 16 | 17 | To configure logging see config/logback.xml. By default, the app logs to stdout and logs/. 18 | To learn more about configuring Logback, read its [documentation](http://logback.qos.ch/documentation.html). 19 | 20 | 21 | ## Developing your service 22 | 23 | 1. Start a new REPL: `lein repl` 24 | 2. Start your service in dev-mode: `(def dev-serv (run-dev))` 25 | 3. Connect your editor to the running REPL session. 26 | Re-evaluated code will be seen immediately in the service. 27 | 28 | ### [Docker](https://www.docker.com/) container support 29 | 30 | 1. Build an uberjar of your service: `lein uberjar` 31 | 2. Build a Docker image: `sudo docker build -t hello-api-clojure .` 32 | 3. Run your Docker image: `docker run -p 8080:8080 hello-api-clojure` 33 | 34 | ### [OSv](http://osv.io/) unikernel support with [Capstan](http://osv.io/capstan/) 35 | 36 | 1. Build and run your image: `capstan run -f "8080:8080"` 37 | 38 | Once the image it built, it's cached. To delete the image and build a new one: 39 | 40 | 1. `capstan rmi hello-api-clojure; capstan build` 41 | 42 | 43 | ## Links 44 | * [Other examples](https://github.com/pedestal/samples) 45 | 46 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/config/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 12 | 13 | 14 | 15 | 16 | logs/hello-api-clojure-%d{yyyy-MM-dd}.%i.log 17 | 18 | 64 MB 19 | 20 | 21 | 22 | true 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | %-5level %logger{36} - %msg%n 31 | 32 | 33 | 34 | INFO 35 | 36 | 37 | 38 | 39 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/project.clj: -------------------------------------------------------------------------------- 1 | (defproject hello-api-clojure "0.0.1-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.8.0"] 7 | [io.pedestal/pedestal.service "0.5.2"] 8 | 9 | ;; Remove this line and uncomment one of the next lines to 10 | ;; use Immutant or Tomcat instead of Jetty: 11 | [io.pedestal/pedestal.jetty "0.5.2"] 12 | ;; [io.pedestal/pedestal.immutant "0.5.2"] 13 | ;; [io.pedestal/pedestal.tomcat "0.5.2"] 14 | 15 | [ch.qos.logback/logback-classic "1.1.8" :exclusions [org.slf4j/slf4j-api]] 16 | [org.slf4j/jul-to-slf4j "1.7.22"] 17 | [org.slf4j/jcl-over-slf4j "1.7.22"] 18 | [org.slf4j/log4j-over-slf4j "1.7.22"]] 19 | :min-lein-version "2.0.0" 20 | :resource-paths ["config", "resources"] 21 | ;; If you use HTTP/2 or ALPN, use the java-agent to pull in the correct alpn-boot dependency 22 | ;:java-agents [[org.mortbay.jetty.alpn/jetty-alpn-agent "2.0.5"]] 23 | :profiles {:dev {:aliases {"run-dev" ["trampoline" "run" "-m" "hello-api-clojure.server/run-dev"]} 24 | :dependencies [[io.pedestal/pedestal.service-tools "0.5.2"]]} 25 | :uberjar {:aot [hello-api-clojure.server]}} 26 | :main ^{:skip-aot true} hello-api-clojure.server) 27 | 28 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/src/hello_api_clojure/server.clj: -------------------------------------------------------------------------------- 1 | (ns hello-api-clojure.server 2 | (:gen-class) ; for -main method in uberjar 3 | (:require [io.pedestal.http :as server] 4 | [io.pedestal.http.route :as route] 5 | [hello-api-clojure.service :as service])) 6 | 7 | ;; This is an adapted service map, that can be started and stopped 8 | ;; From the REPL you can call server/start and server/stop on this service 9 | (defonce runnable-service (server/create-server service/service)) 10 | 11 | (defn run-dev 12 | "The entry-point for 'lein run-dev'" 13 | [& args] 14 | (println "\nCreating your [DEV] server...") 15 | (-> service/service ;; start with production configuration 16 | (merge {:env :dev 17 | ;; do not block thread that starts web server 18 | ::server/join? false 19 | ;; Routes can be a function that resolve routes, 20 | ;; we can use this to set the routes to be reloadable 21 | ::server/routes #(route/expand-routes (deref #'service/routes)) 22 | ;; all origins are allowed in dev mode 23 | ::server/allowed-origins {:creds true :allowed-origins (constantly true)}}) 24 | ;; Wire up interceptor chains 25 | server/default-interceptors 26 | server/dev-interceptors 27 | server/create-server 28 | server/start)) 29 | 30 | (defn -main 31 | "The entry-point for 'lein run'" 32 | [& args] 33 | (println "\nCreating your server...") 34 | (server/start runnable-service)) 35 | 36 | ;; If you package the service up as a WAR, 37 | ;; some form of the following function sections is required (for io.pedestal.servlet.ClojureVarServlet). 38 | 39 | ;;(defonce servlet (atom nil)) 40 | ;; 41 | ;;(defn servlet-init 42 | ;; [_ config] 43 | ;; ;; Initialize your app here. 44 | ;; (reset! servlet (server/servlet-init service/service nil))) 45 | ;; 46 | ;;(defn servlet-service 47 | ;; [_ request response] 48 | ;; (server/servlet-service @servlet request response)) 49 | ;; 50 | ;;(defn servlet-destroy 51 | ;; [_] 52 | ;; (server/servlet-destroy @servlet) 53 | ;; (reset! servlet nil)) 54 | 55 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/src/hello_api_clojure/service.clj: -------------------------------------------------------------------------------- 1 | (ns hello-api-clojure.service 2 | (:require [io.pedestal.http :as http] 3 | [io.pedestal.http.route :as route] 4 | [io.pedestal.http.body-params :as body-params] 5 | [ring.util.response :as ring-resp])) 6 | 7 | (defn get-json 8 | [request] 9 | (ring-resp/response {:foo "foo" :list [1 2 3 4]})) 10 | 11 | (defn about-page 12 | [request] 13 | (ring-resp/response (format "Clojure %s - served from %s" 14 | (clojure-version) 15 | (route/url-for ::about-page)))) 16 | 17 | (defn home-page 18 | [request] 19 | (ring-resp/response "Hello World!")) 20 | 21 | ;; Defines "/" and "/about" routes with their associated :get handlers. 22 | ;; The interceptors defined after the verb map (e.g., {:get home-page} 23 | ;; apply to / and its children (/about). 24 | (def common-interceptors [(body-params/body-params) http/html-body]) 25 | (def common-interceptors-json [(body-params/body-params) http/json-body]) 26 | 27 | 28 | ;; Tabular routes 29 | (def routes #{["/" :get (conj common-interceptors `home-page)] 30 | ["/about" :get (conj common-interceptors `about-page)] 31 | ["/json" :get (conj common-interceptors-json `get-json)]}) 32 | 33 | ;; Map-based routes 34 | ;(def routes `{"/" {:interceptors [(body-params/body-params) http/html-body] 35 | ; :get home-page 36 | ; "/about" {:get about-page}}}) 37 | 38 | ;; Terse/Vector-based routes 39 | ;(def routes 40 | ; `[[["/" {:get home-page} 41 | ; ^:interceptors [(body-params/body-params) http/html-body] 42 | ; ["/about" {:get about-page}]]]]) 43 | 44 | 45 | ;; Consumed by hello-api-clojure.server/create-server 46 | ;; See http/default-interceptors for additional options you can configure 47 | (def service {:env :prod 48 | ;; You can bring your own non-default interceptors. Make 49 | ;; sure you include routing and set it up right for 50 | ;; dev-mode. If you do, many other keys for configuring 51 | ;; default interceptors will be ignored. 52 | ;; ::http/interceptors [] 53 | ::http/routes routes 54 | 55 | ;; Uncomment next line to enable CORS support, add 56 | ;; string(s) specifying scheme, host and port for 57 | ;; allowed source(s): 58 | ;; 59 | ;; "http://localhost:8080" 60 | ;; 61 | ;;::http/allowed-origins ["scheme://host:port"] 62 | 63 | ;; Root for resource interceptor that is available by default. 64 | ::http/resource-path "/public" 65 | 66 | ;; Either :jetty, :immutant or :tomcat (see comments in project.clj) 67 | ::http/type :jetty 68 | ;;::http/host "localhost" 69 | ::http/port 8080 70 | ;; Options to pass to the container (Jetty) 71 | ::http/container-options {:h2c? true 72 | :h2? false 73 | ;:keystore "test/hp/keystore.jks" 74 | ;:key-password "password" 75 | ;:ssl-port 8443 76 | :ssl? false}}) 77 | 78 | -------------------------------------------------------------------------------- /criando-api-clojure/hello-api-clojure/test/hello_api_clojure/service_test.clj: -------------------------------------------------------------------------------- 1 | (ns hello-api-clojure.service-test 2 | (:require [clojure.test :refer :all] 3 | [io.pedestal.test :refer :all] 4 | [io.pedestal.http :as bootstrap] 5 | [hello-api-clojure.service :as service])) 6 | 7 | (def service 8 | (::bootstrap/service-fn (bootstrap/create-servlet service/service))) 9 | 10 | (deftest home-page-test 11 | (is (= 12 | (:body (response-for service :get "/")) 13 | "Hello World!")) 14 | (is (= 15 | (:headers (response-for service :get "/")) 16 | {"Content-Type" "text/html;charset=UTF-8" 17 | "Strict-Transport-Security" "max-age=31536000; includeSubdomains" 18 | "X-Frame-Options" "DENY" 19 | "X-Content-Type-Options" "nosniff" 20 | "X-XSS-Protection" "1; mode=block" 21 | "X-Download-Options" "noopen" 22 | "X-Permitted-Cross-Domain-Policies" "none" 23 | "Content-Security-Policy" "object-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:;"}))) 24 | 25 | (deftest about-page-test 26 | (is (.contains 27 | (:body (response-for service :get "/about")) 28 | "Clojure 1.8")) 29 | (is (= 30 | (:headers (response-for service :get "/about")) 31 | {"Content-Type" "text/html;charset=UTF-8" 32 | "Strict-Transport-Security" "max-age=31536000; includeSubdomains" 33 | "X-Frame-Options" "DENY" 34 | "X-Content-Type-Options" "nosniff" 35 | "X-XSS-Protection" "1; mode=block" 36 | "X-Download-Options" "noopen" 37 | "X-Permitted-Cross-Domain-Policies" "none" 38 | "Content-Security-Policy" "object-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:;"}))) 39 | 40 | -------------------------------------------------------------------------------- /criando-funcoes/criando-funcoes.clj: -------------------------------------------------------------------------------- 1 | ; Exemplos de código de Hello World para os artigos Clojure 2 | ; Artigo Criando funções em Clojure 3 | 4 | ; Usamos defn para definir funções 5 | 6 | (defn hello-world [] 7 | (println "Hello World")) 8 | 9 | (hello-world) 10 | 11 | (defn hello-world-with-param [name] 12 | (println "Hello" name)) 13 | 14 | (hello-world-with-param "Clojure") 15 | 16 | ; Usando args em Clojure - BRINDE do próximo artigo 17 | 18 | (defn hello-world-with-args [& args] 19 | (println args)) 20 | 21 | (hello-world-with-args "Gilmar" "Soares") 22 | 23 | 24 | ; O unico detalhe é que precisamos usar reduce para somar a lista passada por parâmetro 25 | ; esse é um assunto para o próximo artigo 26 | 27 | (defn sum-list [& nums] 28 | (reduce + nums)) 29 | 30 | (sum-list 1 2 3 4 5) 31 | 32 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | ## [Unreleased] 5 | ### Changed 6 | - Add a new arity to `make-widget-async` to provide a different widget shape. 7 | 8 | ## [0.1.1] - 2017-07-29 9 | ### Changed 10 | - Documentation on how to make the widgets. 11 | 12 | ### Removed 13 | - `make-widget-sync` - we're all async, all the time. 14 | 15 | ### Fixed 16 | - Fixed widget maker to keep working when daylight savings switches over. 17 | 18 | ## 0.1.0 - 2017-07-29 19 | ### Added 20 | - Files from the new template. 21 | - Widget maker public API - `make-widget-sync`. 22 | 23 | [Unreleased]: https://github.com/your-name/hello-clojure/compare/0.1.1...HEAD 24 | [0.1.1]: https://github.com/your-name/hello-clojure/compare/0.1.0...0.1.1 25 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor to control, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/README.md: -------------------------------------------------------------------------------- 1 | # hello-clojure 2 | 3 | A Clojure library designed to ... well, that part is up to you. 4 | 5 | ## Usage 6 | 7 | FIXME 8 | 9 | ## License 10 | 11 | Copyright © 2017 FIXME 12 | 13 | Distributed under the Eclipse Public License either version 1.0 or (at 14 | your option) any later version. 15 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to hello-clojure 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/project.clj: -------------------------------------------------------------------------------- 1 | (defproject hello-clojure "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.8.0"]] 7 | :main hello-clojure.core/foo) 8 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/src/hello_clojure/core.clj: -------------------------------------------------------------------------------- 1 | (ns hello-clojure.core) 2 | 3 | (defn foo 4 | "I don't do a whole lot." 5 | [x] 6 | (println x "Hello, World! ")) 7 | 8 | (def param "USANDO NO FOO PARA") 9 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/src/hello_clojure/using-core.clj: -------------------------------------------------------------------------------- 1 | (ns hello-clojure.using-core 2 | (use 3 | [hello-clojure.core :as core])) 4 | 5 | (def x 6 | (core/foo core/param)) 7 | -------------------------------------------------------------------------------- /criando-projeto-clojure/hello-clojure/test/hello_clojure/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns hello-clojure.core-test 2 | (:require [clojure.test :refer :all] 3 | [hello-clojure.core :refer :all])) 4 | 5 | (deftest a-test 6 | (testing "FIXME, I fail." 7 | (is (= 0 1)))) 8 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /lib 3 | /classes 4 | /checkouts 5 | pom.xml 6 | *.jar 7 | *.class 8 | /.lein-* 9 | profiles.clj 10 | /.env 11 | .nrepl-port 12 | /log 13 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/Capstanfile: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # Name of the base image. Capstan will download this automatically from 4 | # Cloudius S3 repository. 5 | # 6 | #base: cloudius/osv 7 | base: cloudius/osv-openjdk8 8 | 9 | # 10 | # The command line passed to OSv to start up the application. 11 | # 12 | cmdline: /java.so -jar /hello-web-app/app.jar 13 | 14 | # 15 | # The command to use to build the application. 16 | # You can use any build tool/command (make/rake/lein/boot) - this runs locally on your machine 17 | # 18 | # For Leiningen, you can use: 19 | #build: lein uberjar 20 | # For Boot, you can use: 21 | #build: boot build 22 | 23 | # 24 | # List of files that are included in the generated image. 25 | # 26 | files: 27 | /hello-web-app/app.jar: ./target/uberjar/hello-web-app.jar 28 | 29 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM java:8-alpine 2 | MAINTAINER Your Name 3 | 4 | ADD target/uberjar/hello-web-app.jar /hello-web-app/app.jar 5 | 6 | EXPOSE 3000 7 | 8 | CMD ["java", "-jar", "/hello-web-app/app.jar"] 9 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/Procfile: -------------------------------------------------------------------------------- 1 | web: java $JVM_OPTS -cp target/uberjar/hello-web-app.jar clojure.main -m hello-web-app.core 2 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/README.md: -------------------------------------------------------------------------------- 1 | # hello-web-app 2 | 3 | generated using Luminus version "2.9.11.78" 4 | 5 | FIXME 6 | 7 | ## Prerequisites 8 | 9 | You will need [Leiningen][1] 2.0 or above installed. 10 | 11 | [1]: https://github.com/technomancy/leiningen 12 | 13 | ## Running 14 | 15 | To start a web server for the application, run: 16 | 17 | lein run 18 | 19 | ## License 20 | 21 | Copyright © 2017 FIXME 22 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/dev/clj/hello_web_app/dev_middleware.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.dev-middleware 2 | (:require [ring.middleware.reload :refer [wrap-reload]] 3 | [selmer.middleware :refer [wrap-error-page]] 4 | [prone.middleware :refer [wrap-exceptions]])) 5 | 6 | (defn wrap-dev [handler] 7 | (-> handler 8 | wrap-reload 9 | wrap-error-page 10 | wrap-exceptions)) 11 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/dev/clj/hello_web_app/env.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.env 2 | (:require [selmer.parser :as parser] 3 | [clojure.tools.logging :as log] 4 | [hello-web-app.dev-middleware :refer [wrap-dev]])) 5 | 6 | (def defaults 7 | {:init 8 | (fn [] 9 | (parser/cache-off!) 10 | (log/info "\n-=[hello-web-app started successfully using the development profile]=-")) 11 | :stop 12 | (fn [] 13 | (log/info "\n-=[hello-web-app has shut down successfully]=-")) 14 | :middleware wrap-dev}) 15 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/dev/clj/user.clj: -------------------------------------------------------------------------------- 1 | (ns user 2 | (:require [mount.core :as mount] 3 | hello-web-app.core)) 4 | 5 | (defn start [] 6 | (mount/start-without #'hello-web-app.core/repl-server)) 7 | 8 | (defn stop [] 9 | (mount/stop-except #'hello-web-app.core/repl-server)) 10 | 11 | (defn restart [] 12 | (stop) 13 | (start)) 14 | 15 | 16 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/dev/resources/config.edn: -------------------------------------------------------------------------------- 1 | {:dev true 2 | :port 3000 3 | ;; when :nrepl-port is set the application starts the nREPL server on load 4 | :nrepl-port 7000} 5 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/dev/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | UTF-8 9 | %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n 10 | 11 | 12 | 13 | log/hello-web-app.log 14 | 15 | log/hello-web-app.%d{yyyy-MM-dd}.%i.log 16 | 17 | 100MB 18 | 19 | 20 | 30 21 | 22 | 23 | UTF-8 24 | %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/prod/clj/hello_web_app/env.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.env 2 | (:require [clojure.tools.logging :as log])) 3 | 4 | (def defaults 5 | {:init 6 | (fn [] 7 | (log/info "\n-=[hello-web-app started successfully]=-")) 8 | :stop 9 | (fn [] 10 | (log/info "\n-=[hello-web-app has shut down successfully]=-")) 11 | :middleware identity}) 12 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/prod/resources/config.edn: -------------------------------------------------------------------------------- 1 | {:production true 2 | :port 3000} 3 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/prod/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | log/hello-web-app.log 6 | 7 | log/hello-web-app.%d{yyyy-MM-dd}.%i.log 8 | 9 | 100MB 10 | 11 | 12 | 30 13 | 14 | 15 | UTF-8 16 | %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/test/resources/config.edn: -------------------------------------------------------------------------------- 1 | {:dev true 2 | :port 3000 3 | ;; when :nrepl-port is set the application starts the nREPL server on load 4 | :nrepl-port 7000} 5 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/env/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | UTF-8 9 | %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n 10 | 11 | 12 | 13 | log/hello-web-app.log 14 | 15 | log/hello-web-app.%d{yyyy-MM-dd}.%i.log 16 | 17 | 100MB 18 | 19 | 20 | 30 21 | 22 | 23 | UTF-8 24 | %date{ISO8601} [%thread] %-5level %logger{36} - %msg %n 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/project.clj: -------------------------------------------------------------------------------- 1 | (defproject hello-web-app "0.1.0-SNAPSHOT" 2 | 3 | :description "FIXME: write description" 4 | :url "http://example.com/FIXME" 5 | 6 | :dependencies [[clj-time "0.14.0"] 7 | [compojure "1.6.0"] 8 | [cprop "0.1.11"] 9 | [funcool/struct "1.0.0"] 10 | [luminus-immutant "0.2.3"] 11 | [luminus-nrepl "0.1.4"] 12 | [luminus/ring-ttl-session "0.3.2"] 13 | [markdown-clj "0.9.99"] 14 | [metosin/muuntaja "0.3.2"] 15 | [metosin/ring-http-response "0.9.0"] 16 | [mount "0.1.11"] 17 | [org.clojure/clojure "1.8.0"] 18 | [org.clojure/tools.cli "0.3.5"] 19 | [org.clojure/tools.logging "0.4.0"] 20 | [org.webjars.bower/tether "1.4.0"] 21 | [org.webjars/bootstrap "4.0.0-alpha.5"] 22 | [org.webjars/font-awesome "4.7.0"] 23 | [org.webjars/jquery "3.2.1"] 24 | [ring-webjars "0.2.0"] 25 | [ring/ring-core "1.6.2"] 26 | [ring/ring-defaults "0.3.1"] 27 | [selmer "1.11.0"]] 28 | 29 | :min-lein-version "2.0.0" 30 | 31 | :jvm-opts ["-server" "-Dconf=.lein-env"] 32 | :source-paths ["src/clj"] 33 | :test-paths ["test/clj"] 34 | :resource-paths ["resources"] 35 | :target-path "target/%s/" 36 | :main ^:skip-aot hello-web-app.core 37 | 38 | :plugins [[lein-cprop "1.0.3"] 39 | [lein-immutant "2.1.0"]] 40 | 41 | :profiles 42 | {:uberjar {:omit-source true 43 | :aot :all 44 | :uberjar-name "hello-web-app.jar" 45 | :source-paths ["env/prod/clj"] 46 | :resource-paths ["env/prod/resources"]} 47 | 48 | :dev [:project/dev :profiles/dev] 49 | :test [:project/dev :project/test :profiles/test] 50 | 51 | :project/dev {:dependencies [[prone "1.1.4"] 52 | [ring/ring-mock "0.3.1"] 53 | [ring/ring-devel "1.6.2"] 54 | [pjstadig/humane-test-output "0.8.2"]] 55 | :plugins [[com.jakemccrary/lein-test-refresh "0.19.0"]] 56 | 57 | :source-paths ["env/dev/clj"] 58 | :resource-paths ["env/dev/resources"] 59 | :repl-options {:init-ns user} 60 | :injections [(require 'pjstadig.humane-test-output) 61 | (pjstadig.humane-test-output/activate!)]} 62 | :project/test {:resource-paths ["env/test/resources"]} 63 | :profiles/dev {} 64 | :profiles/test {}}) 65 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/docs/docs.md: -------------------------------------------------------------------------------- 1 |

Congratulations, your Luminus site is ready!

2 | 3 | This page will help guide you through the first steps of building your site. 4 | 5 | #### Why are you seeing this page? 6 | 7 | The `home-routes` handler in the `hello-web-app.routes.home` namespace 8 | defines the route that invokes the `home-page` function whenever an HTTP 9 | request is made to the `/` URI using the `GET` method. 10 | 11 | ``` 12 | (defroutes home-routes 13 | (GET "/" [] (home-page)) 14 | (GET "/about" [] (about-page))) 15 | ``` 16 | 17 | The `home-page` function will in turn call the `hello-web-app.layout/render` function 18 | to render the HTML content: 19 | 20 | ``` 21 | (defn home-page [] 22 | (layout/render 23 | "home.html" {:docs (-> "docs/docs.md" io/resource slurp)})) 24 | ``` 25 | 26 | The `render` function will render the `home.html` template found in the `resources/templates` 27 | folder using a parameter map containing the `:docs` key. This key points to the 28 | contents of the `resources/docs/docs.md` file containing these instructions. 29 | 30 | 31 | The HTML templates are written using [Selmer](https://github.com/yogthos/Selmer) templating engine. 32 | 33 | 34 | ``` 35 |
36 |
37 | {{docs|markdown}} 38 |
39 |
40 | ``` 41 | 42 | learn more about HTML templating » 43 | 44 | 45 | 46 | #### Organizing the routes 47 | 48 | The routes are aggregated and wrapped with middleware in the `hello-web-app.handler` namespace: 49 | 50 | ``` 51 | (def app-routes 52 | (routes 53 | (-> #'home-routes 54 | (wrap-routes middleware/wrap-csrf) 55 | (wrap-routes middleware/wrap-formats)) 56 | (route/not-found 57 | (:body 58 | (error-page {:status 404 59 | :title "page not found"}))))) 60 | ``` 61 | 62 | The `app-routes` definition groups all the routes in the application into a single handler. 63 | A default route group is added to handle the `404` case. 64 | 65 | learn more about routing » 66 | 67 | The `home-routes` are wrapped with two middleware functions. The first enables CSRF protection. 68 | The second takes care of serializing and deserializing various encoding formats, such as JSON. 69 | 70 | #### Managing your middleware 71 | 72 | Request middleware functions are located under the `hello-web-app.middleware` namespace. 73 | 74 | This namespace is reserved for any custom middleware for the application. Some default middleware is 75 | already defined here. The middleware is assembled in the `wrap-base` function. 76 | 77 | Middleware used for development is placed in the `hello-web-app.dev-middleware` namespace found in 78 | the `env/dev/clj/` source path. 79 | 80 | learn more about middleware » 81 | 82 | 83 | 84 | 85 | #### Need some help? 86 | 87 | Visit the [official documentation](http://www.luminusweb.net/docs) for examples 88 | 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. 89 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/public/css/screen.css: -------------------------------------------------------------------------------- 1 | html, 2 | body { 3 | font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; 4 | height: 100%; 5 | } 6 | .navbar { 7 | margin-bottom: 10px; 8 | border-radius: 0px; 9 | } 10 | .navbar-brand { 11 | float: none; 12 | } 13 | .navbar-nav .nav-item { 14 | float: none; 15 | } 16 | .navbar-divider, 17 | .navbar-nav .nav-item+.nav-item, 18 | .navbar-nav .nav-link + .nav-link { 19 | margin-left: 0; 20 | } 21 | @media (min-width: 34em) { 22 | .navbar-brand { 23 | float: left; 24 | } 25 | .navbar-nav .nav-item { 26 | float: left; 27 | } 28 | .navbar-divider, 29 | .navbar-nav .nav-item+.nav-item, 30 | .navbar-nav .nav-link + .nav-link { 31 | margin-left: 1rem; 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linuxsoares/artigos-clojure/6c4183e767e71168c778973dd5831e7c9edfaf4a/criando-web-application-clojure/hello-web-app/resources/public/favicon.ico -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/public/img/warning_clojure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linuxsoares/artigos-clojure/6c4183e767e71168c778973dd5831e7c9edfaf4a/criando-web-application-clojure/hello-web-app/resources/public/img/warning_clojure.png -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/templates/about.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | {% block content %} 3 | 4 | {% endblock %} 5 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Welcome to hello-web-app 7 | 8 | 9 | {% style "/assets/bootstrap/css/bootstrap.min.css" %} 10 | {% style "/assets/font-awesome/css/font-awesome.min.css" %} 11 | 12 | {% style "/css/screen.css" %} 13 | 14 | 15 | 16 | 47 | 48 |
49 | {% block content %} 50 | {% endblock %} 51 |
52 | 53 | 54 | {% script "/assets/jquery/jquery.min.js" %} 55 | {% script "/assets/tether/dist/js/tether.min.js" %} 56 | {% script "/assets/bootstrap/js/bootstrap.min.js" %} 57 | 58 | 61 | {% block page-scripts %} 62 | {% endblock %} 63 | 64 | 65 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/templates/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Something bad happened 5 | 6 | 7 | {% style "/assets/bootstrap/css/bootstrap.min.css" %} 8 | {% style "/assets/bootstrap/css/bootstrap-theme.min.css" %} 9 | 35 | 36 | 37 |
38 |
39 |
40 |
41 |
42 |

Error: {{status}}

43 |
44 | {% if title %} 45 |

{{title}}

46 | {% endif %} 47 | {% if message %} 48 |

{{message}}

49 | {% endif %} 50 |
51 |
52 |
53 |
54 |
55 | 56 | 57 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/templates/hello.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | {% block content %} 3 |
4 |

HELLO WEBAPP CLOJURE

5 |
6 | {% endblock %} 7 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | {% block content %} 3 |
4 |
5 | {{docs|markdown}} 6 |
7 |
8 | {% endblock %} 9 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/src/clj/hello_web_app/config.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.config 2 | (:require [cprop.core :refer [load-config]] 3 | [cprop.source :as source] 4 | [mount.core :refer [args defstate]])) 5 | 6 | (defstate env :start (load-config 7 | :merge 8 | [(args) 9 | (source/from-system-props) 10 | (source/from-env)])) 11 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/src/clj/hello_web_app/core.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.core 2 | (:require [hello-web-app.handler :as handler] 3 | [luminus.repl-server :as repl] 4 | [luminus.http-server :as http] 5 | [hello-web-app.config :refer [env]] 6 | [clojure.tools.cli :refer [parse-opts]] 7 | [clojure.tools.logging :as log] 8 | [mount.core :as mount]) 9 | (:gen-class)) 10 | 11 | (def cli-options 12 | [["-p" "--port PORT" "Port number" 13 | :parse-fn #(Integer/parseInt %)]]) 14 | 15 | (mount/defstate ^{:on-reload :noop} 16 | http-server 17 | :start 18 | (http/start 19 | (-> env 20 | (assoc :handler (handler/app)) 21 | (update :port #(or (-> env :options :port) %)))) 22 | :stop 23 | (http/stop http-server)) 24 | 25 | (mount/defstate ^{:on-reload :noop} 26 | repl-server 27 | :start 28 | (when-let [nrepl-port (env :nrepl-port)] 29 | (repl/start {:port nrepl-port})) 30 | :stop 31 | (when repl-server 32 | (repl/stop repl-server))) 33 | 34 | 35 | (defn stop-app [] 36 | (doseq [component (:stopped (mount/stop))] 37 | (log/info component "stopped")) 38 | (shutdown-agents)) 39 | 40 | (defn start-app [args] 41 | (doseq [component (-> args 42 | (parse-opts cli-options) 43 | mount/start-with-args 44 | :started)] 45 | (log/info component "started")) 46 | (.addShutdownHook (Runtime/getRuntime) (Thread. stop-app))) 47 | 48 | (defn -main [& args] 49 | (start-app args)) 50 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/src/clj/hello_web_app/handler.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.handler 2 | (:require [compojure.core :refer [routes wrap-routes]] 3 | [hello-web-app.layout :refer [error-page]] 4 | [hello-web-app.routes.home :refer [home-routes]] 5 | [compojure.route :as route] 6 | [hello-web-app.env :refer [defaults]] 7 | [mount.core :as mount] 8 | [hello-web-app.middleware :as middleware])) 9 | 10 | (mount/defstate init-app 11 | :start ((or (:init defaults) identity)) 12 | :stop ((or (:stop defaults) identity))) 13 | 14 | (def app-routes 15 | (routes 16 | (-> #'home-routes 17 | (wrap-routes middleware/wrap-csrf) 18 | (wrap-routes middleware/wrap-formats)) 19 | (route/not-found 20 | (:body 21 | (error-page {:status 404 22 | :title "page not found"}))))) 23 | 24 | 25 | (defn app [] (middleware/wrap-base #'app-routes)) 26 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/src/clj/hello_web_app/layout.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.layout 2 | (:require [selmer.parser :as parser] 3 | [selmer.filters :as filters] 4 | [markdown.core :refer [md-to-html-string]] 5 | [ring.util.http-response :refer [content-type ok]] 6 | [ring.util.anti-forgery :refer [anti-forgery-field]] 7 | [ring.middleware.anti-forgery :refer [*anti-forgery-token*]])) 8 | 9 | (declare ^:dynamic *app-context*) 10 | (parser/set-resource-path! (clojure.java.io/resource "templates")) 11 | (parser/add-tag! :csrf-field (fn [_ _] (anti-forgery-field))) 12 | (filters/add-filter! :markdown (fn [content] [:safe (md-to-html-string content)])) 13 | 14 | (defn render 15 | "renders the HTML template located relative to resources/templates" 16 | [template & [params]] 17 | (content-type 18 | (ok 19 | (parser/render-file 20 | template 21 | (assoc params 22 | :page template 23 | :csrf-token *anti-forgery-token* 24 | :servlet-context *app-context*))) 25 | "text/html; charset=utf-8")) 26 | 27 | (defn error-page 28 | "error-details should be a map containing the following keys: 29 | :status - error status 30 | :title - error title (optional) 31 | :message - detailed error message (optional) 32 | 33 | returns a response map with the error page as the body 34 | and the status specified by the status key" 35 | [error-details] 36 | {:status (:status error-details) 37 | :headers {"Content-Type" "text/html; charset=utf-8"} 38 | :body (parser/render-file "error.html" error-details)}) 39 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/src/clj/hello_web_app/middleware.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.middleware 2 | (:require [hello-web-app.env :refer [defaults]] 3 | [cognitect.transit :as transit] 4 | [clojure.tools.logging :as log] 5 | [hello-web-app.layout :refer [*app-context* error-page]] 6 | [ring.middleware.anti-forgery :refer [wrap-anti-forgery]] 7 | [ring.middleware.webjars :refer [wrap-webjars]] 8 | [muuntaja.core :as muuntaja] 9 | [muuntaja.format.transit :as transit-format] 10 | [muuntaja.middleware :refer [wrap-format wrap-params]] 11 | [hello-web-app.config :refer [env]] 12 | [ring.middleware.flash :refer [wrap-flash]] 13 | [immutant.web.middleware :refer [wrap-session]] 14 | [ring.middleware.defaults :refer [site-defaults wrap-defaults]]) 15 | (:import [javax.servlet ServletContext] 16 | [org.joda.time ReadableInstant])) 17 | 18 | (defn wrap-context [handler] 19 | (fn [request] 20 | (binding [*app-context* 21 | (if-let [context (:servlet-context request)] 22 | ;; If we're not inside a servlet environment 23 | ;; (for example when using mock requests), then 24 | ;; .getContextPath might not exist 25 | (try (.getContextPath ^ServletContext context) 26 | (catch IllegalArgumentException _ context)) 27 | ;; if the context is not specified in the request 28 | ;; we check if one has been specified in the environment 29 | ;; instead 30 | (:app-context env))] 31 | (handler request)))) 32 | 33 | (defn wrap-internal-error [handler] 34 | (fn [req] 35 | (try 36 | (handler req) 37 | (catch Throwable t 38 | (log/error t) 39 | (error-page {:status 500 40 | :title "Something very bad has happened!" 41 | :message "We've dispatched a team of highly trained gnomes to take care of the problem."}))))) 42 | 43 | (defn wrap-csrf [handler] 44 | (wrap-anti-forgery 45 | handler 46 | {:error-response 47 | (error-page 48 | {:status 403 49 | :title "Invalid anti-forgery token"})})) 50 | 51 | (def joda-time-writer 52 | (transit/write-handler 53 | (constantly "m") 54 | (fn [v] (-> ^ReadableInstant v .getMillis)) 55 | (fn [v] (-> ^ReadableInstant v .getMillis .toString)))) 56 | 57 | (def restful-format-options 58 | (update 59 | muuntaja/default-options 60 | :formats 61 | merge 62 | {"application/transit+json" 63 | {:decoder [(partial transit-format/make-transit-decoder :json)] 64 | :encoder [#(transit-format/make-transit-encoder 65 | :json 66 | (merge 67 | % 68 | {:handlers {org.joda.time.DateTime joda-time-writer}}))]}})) 69 | 70 | (defn wrap-formats [handler] 71 | (let [wrapped (-> handler wrap-params (wrap-format restful-format-options))] 72 | (fn [request] 73 | ;; disable wrap-formats for websockets 74 | ;; since they're not compatible with this middleware 75 | ((if (:websocket? request) handler wrapped) request)))) 76 | 77 | (defn wrap-base [handler] 78 | (-> ((:middleware defaults) handler) 79 | wrap-webjars 80 | wrap-flash 81 | (wrap-session {:cookie-attrs {:http-only true}}) 82 | (wrap-defaults 83 | (-> site-defaults 84 | (assoc-in [:security :anti-forgery] false) 85 | (dissoc :session))) 86 | wrap-context 87 | wrap-internal-error)) 88 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/src/clj/hello_web_app/routes/home.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.routes.home 2 | (:require [hello-web-app.layout :as layout] 3 | [compojure.core :refer [defroutes GET]] 4 | [ring.util.http-response :as response] 5 | [clojure.java.io :as io])) 6 | 7 | (defn home-page [] 8 | (layout/render 9 | "home.html" {:docs (-> "docs/docs.md" io/resource slurp)})) 10 | 11 | (defn hello [] 12 | (layout/render 13 | "hello.html")) 14 | 15 | (defn about-page [] 16 | (layout/render "about.html")) 17 | 18 | (defroutes home-routes 19 | (GET "/" [] (home-page)) 20 | (GET "/hello" [] (hello)) 21 | (GET "/about" [] (about-page))) 22 | 23 | -------------------------------------------------------------------------------- /criando-web-application-clojure/hello-web-app/test/clj/hello_web_app/test/handler.clj: -------------------------------------------------------------------------------- 1 | (ns hello-web-app.test.handler 2 | (:require [clojure.test :refer :all] 3 | [ring.mock.request :refer :all] 4 | [hello-web-app.handler :refer :all])) 5 | 6 | (deftest test-app 7 | (testing "main route" 8 | (let [response ((app) (request :get "/"))] 9 | (is (= 200 (:status response))))) 10 | 11 | (testing "not-found route" 12 | (let [response ((app) (request :get "/invalid"))] 13 | (is (= 404 (:status response)))))) 14 | -------------------------------------------------------------------------------- /funcoes-clojure/funcoes-clojure.clj: -------------------------------------------------------------------------------- 1 | ; Exemplos usados no terceiro artigo de Clojure. 2 | 3 | ; Função map, espera uma função e uma lista para executar uma ação em cada item da lista 4 | (map inc [1 2 3 4 5 6]) 5 | 6 | ; Basicamente reduz a uma lista executando uma acação, no caso abaixo uma soma 7 | (reduce + [1 2 3]) 8 | 9 | 10 | ; Função filter, como o proprio nome diz, serve para filtrar itens de uma lista 11 | (filter even? [12 2 3 4]) 12 | 13 | 14 | ; Abaixo definimos uma função pares, que não faz nada além de receber uma lista e filtrar os pares com a 15 | ; função filter 16 | (defn pares [& args] 17 | (filter even? args)) 18 | 19 | (pares 1 2 3 4 5 6 7 8 9 10) 20 | 21 | 22 | -------------------------------------------------------------------------------- /hello-world/hello-world.clj: -------------------------------------------------------------------------------- 1 | ; Exemplos de código de Hello World para os artigos Clojure 2 | ; Artigo Hello World em Clojure 3 | 4 | 5 | ; (println ) 6 | 7 | (println "Hello World") 8 | 9 | ; (println ) 10 | 11 | (println "Hello" "World") 12 | 13 | ; Criando variaveis e utilizando como parametros para a função println 14 | 15 | (def name "Gilmar") 16 | 17 | ; ( ) 18 | 19 | (println "Hello" name) 20 | -------------------------------------------------------------------------------- /import-java-clojure/Example.java: -------------------------------------------------------------------------------- 1 | import java.util.Date; 2 | import java.text.SimpleDateFormat; 3 | 4 | public class Example{ 5 | public static void main(String args[]){ 6 | SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy"); 7 | String date = sdf.format(new Date()); 8 | System.out.println(date); 9 | 10 | String s = "clojure with java"; 11 | System.out.println(s.toUpperCase()); 12 | 13 | System.out.println("JVM VERSION: " + System.getProperty("java.vm.version")); 14 | } 15 | } -------------------------------------------------------------------------------- /import-java-clojure/import-java-clojure.clj: -------------------------------------------------------------------------------- 1 | (ns import-java-clojure.import-java-clojure 2 | (:import 3 | java.util.Date 4 | java.text.SimpleDateFormat)) 5 | 6 | 7 | 8 | (def date (Date.)) 9 | (def sdf (SimpleDateFormat. "MM/dd/yyyy")) 10 | 11 | (println (.toUpperCase "clojure with java")) 12 | 13 | 14 | (.format (SimpleDateFormat. "MM/dd/yyyy") (Date.)) 15 | 16 | (.format sdf (Date.)) 17 | 18 | (.format (SimpleDateFormat. "MM/dd/yyyy") date) 19 | 20 | (.format sdf date) 21 | 22 | (println (System/getProperty "java.vm.version")) 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /todo/.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/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /todo/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /todo/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 60 | 61 | 62 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 |