Browse files

Migrate in Pedestal Service samples

  • Loading branch information...
1 parent 37c38a1 commit d54e5824cb573600420cdccacfdb035b8776855c @ohpauleez ohpauleez committed Jan 30, 2014
Showing with 2,231 additions and 0 deletions.
  1. +44 −0 samples/README.md
  2. +3 −0 samples/auto-reload-server/.gitignore
  3. +20 −0 samples/auto-reload-server/README.md
  4. +52 −0 samples/auto-reload-server/config/logback.xml
  5. +54 −0 samples/auto-reload-server/dev/dev.clj
  6. +5 −0 samples/auto-reload-server/dev/user.clj
  7. +25 −0 samples/auto-reload-server/project.clj
  8. +31 −0 samples/auto-reload-server/src/auto_reload_server/server.clj
  9. +49 −0 samples/auto-reload-server/src/auto_reload_server/service.clj
  10. +24 −0 samples/auto-reload-server/test/auto_reload_server/service_test.clj
  11. +2 −0 samples/cors/.gitignore
  12. +48 −0 samples/cors/README.md
  13. +52 −0 samples/cors/config/logback.xml
  14. +40 −0 samples/cors/dev/dev.clj
  15. +17 −0 samples/cors/dev/user.clj
  16. +28 −0 samples/cors/project.clj
  17. +13 −0 samples/cors/src/blob.html
  18. +49 −0 samples/cors/src/cors/server.clj
  19. +71 −0 samples/cors/src/cors/service.clj
  20. +21 −0 samples/cors/test/cors/service_test.clj
  21. +3 −0 samples/jboss/.gitignore
  22. +29 −0 samples/jboss/README.md
  23. +52 −0 samples/jboss/config/logback.xml
  24. +30 −0 samples/jboss/dev/dev.clj
  25. +5 −0 samples/jboss/dev/user.clj
  26. +27 −0 samples/jboss/project.clj
  27. +24 −0 samples/jboss/src/immutant/init.clj
  28. +42 −0 samples/jboss/src/jboss/server.clj
  29. +51 −0 samples/jboss/src/jboss/service.clj
  30. +18 −0 samples/jboss/test/jboss/service_test.clj
  31. +2 −0 samples/ring-middleware/.gitignore
  32. +53 −0 samples/ring-middleware/README.md
  33. +52 −0 samples/ring-middleware/config/logback.xml
  34. +39 −0 samples/ring-middleware/dev/dev.clj
  35. +27 −0 samples/ring-middleware/project.clj
  36. +16 −0 samples/ring-middleware/src/hello-form.html
  37. +48 −0 samples/ring-middleware/src/ring_middleware/server.clj
  38. +106 −0 samples/ring-middleware/src/ring_middleware/service.clj
  39. +25 −0 samples/ring-middleware/test/ring_middleware/service_test.clj
  40. +3 −0 samples/server-sent-events/.gitignore
  41. +18 −0 samples/server-sent-events/README.md
  42. +52 −0 samples/server-sent-events/config/logback.xml
  43. +39 −0 samples/server-sent-events/dev/dev.clj
  44. +5 −0 samples/server-sent-events/dev/user.clj
  45. +22 −0 samples/server-sent-events/project.clj
  46. +47 −0 samples/server-sent-events/src/server_sent_events/server.clj
  47. +44 −0 samples/server-sent-events/src/server_sent_events/service.clj
  48. +18 −0 samples/server-sent-events/test/server_sent_events/service_test.clj
  49. +2 −0 samples/server-with-links/.gitignore
  50. +24 −0 samples/server-with-links/README.md
  51. +52 −0 samples/server-with-links/config/logback.xml
  52. +39 −0 samples/server-with-links/dev/dev.clj
  53. +27 −0 samples/server-with-links/project.clj
  54. +48 −0 samples/server-with-links/src/server_with_links/server.clj
  55. +59 −0 samples/server-with-links/src/server_with_links/service.clj
  56. +25 −0 samples/server-with-links/test/server_with_links/service_test.clj
  57. +2 −0 samples/template-server/.gitignore
  58. +39 −0 samples/template-server/README.md
  59. +52 −0 samples/template-server/config/logback.xml
  60. +39 −0 samples/template-server/dev/dev.clj
  61. +17 −0 samples/template-server/dev/user.clj
  62. +32 −0 samples/template-server/project.clj
  63. +6 −0 samples/template-server/resources/public/comb.html
  64. +12 −0 samples/template-server/resources/public/enlive-template.html
  65. +10 −0 samples/template-server/resources/public/mustache-template.html
  66. +48 −0 samples/template-server/src/template_server/server.clj
  67. +121 −0 samples/template-server/src/template_server/service.clj
  68. +32 −0 samples/template-server/test/template_server/service_test.clj
View
44 samples/README.md
@@ -0,0 +1,44 @@
+# Pedestal Samples
+
+This is a collection of examples of services using the
+[pedestal](http://pedestal.io) toolkit. They range from [very
+simple](./helloworld_app) to [quite complex](./chat).
+
+If you're new to pedestal, we recommend that you have a look at [our
+documentation](http://pedestal.io/documentation) and then start with the
+examples.
+
+## Samples
+
+* [auto-reload-server](./auto-reload-server), a **pedestal-service** that
+ uses [ns-tracker](https://github.com/weavejester/ns-tracker) to reload
+ changed code
+* [cors](./cors)
+* [jboss](./jboss), a **pedestal-service** that can be deployed in
+ [JBoss](http://jboss.org)
+* [ring-middleware](./ring-middleware), shows how to use
+ [ring](https://github.com/ring-clojure/ring) middlewares with
+ **pedestal-services**
+* [server-sent-events](./server-sent-events)
+* [server-with-links](./server-with-links), a **pedestal-service** that
+ demonstrates links generated from routes
+* [template-server](./template-server), a **pedestal-service** that shows
+ how to use different template engines
+
+## Contributing
+
+The Pedestal samples follow a similar procedure to Pedestal's [CONTRIBUTING.md](https://github.com/pedestal/pedestal/blob/master/CONTRIBUTING.md).
+
+## License
+Copyright 2013 Relevance, Inc.
+Copyright 2014 Cognitect, Inc.
+
+The use and distribution terms for this software are covered by the
+Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+which can be found in the file [epl-v10.html](epl-v10.html) at the root of this distribution.
+
+By using this software in any fashion, you are agreeing to be bound by
+the terms of this license.
+
+You must not remove this notice, or any other, from this software.
+
View
3 samples/auto-reload-server/.gitignore
@@ -0,0 +1,3 @@
+target/
+tmp/
+logs/
View
20 samples/auto-reload-server/README.md
@@ -0,0 +1,20 @@
+# auto-reload-server
+
+FIXME
+
+## Getting Started
+
+1. Start the application: `lein run`
+2. Go to [localhost:8080](http://localhost:8080/) to see: `Hello World!`
+3. Read your app's source code at src/auto_reload_server/service.clj. Explore the docs of functions
+ that define routes and responses.
+4. Run your app's tests with `lein test`. Read the tests at test/auto_reload_server/service_test.clj.
+5. Learn more! See the [Links section below](#links).
+
+## Configuration
+
+To configure logging see config/logback.xml. By default, the app logs to stdout and logs/.
+To learn more about configuring Logback, read its [documentation](http://logback.qos.ch/documentation.html).
+
+## Links
+* [Other examples](https://github.com/pedestal/samples)
View
52 samples/auto-reload-server/config/logback.xml
@@ -0,0 +1,52 @@
+<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
+<configuration scan="true" scanPeriod="10 seconds">
+
+ <!-- Simple file output -->
+ <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+
+ <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <!-- rollover daily -->
+ <fileNamePattern>logs/auto-reload-server-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+ <timeBasedFileNamingAndTriggeringPolicy
+ class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+ <!-- or whenever the file size reaches 64 MB -->
+ <maxFileSize>64 MB</maxFileSize>
+ </timeBasedFileNamingAndTriggeringPolicy>
+ </rollingPolicy>
+
+ <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->
+ <prudent>true</prudent>
+ </appender>
+
+
+ <!-- Console output -->
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ <!-- Only log level INFO and above -->
+ <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+ <level>INFO</level>
+ </filter>
+ </appender>
+
+
+ <!-- Enable FILE and STDOUT appenders for all log messages.
+ By default, only log at level INFO and above. -->
+ <root level="INFO">
+ <appender-ref ref="FILE" />
+ <appender-ref ref="STDOUT" />
+ </root>
+
+ <!-- For loggers in the these namespaces, log at all levels. -->
+ <logger name="user" level="ALL" />
+ <!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG
+ <logger name="pedestal" level="ALL" />
+ -->
+
+</configuration>
View
54 samples/auto-reload-server/dev/dev.clj
@@ -0,0 +1,54 @@
+(ns dev
+ (:require [io.pedestal.service.http :as bootstrap]
+ [auto-reload-server.service :as service]
+ [auto-reload-server.server :as server]
+ [ns-tracker.core :as tracker]))
+
+(def service (-> service/service ;; start with production configuration
+ (merge {:env :dev
+ ;; do not block thread that starts web server
+ ::bootstrap/join? false
+ ;; reload routes on every request
+ ::bootstrap/routes #(deref #'service/routes)
+ ;; all origins are allowed in dev mode
+ ::bootstrap/allowed-origins (constantly true)})
+ (bootstrap/default-interceptors)
+ (bootstrap/dev-interceptors)))
+
+(defn start
+ [& [opts]]
+ (server/create-server (merge service opts))
+ (bootstrap/start server/service-instance))
+
+(defn stop
+ []
+ (bootstrap/stop server/service-instance))
+
+(defn restart
+ []
+ (stop)
+ (start))
+
+(defn- ns-reload [track]
+ (try
+ (doseq [ns-sym (track)]
+ (require ns-sym :reload))
+ (catch Throwable e (.printStackTrace e))))
+
+(defn watch
+ ([] (watch ["src"]))
+ ([src-paths]
+ (let [track (tracker/ns-tracker src-paths)
+ done (atom false)]
+ (doto
+ (Thread. (fn []
+ (while (not @done)
+ (ns-reload track)
+ (Thread/sleep 500))))
+ (.setDaemon true)
+ (.start))
+ (fn [] (swap! done not)))))
+
+(defn -main [& args]
+ (start)
+ (watch))
View
5 samples/auto-reload-server/dev/user.clj
@@ -0,0 +1,5 @@
+(defn dev
+ []
+ (require 'dev)
+ (in-ns 'dev)
+ #_(dev/start))
View
25 samples/auto-reload-server/project.clj
@@ -0,0 +1,25 @@
+(defproject auto-reload-server "0.0.1-SNAPSHOT"
+ :description "FIXME: write description"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.5.0"]
+ [io.pedestal/pedestal.service "0.1.9-SNAPSHOT"]
+
+ ;; Remove this line and uncomment the next line to
+ ;; use Tomcat instead of Jetty:
+ [io.pedestal/pedestal.jetty "0.1.9-SNAPSHOT"]
+ ;; [io.pedestal/pedestal.tomcat "0.1.9-SNAPSHOT"]
+
+ ;; auto-reload changes
+ [ns-tracker "0.2.1"]
+
+ ;; Logging
+ [ch.qos.logback/logback-classic "1.0.7" :exclusions [org.slf4j/slf4j-api]]
+ [org.slf4j/jul-to-slf4j "1.7.2"]
+ [org.slf4j/jcl-over-slf4j "1.7.2"]
+ [org.slf4j/log4j-over-slf4j "1.7.2"]]
+ :profiles {:dev {:source-paths ["dev"]}}
+ :min-lein-version "2.0.0"
+ :resource-paths ["config", "resources"]
+ :main ^{:skip-aot true} auto-reload-server.server)
View
31 samples/auto-reload-server/src/auto_reload_server/server.clj
@@ -0,0 +1,31 @@
+(ns auto-reload-server.server
+ (:gen-class) ; for -main method in uberjar
+ (:require [auto-reload-server.service :as service]
+ [io.pedestal.service.http :as bootstrap]))
+
+(defonce service-instance nil)
+
+(defn create-server
+ "Standalone dev/prod mode."
+ [& [opts]]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-server (merge service/service opts)))))
+
+(defn -main [& args]
+ (create-server)
+ (bootstrap/start service-instance))
+
+
+;; Container prod mode for use with the io.pedestal.servlet.ClojureVarServlet class.
+
+(defn servlet-init [this config]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-servlet service/service)))
+ (.init (::bootstrap/servlet service-instance) config))
+
+(defn servlet-destroy [this]
+ (alter-var-root #'service-instance nil))
+
+(defn servlet-service [this servlet-req servlet-resp]
+ (.service ^javax.servlet.Servlet (::bootstrap/servlet service-instance)
+ servlet-req servlet-resp))
View
49 samples/auto-reload-server/src/auto_reload_server/service.clj
@@ -0,0 +1,49 @@
+(ns auto-reload-server.service
+ (:require [io.pedestal.service.http :as bootstrap]
+ [io.pedestal.service.http.route :as route]
+ [io.pedestal.service.http.body-params :as body-params]
+ [io.pedestal.service.http.route.definition :refer [defroutes]]
+ [ring.util.response :as ring-resp]))
+
+(defn about-page
+ [request]
+ (ring-resp/response (format "Clojure %s" (clojure-version))))
+
+(defn home-page
+ [request]
+ (ring-resp/response "Hello World!"))
+
+(defroutes routes
+ [[["/" {:get home-page}
+ ;; Set default interceptors for /about and any other paths under /
+ ^:interceptors [(body-params/body-params) bootstrap/html-body]
+ ["/about" {:get about-page}]]]])
+
+;; You can use this fn or a per-request fn via io.pedestal.service.http.route/url-for
+(def url-for (route/url-for-routes routes))
+
+;; Consumed by auto-reload-server.server/create-server
+(def service {:env :prod
+ ;; You can bring your own non-default interceptors. Make
+ ;; sure you include routing and set it up right for
+ ;; dev-mode. If you do, many other keys for configuring
+ ;; default interceptors will be ignored.
+ ;; :bootstrap/interceptors []
+ ::bootstrap/routes routes
+
+ ;; Uncomment next line to enable CORS support, add
+ ;; string(s) specifying scheme, host and port for
+ ;; allowed source(s):
+ ;;
+ ;; "http://localhost:8080"
+ ;;
+ ;;::boostrap/allowed-origins ["scheme://host:port"]
+
+ ;; Root for resource interceptor that is available by default.
+ ::bootstrap/resource-path "/public"
+
+ ;; Either :jetty or :tomcat (see comments in project.clj
+ ;; to enable Tomcat)
+ ;;::bootstrap/host "localhost"
+ ::bootstrap/type :jetty
+ ::bootstrap/port 8080})
View
24 samples/auto-reload-server/test/auto_reload_server/service_test.clj
@@ -0,0 +1,24 @@
+(ns auto-reload-server.service-test
+ (:require [clojure.test :refer :all]
+ [io.pedestal.service.test :refer :all]
+ [io.pedestal.service.http :as bootstrap]
+ [auto-reload-server.service :as service]))
+
+(def service
+ (::bootstrap/service-fn (bootstrap/create-servlet service/service)))
+
+(deftest home-page-test
+ (is (=
+ (:body (response-for service :get "/"))
+ "Hello World!"))
+ (is (=
+ (:headers (response-for service :get "/"))
+ {"Content-Type" "text/html;charset=UTF-8"})))
+
+(deftest about-page-test
+ (is (.contains
+ (:body (response-for service :get "/about"))
+ "Clojure 1.5"))
+ (is (=
+ (:headers (response-for service :get "/about"))
+ {"Content-Type" "text/html;charset=UTF-8"})))
View
2 samples/cors/.gitignore
@@ -0,0 +1,2 @@
+target/
+tmp/
View
48 samples/cors/README.md
@@ -0,0 +1,48 @@
+# cors demo
+
+Here we demonstrate an implementation of [cross-origin resource sharing](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing).
+
+In order to fully demonstrate this feature, we must start two running instances of this application on ports 8080 and 8081. A static
+page with embedded javascript is served from http://localhost:8080/js, and it consumes an SSE EventSource found at http://localhost:8081/.
+
+1. Start two instances on different ports: `lein run 8080` and `lein run 8081`
+1. Open the Javascript console, as all output from the sample will be
+ displayed there.
+1. Visit [localhost:8080/js](http://localhost:8080/js) to load the
+ event consumer, and watch the JavaScript console. The inline JavaScript returned
+ will attempt to access a service on port 8081, a different origin. If allowed,
+ the event source passes back an event containing the thread id, which is consumed
+ and displayed in the console.
+
+In `src/cors/service.clj`, you will find a definition of `cors-interceptor` that adds CORS headers
+when the origin matches the authorized origin.
+
+For more detailed information, please consult the `NOTES.md` document alongside this README.
+
+## Thanks
+
+This samples uses the `EventSource` polyfill from [Yaffle/EventSource](https://github.com/Yaffle/EventSource).
+
+## Configuration
+
+To configure logging see config/logback.xml. By default, the app logs to stdout and logs/.
+To learn more about configuring Logback, read its [documentation](http://logback.qos.ch/documentation.html).
+
+## Links
+
+* [Other examples](https://github.com/pedestal/samples)
+
+
+License
+-------
+Copyright 2013 Relevance, Inc.
+Copyright 2014 Cognitect, Inc.
+
+The use and distribution terms for this software are covered by the
+Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+which can be found in the file epl-v10.html at the root of this distribution.
+
+By using this software in any fashion, you are agreeing to be bound by
+the terms of this license.
+
+You must not remove this notice, or any other, from this software.
View
52 samples/cors/config/logback.xml
@@ -0,0 +1,52 @@
+<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
+<configuration scan="true" scanPeriod="10 seconds">
+
+ <!-- Simple file output -->
+ <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+
+ <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <!-- rollover daily -->
+ <fileNamePattern>logs/cors-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+ <timeBasedFileNamingAndTriggeringPolicy
+ class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+ <!-- or whenever the file size reaches 64 MB -->
+ <maxFileSize>64 MB</maxFileSize>
+ </timeBasedFileNamingAndTriggeringPolicy>
+ </rollingPolicy>
+
+ <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->
+ <prudent>true</prudent>
+ </appender>
+
+
+ <!-- Console output -->
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ <!-- Only log level INFO and above -->
+ <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+ <level>WARN</level>
+ </filter>
+ </appender>
+
+
+ <!-- Enable FILE and STDOUT appenders for all log messages.
+ By default, only log at level INFO and above. -->
+ <root level="INFO">
+ <appender-ref ref="FILE" />
+ <appender-ref ref="STDOUT" />
+ </root>
+
+ <!-- For loggers in the these namespaces, log at all levels. -->
+ <logger name="user" level="ALL" />
+ <!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG
+ <logger name="io.pedestal" level="ALL" />
+ -->
+
+</configuration>
View
40 samples/cors/dev/dev.clj
@@ -0,0 +1,40 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+;; dev mode in repl (can get prod mode by passing prod options to dev-init
+(ns dev
+ (:require [io.pedestal.service.http :as bootstrap]
+ [cors.service :as service]
+ [cors.server :as server]))
+
+(def service (-> service/service
+ (merge {:env :dev
+ ::bootstrap/join? false
+ ::bootstrap/routes #(deref #'service/routes)
+ ::bootstrap/allowed-origins (constantly true)})
+ (bootstrap/default-interceptors)
+ (bootstrap/dev-interceptors)))
+
+(defn start
+ [& [opts]]
+ (server/create-server (merge service opts))
+ (bootstrap/start server/service-instance))
+
+(defn stop
+ []
+ (bootstrap/stop server/service-instance))
+
+(defn restart
+ []
+ (stop)
+ (start))
+
View
17 samples/cors/dev/user.clj
@@ -0,0 +1,17 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(defn dev
+ []
+ (require 'dev)
+ (in-ns 'dev)
+ #_(dev/start))
View
28 samples/cors/project.clj
@@ -0,0 +1,28 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(defproject cors "0.1.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.5.1"]
+ [io.pedestal/pedestal.service "0.1.3"]
+ [io.pedestal/pedestal.jetty "0.1.3"]
+ [ch.qos.logback/logback-classic "1.0.7"]
+ [org.slf4j/jul-to-slf4j "1.7.2"]
+ [org.slf4j/jcl-over-slf4j "1.7.2"]
+ [org.slf4j/log4j-over-slf4j "1.7.2"]
+ [ring-cors/ring-cors "0.1.0"]]
+ :profiles {:dev {:source-paths ["dev"]}}
+ :resource-paths ["config"]
+ :main ^{:skip-aot true} cors.server)
View
13 samples/cors/src/blob.html
@@ -0,0 +1,13 @@
+
+<script type="text/javascript" src="https://raw.github.com/Yaffle/EventSource/master/eventsource.js" ></script>
+
+<script type="text/javascript">
+console.log("Starting event listener for http://localhost:8081/");
+
+var source = new EventSource('http://localhost:8081/');
+
+source.addEventListener('thread-id', function(e) {
+ console.log(e.data);
+}, false);
+
+</script>
View
49 samples/cors/src/cors/server.clj
@@ -0,0 +1,49 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns cors.server
+ (:require [cors.service :as service]
+ [io.pedestal.service.http :as bootstrap]))
+
+(def service-instance
+ "Global var to hold service instance."
+ nil)
+
+(defn create-server
+ "Standalone dev/prod mode."
+ [& [opts]]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-server (merge service/service opts)))))
+
+(defn -main [& args]
+ (let [port (Long/valueOf (first args))]
+ (println "Creating server...")
+ (create-server [::bootstrap/port port])
+ (println (str "Server created. Awaiting connections on port " port))
+ (bootstrap/start service-instance)))
+
+
+;; Container prod mode for use with the pedestal.servlet.ClojureVarServlet class.
+
+(defn servlet-init [this config]
+ (require 'cors.service)
+ (alter-var-root #'service-instance (bootstrap/create-servlet service/service))
+ (bootstrap/start service-instance)
+ (.init (::bootstrap/servlet service-instance) config))
+
+(defn servlet-destroy [this]
+ (bootstrap/stop service-instance)
+ (alter-var-root #'service-instance nil))
+
+(defn servlet-service [this servlet-req servlet-resp]
+ (.service ^javax.servlet.Servlet (::bootstrap/servlet service-instance)
+ servlet-req servlet-resp))
View
71 samples/cors/src/cors/service.clj
@@ -0,0 +1,71 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns cors.service
+ (:require [clojure.java.io :as io]
+ [io.pedestal.service.interceptor :refer [defhandler defbefore defafter definterceptor]]
+ [io.pedestal.service.log :as log]
+ [io.pedestal.service.http :as bootstrap]
+ [io.pedestal.service.http.route :as route]
+ [io.pedestal.service.http.body-params :as body-params]
+ [io.pedestal.service.http.route.definition :refer [defroutes]]
+ [io.pedestal.service.http.sse :refer [sse-setup send-event end-event-stream]]
+ [io.pedestal.service.http.impl.servlet-interceptor :as servlet-interceptor]
+ [ring.util.response :as ring-response]
+ [ring.middleware.cors :as cors]))
+
+(defn send-thread-id [context]
+ (send-event context "thread-id" (str (.getId (Thread/currentThread)))))
+
+(defn thread-id-sender [{{^ServletResponse response :servlet-response
+ :as request} :request :as context}]
+
+ (log/info :msg "starting sending thread id")
+ (dotimes [_ 10]
+ (Thread/sleep 3000)
+ (send-thread-id context))
+ (log/info :msg "stopping sending thread id")
+
+ (end-event-stream context))
+
+(defhandler send-js
+ "Send the client a response containing the stub JS which consumes an
+ event source."
+ [req]
+ (log/info :msg "returning js")
+ (-> (ring-response/response (slurp (io/resource "blob.html")))
+ (ring-response/content-type "text/html")))
+
+(definterceptor thread-id-sender (sse-setup thread-id-sender))
+
+(defroutes routes
+ [[["/js" {:get send-js}]
+ ["/" {:get thread-id-sender}]]])
+
+
+;; You can use this fn or a per-request fn via io.pedestal.service.http.route/url-for
+(def url-for (route/url-for-routes routes))
+
+;; Consumed by cors.server/create-server
+(def service {:env :prod
+ ;; You can bring your own non-default interceptors. Make
+ ;; sure you include routing and set it up right for
+ ;; dev-mode. If you do, many other keys for configuring
+ ;; default interceptors will be ignored.
+ ;; :bootstrap/interceptors []
+ ::bootstrap/routes routes
+ ::bootstrap/allowed-origins ["http://localhost:8080"]
+ ;; Root for resource interceptor that is available by default.
+ ::bootstrap/resource-path "/public"
+ ;; Choose from [:jetty :tomcat].
+ ::bootstrap/type :jetty
+ ::bootstrap/port 8081})
View
21 samples/cors/test/cors/service_test.clj
@@ -0,0 +1,21 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns cors.service-test
+ (:require [clojure.test :refer :all]
+ [io.pedestal.service.test :refer :all]
+ [io.pedestal.service.http :as bootstrap]
+ [cors.service :as service]))
+
+(def service
+ (::bootstrap/service-fn (bootstrap/create-servlet service/service)))
+
View
3 samples/jboss/.gitignore
@@ -0,0 +1,3 @@
+target/
+tmp/
+logs/
View
29 samples/jboss/README.md
@@ -0,0 +1,29 @@
+# jboss
+
+This sample demonstrates how to deploy a service in jboss using
+immutant. The sample can run on jetty as well. Routing and URL
+generation work correctly in both cases.
+
+## Getting Started
+
+1. Start the application: `lein run`
+2. Go to [localhost:8080](http://localhost:8080) to see how the route
+is resolved.
+3. Go to [localhost:8080/about](http://localhost:8080/about) to see
+how the route is resolved.
+4. Stop the server.
+5. Install the immutant container: 'lein immutant install'
+6. Run the immutant container: 'lein immutant run'
+7. In a separate shell, deploy the sample to the immutant container: 'lein immutant deploy'
+8. Watch the immutant log to see when the sample is completely
+deployed.
+9. Go to [localhost:8080](http://localhost:8080/foo) to see how the route
+is resolved.
+10. Go to [localhost:8080/about](http://localhost:8080/foo/about) to see
+how the route is resolved.
+11. Read the service's source code at src/jboss/service.clj. Read
+immutant initialization code at src/immutant/init.clj.
+12. Learn more! See the [Links section below](#links).
+
+## Links
+* [Other examples](https://github.com/pedestal/samples)
View
52 samples/jboss/config/logback.xml
@@ -0,0 +1,52 @@
+<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
+<configuration scan="true" scanPeriod="10 seconds">
+
+ <!-- Simple file output -->
+ <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+
+ <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <!-- rollover daily -->
+ <fileNamePattern>logs/jboss-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+ <timeBasedFileNamingAndTriggeringPolicy
+ class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+ <!-- or whenever the file size reaches 64 MB -->
+ <maxFileSize>64 MB</maxFileSize>
+ </timeBasedFileNamingAndTriggeringPolicy>
+ </rollingPolicy>
+
+ <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->
+ <prudent>true</prudent>
+ </appender>
+
+
+ <!-- Console output -->
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ <!-- Only log level INFO and above -->
+ <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+ <level>WARN</level>
+ </filter>
+ </appender>
+
+
+ <!-- Enable FILE and STDOUT appenders for all log messages.
+ By default, only log at level INFO and above. -->
+ <root level="INFO">
+ <appender-ref ref="FILE" />
+ <appender-ref ref="STDOUT" />
+ </root>
+
+ <!-- For loggers in the these namespaces, log at all levels. -->
+ <logger name="user" level="ALL" />
+ <!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG
+ <logger name="pedestal" level="ALL" />
+ -->
+
+</configuration>
View
30 samples/jboss/dev/dev.clj
@@ -0,0 +1,30 @@
+(ns dev
+ (:require [io.pedestal.service.http :as bootstrap]
+ [jboss.service :as service]
+ [jboss.server :as server]))
+
+(def service (-> service/service ;; start with production configuration
+ (merge {:env :dev
+ ;; do not block thread that starts web server
+ ::bootstrap/join? false
+ ;; reload routes on every request
+ ::bootstrap/routes #(deref #'service/routes)
+ ;; all origins are allowed in dev mode
+ ::bootstrap/allowed-origins (constantly true)})
+ (bootstrap/default-interceptors)
+ (bootstrap/dev-interceptors)))
+
+(defn start
+ [& [opts]]
+ (server/create-server (merge service opts))
+ (bootstrap/start server/service-instance))
+
+(defn stop
+ []
+ (bootstrap/stop server/service-instance))
+
+(defn restart
+ []
+ (stop)
+ (start))
+
View
5 samples/jboss/dev/user.clj
@@ -0,0 +1,5 @@
+(defn dev
+ []
+ (require 'dev)
+ (in-ns 'dev)
+ #_(dev/start))
View
27 samples/jboss/project.clj
@@ -0,0 +1,27 @@
+(defproject jboss "0.0.1-SNAPSHOT"
+ :description "FIXME: write description"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.5.0"]
+ [io.pedestal/pedestal.service "0.1.8"]
+ [org.immutant/immutant "0.9.0"]
+
+ ;; Remove this line and uncomment the next line to
+ ;; use Tomcat instead of Jetty:
+ [io.pedestal/pedestal.jetty "0.1.8"]
+ ;; [io.pedestal/pedestal.tomcat "0.1.8"]
+
+ ;; Logging
+ [ch.qos.logback/logback-classic "1.0.7"]
+ [org.slf4j/jul-to-slf4j "1.7.2"]
+ [org.slf4j/jcl-over-slf4j "1.7.2"]
+ [org.slf4j/log4j-over-slf4j "1.7.2"]]
+ :plugins [[lein-immutant "0.18.0"]]
+ :immutant {;:init immutant/init
+ :resolve-dependencies true
+ :context-path "/foo"}
+ :profiles {:dev {:source-paths ["dev"]}}
+ :min-lein-version "2.0.0"
+ :resource-paths ["config"]
+ :main ^{:skip-aot true} jboss.server)
View
24 samples/jboss/src/immutant/init.clj
@@ -0,0 +1,24 @@
+(ns immutant.init
+ (:require [immutant.web :as web]
+ [io.pedestal.service.http :as bootstrap]
+ [io.pedestal.service.log :as log]
+ [jboss.service :as service]
+ [jboss.server :as server]))
+
+(let [servlet-map (bootstrap/create-servlet service/service)]
+ (alter-var-root #'server/service-instance (constantly servlet-map))
+ (def servlet (::bootstrap/servlet servlet-map))
+)
+
+(web/start-servlet "/" servlet)
+
+
+
+
+
+
+
+
+
+
+
View
42 samples/jboss/src/jboss/server.clj
@@ -0,0 +1,42 @@
+(ns jboss.server
+ #_(:gen-class) ; for -main method in uberjar
+ (:require [jboss.service :as service]
+ [io.pedestal.service.http :as bootstrap]))
+
+(def service-instance
+ "Global var to hold service instance."
+ nil)
+
+(defn get-context
+ []
+ (let [servlet (::bootstrap/servlet service-instance)
+ context (.getServletContext servlet)
+ context-path (when context (.getContextPath context))]
+ context-path))
+
+(defn create-server
+ "Standalone dev/prod mode."
+ [& [opts]]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-server (merge service/service opts)))))
+
+(defn -main [& args]
+ (create-server)
+ (bootstrap/start service-instance))
+
+
+;; Container prod mode for use with the io.pedestal.servlet.ClojureVarServlet class.
+
+(defn servlet-init [this config]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-servlet service/service)))
+ (.init (::bootstrap/servlet service-instance) config)
+;; (service/init-url-for (get-context))
+ )
+
+(defn servlet-destroy [this]
+ (alter-var-root #'service-instance nil))
+
+(defn servlet-service [this servlet-req servlet-resp]
+ (.service ^javax.servlet.Servlet (::bootstrap/servlet service-instance)
+ servlet-req servlet-resp))
View
51 samples/jboss/src/jboss/service.clj
@@ -0,0 +1,51 @@
+(ns jboss.service
+ (:require [io.pedestal.service.http :as bootstrap]
+ [io.pedestal.service.http.route :as route]
+ [io.pedestal.service.http.body-params :as body-params]
+ [io.pedestal.service.http.route.definition :refer [defroutes]]
+ [ring.util.response :as ring-resp]))
+
+(defn about-page
+ [request]
+ (ring-resp/response (-> request
+ (select-keys [:context-path :servlet-path :path-info :uri])
+ (merge {::route/url-for (route/url-for ::about-page)}))))
+
+(defn home-page
+ [request]
+ (ring-resp/response (-> request
+ (select-keys [:context-path :servlet-path :path-info :uri])
+ (merge {::route/url-for (route/url-for ::home-page)}))))
+
+(defroutes routes
+ [[["/" {:get home-page}
+ ;; Set default interceptors for /about and any other paths under /
+ ^:interceptors [(body-params/body-params)]
+ ["/about" {:get about-page}]]]])
+
+;; Consumed by jboss.server/create-server
+(def service {:env :prod
+ ;; You can bring your own non-default interceptors. Make
+ ;; sure you include routing and set it up right for
+ ;; dev-mode. If you do, many other keys for configuring
+ ;; default interceptors will be ignored.
+ ;; :bootstrap/interceptors []
+ ::bootstrap/routes routes
+
+ ;; Uncomment next line to enable CORS support, add
+ ;; string(s) specifying scheme, host and port for
+ ;; allowed source(s):
+ ;;
+ ;; "http://localhost:8080"
+ ;;
+ ;;::boostrap/allowed-origins ["scheme://host:port"]
+
+ ;; Root for resource interceptor that is available by default.
+ ::bootstrap/resource-path "/public"
+
+ ;; Either :jetty or :tomcat (see comments in project.clj
+ ;; to enable Tomcat)
+ ;;::bootstrap/host "localhost"
+ ::bootstrap/type :jetty
+ ::bootstrap/port 8080})
+
View
18 samples/jboss/test/jboss/service_test.clj
@@ -0,0 +1,18 @@
+(ns jboss.service-test
+ (:require [clojure.test :refer :all]
+ [io.pedestal.service.test :refer :all]
+ [io.pedestal.service.http :as bootstrap]
+ [jboss.service :as service]))
+
+(def service
+ (::bootstrap/service-fn (bootstrap/create-servlet service/service)))
+
+(deftest home-page-test
+ (is (=
+ (:body (response-for service :get "/"))
+ "Hello World!")))
+
+(deftest about-page-test
+ (is (.contains
+ (:body (response-for service :get "/about"))
+ "Clojure 1.5")))
View
2 samples/ring-middleware/.gitignore
@@ -0,0 +1,2 @@
+target/
+tmp/
View
53 samples/ring-middleware/README.md
@@ -0,0 +1,53 @@
+# Ring Middleware Sample
+
+Pedestal incorporates existing ring middleware into its
+interceptors functionality. In this example, we implement a service
+that stores your name in the session via setting a session token
+in the cookie store. We are depending on `ring.middleware.session.cookie`
+for this functionality.
+
+To make this work, we use `definterceptor` to define the interceptor
+we'll need in the processing list to store a value in a cookie. From
+`services.clj`:
+
+```clojure
+(definterceptor session-interceptor
+ (middlewares/session {:store (cookie/cookie-store)}))
+```
+
+For further enlightenment, take a look at:
+
+* The pedestal ring-middlewares package: `pedestal.service.http.ring-middlewares`
+* The pedestal interceptors functionality: `pedestal.service.interceptor`
+
+## See it in Action
+
+1. Start the application: `lein run`
+2. Go to [localhost:8080](http://localhost:8080/) and enter your
+ name in the field. You will be directed to the `/hello` page, and
+ your name displayed. So long as the service is not killed, you
+ will be remembered.
+3. Learn more! See the [Links section below](#links).
+
+## Configuration
+
+To configure logging see config/logback.xml. By default, the app logs to stdout and logs/.
+To learn more about configuring Logback, read its [documentation](http://logback.qos.ch/documentation.html).
+
+## Links
+* [Other examples](https://github.com/pedestal/samples)
+
+
+License
+-------
+Copyright 2013 Relevance, Inc.
+Copyright 2014 Cognitect, Inc.
+
+The use and distribution terms for this software are covered by the
+Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+which can be found in the file epl-v10.html at the root of this distribution.
+
+By using this software in any fashion, you are agreeing to be bound by
+the terms of this license.
+
+You must not remove this notice, or any other, from this software.
View
52 samples/ring-middleware/config/logback.xml
@@ -0,0 +1,52 @@
+<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
+<configuration scan="true" scanPeriod="10 seconds">
+
+ <!-- Simple file output -->
+ <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+
+ <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <!-- rollover daily -->
+ <fileNamePattern>logs/ring-middleware-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+ <timeBasedFileNamingAndTriggeringPolicy
+ class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+ <!-- or whenever the file size reaches 64 MB -->
+ <maxFileSize>64 MB</maxFileSize>
+ </timeBasedFileNamingAndTriggeringPolicy>
+ </rollingPolicy>
+
+ <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->
+ <prudent>true</prudent>
+ </appender>
+
+
+ <!-- Console output -->
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ <!-- Only log level INFO and above -->
+ <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+ <level>WARN</level>
+ </filter>
+ </appender>
+
+
+ <!-- Enable FILE and STDOUT appenders for all log messages.
+ By default, only log at level INFO and above. -->
+ <root level="INFO">
+ <appender-ref ref="FILE" />
+ <appender-ref ref="STDOUT" />
+ </root>
+
+ <!-- For loggers in the these namespaces, log at all levels. -->
+ <logger name="user" level="ALL" />
+ <!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG
+ <logger name="pedestal" level="ALL" />
+ -->
+
+</configuration>
View
39 samples/ring-middleware/dev/dev.clj
@@ -0,0 +1,39 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+;; dev mode in repl (can get prod mode by passing prod options to dev-init
+(ns dev
+ (:require [ring-middleware.service :as service]
+ [ring-middleware.server :as server]
+ [io.pedestal.service.http :as bootstrap]))
+
+(def service (-> service/service
+ (merge {:env :dev
+ ::bootstrap/join? false
+ ::bootstrap/routes #(deref #'service/routes)})
+ (bootstrap/default-interceptors)
+ (bootstrap/dev-interceptors)))
+
+(defn start
+ [& [opts]]
+ (server/create-server (merge service opts))
+ (bootstrap/start server/service-instance))
+
+(defn stop
+ []
+ (bootstrap/stop server/service-instance))
+
+(defn restart
+ []
+ (stop)
+ (start))
+
View
27 samples/ring-middleware/project.clj
@@ -0,0 +1,27 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(defproject ring-middleware "0.1.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.5.1"]
+ [io.pedestal/pedestal.service "0.1.2"]
+ [io.pedestal/pedestal.jetty "0.1.2"]
+ [ch.qos.logback/logback-classic "1.0.7"]
+ [org.slf4j/jul-to-slf4j "1.7.2"]
+ [org.slf4j/jcl-over-slf4j "1.7.2"]
+ [org.slf4j/log4j-over-slf4j "1.7.2"]]
+ :profiles {:dev {:source-paths ["dev"]}}
+ :resource-paths ["config"]
+ :main ^{:skip-aot true} ring-middleware.server)
View
16 samples/ring-middleware/src/hello-form.html
@@ -0,0 +1,16 @@
+<html>
+ <head>
+ <title>Session applying Ring Middleware sample</title>
+ </head>
+ <body>
+ <h1>Enter your name and I will remember you.</h1>
+ <form action="/introduce" method="POST">
+ <label for="name">Name</label>
+ <input type="text" name="name"/>
+ <input type="submit" name="submit" />
+ </form>
+ <p>
+ Alternatively, visit the <a href="/hello">greeting page</a> to see who I think you are.
+ </p>
+ </body>
+</html>
View
48 samples/ring-middleware/src/ring_middleware/server.clj
@@ -0,0 +1,48 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns ring-middleware.server
+ (:require [ring-middleware.service :as service]
+ [io.pedestal.service.http :as bootstrap]))
+
+(def service-instance
+ "Global var to hold service instance."
+ nil)
+
+(defn create-server
+ "Standalone dev/prod mode."
+ [& [opts]]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-server (merge service/service opts)))))
+
+(defn -main [& args]
+ (println "Creating server...")
+ (create-server)
+ (println "Server created. Awaiting connections.")
+ (bootstrap/start service-instance))
+
+
+;; Container prod mode for use with the pedestal.servlet.ClojureVarServlet class.
+
+(defn servlet-init [this config]
+ (require 'ring-middleware.service)
+ (alter-var-root #'service-instance (bootstrap/create-servlet service/service))
+ (bootstrap/start service-instance)
+ (.init (::bootstrap/servlet service-instance) config))
+
+(defn servlet-destroy [this]
+ (bootstrap/stop service-instance)
+ (alter-var-root #'service-instance nil))
+
+(defn servlet-service [this servlet-req servlet-resp]
+ (.service ^javax.servlet.Servlet (::bootstrap/servlet service-instance)
+ servlet-req servlet-resp))
View
106 samples/ring-middleware/src/ring_middleware/service.clj
@@ -0,0 +1,106 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns ring-middleware.service
+ (:require [clojure.java.io :as io]
+ [io.pedestal.service.http :as bootstrap]
+ [io.pedestal.service.http.route :as route]
+ [io.pedestal.service.http.body-params :as body-params]
+ [io.pedestal.service.http.route.definition :refer [defroutes]]
+ [io.pedestal.service.http.ring-middlewares :as middlewares]
+ [io.pedestal.service.interceptor :refer [defhandler definterceptor]]
+ [ring.middleware.session.cookie :as cookie]
+ [ring.util.response :as ring-resp]))
+
+
+(defn html-response
+ [html]
+ (ring-resp/content-type (ring-resp/response html) "text/html"))
+
+;; Gather some data from the user to retain in their session.
+(defhandler intro-form
+ "Prompt a user for their name, then remember it."
+ [req]
+ (html-response
+ (slurp (io/resource "hello-form.html"))))
+
+(defhandler introduction
+ "Place the name provided by the user into their session, then send
+ them to hello."
+ [req]
+ (let [name (-> req
+ :params
+ :name)]
+ (-> (ring-resp/redirect "/hello")
+ (assoc :session {:name name}))))
+
+;; Behavior dictated by data in the user's session. Using ring
+;; middleware means that the request is what gets modified by
+;; interceptors.
+
+;; We default in 'Stranger' so users visiting the service can see the
+;; behavior of the service when no session data is present.
+(defhandler hello
+ "Look up the name for this http session, if present greet the user
+ by their name. If not, greet the user as stranger."
+ [req]
+ (let [name (or (-> req
+ :session
+ :name)
+ "Stranger")]
+ (html-response (str "<html><body><h1>Hello, " name "!</h1></body></html>"))))
+
+;; Two notes:
+
+;; 1: You can create a session interceptor without specifying a store,
+;; in which case the interceptor will store the session data nowhere
+;; and it will be about as useful as not having it in the first
+;; place. Storing session data requires specifying the session store.
+
+;; 2: In this example code we do not specify the secret with which the
+;; session data is encrypted prior to being sent back to the
+;; browser. This has two consequences, the first being that we need to
+;; use the same interceptor instance throughout the service so that the
+;; session data is readable and writable to all paths. The second
+;; consequence is that session data will become unrecoverable when the
+;; server process is ended. Even though the browser retains the
+;; cookie, it is not unrecoverable ciphertext and the session
+;; interceptor will treat it as non-existant.
+(definterceptor session-interceptor
+ (middlewares/session {:store (cookie/cookie-store)}))
+
+;; Set up routes to get all the above handlers accessible.
+(defroutes routes
+ [[["/" {:get intro-form}]
+ ["/introduce" ^:interceptors [middlewares/params
+ middlewares/keyword-params
+ session-interceptor]
+ {:post introduction}]
+ ["/hello" ^:interceptors [session-interceptor]
+ {:get hello}]]])
+
+;; You can use this fn or a per-request fn via io.pedestal.service.http.route/url-for
+(def url-for (route/url-for-routes routes))
+
+;; Consumed by ring-middleware.server/create-server
+(def service {:env :prod
+ ;; You can bring your own non-default interceptors. Make
+ ;; sure you include routing and set it up right for
+ ;; dev-mode. If you do, many other keys for configuring
+ ;; default interceptors will be ignored.
+ ;; :bootstrap/interceptors []
+ ::bootstrap/routes routes
+ ;; Root for resource interceptor that is available by default.
+ ::bootstrap/resource-path "/public"
+ ;; Choose from [:jetty :tomcat].
+ ::bootstrap/type :jetty
+ ::bootstrap/port 8080})
View
25 samples/ring-middleware/test/ring_middleware/service_test.clj
@@ -0,0 +1,25 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns ring-middleware.service-test
+ (:require [clojure.test :refer :all]
+ [io.pedestal.service.test :refer :all]
+ [io.pedestal.service.http :as bootstrap]
+ [ring-middleware.service :as service]))
+
+(def service
+ (::bootstrap/service-fn (bootstrap/create-servlet service/service)))
+
+(deftest home-page-renders-correctly
+ (is (.contains
+ (:body (response-for service :get "/"))
+ "Enter your name")))
View
3 samples/server-sent-events/.gitignore
@@ -0,0 +1,3 @@
+target/
+tmp/
+logs/
View
18 samples/server-sent-events/README.md
@@ -0,0 +1,18 @@
+# server-sent-events
+
+Demonstrates setting up, using and tearing down a server-sent-events
+event stream. Events are sent from multiple threads.
+
+## Getting Started
+
+1. Start the application: `lein run`
+2. Use `curl http://localhost:8080` to see event stream data.
+3. Read the source code at src/server_sent_events/service.clj.
+
+## Configuration
+
+To configure logging see config/logback.xml. By default, the app logs to stdout and logs/.
+To learn more about configuring Logback, read its [documentation](http://logback.qos.ch/documentation.html).
+
+## Links
+* [Other examples](https://github.com/pedestal/samples)
View
52 samples/server-sent-events/config/logback.xml
@@ -0,0 +1,52 @@
+<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
+<configuration scan="true" scanPeriod="10 seconds">
+
+ <!-- Simple file output -->
+ <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+
+ <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <!-- rollover daily -->
+ <fileNamePattern>logs/server-sent-events-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+ <timeBasedFileNamingAndTriggeringPolicy
+ class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+ <!-- or whenever the file size reaches 64 MB -->
+ <maxFileSize>64 MB</maxFileSize>
+ </timeBasedFileNamingAndTriggeringPolicy>
+ </rollingPolicy>
+
+ <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->
+ <prudent>true</prudent>
+ </appender>
+
+
+ <!-- Console output -->
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ <!-- Only log level INFO and above -->
+ <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+ <level>WARN</level>
+ </filter>
+ </appender>
+
+
+ <!-- Enable FILE and STDOUT appenders for all log messages.
+ By default, only log at level INFO and above. -->
+ <root level="INFO">
+ <appender-ref ref="FILE" />
+ <appender-ref ref="STDOUT" />
+ </root>
+
+ <!-- For loggers in the these namespaces, log at all levels. -->
+ <logger name="user" level="ALL" />
+ <!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG
+ <logger name="pedestal" level="ALL" />
+ -->
+
+</configuration>
View
39 samples/server-sent-events/dev/dev.clj
@@ -0,0 +1,39 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+;; dev mode in repl (can get prod mode by passing prod options to dev-init
+(ns dev
+ (:require [io.pedestal.service.http :as bootstrap]
+ [server-sent-events.service :as service]
+ [server-sent-events.server :as server]))
+
+(def service (-> service/service
+ (merge {:env :dev
+ ::bootstrap/join? false
+ ::bootstrap/routes #(deref #'service/routes)})
+ (bootstrap/default-interceptors)
+ (bootstrap/dev-interceptors)))
+
+(defn start
+ [& [opts]]
+ (server/create-server (merge service opts))
+ (bootstrap/start server/service-instance))
+
+(defn stop
+ []
+ (bootstrap/stop server/service-instance))
+
+(defn restart
+ []
+ (stop)
+ (start))
+
View
5 samples/server-sent-events/dev/user.clj
@@ -0,0 +1,5 @@
+(defn dev
+ []
+ (require 'dev)
+ (in-ns 'dev)
+ #_(dev/start))
View
22 samples/server-sent-events/project.clj
@@ -0,0 +1,22 @@
+(defproject server-sent-events "0.0.1-SNAPSHOT"
+ :description "FIXME: write description"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.5.1"]
+ [io.pedestal/pedestal.service "0.1.2"]
+
+ ;; Remove this line and uncomment the next line to
+ ;; use Tomcat instead of Jetty:
+ [io.pedestal/pedestal.jetty "0.1.2"]
+ ;; [io.pedestal/pedestal.tomcat "0.1.2"]
+
+ ;; Logging
+ [ch.qos.logback/logback-classic "1.0.7"]
+ [org.slf4j/jul-to-slf4j "1.7.2"]
+ [org.slf4j/jcl-over-slf4j "1.7.2"]
+ [org.slf4j/log4j-over-slf4j "1.7.2"]]
+ :profiles {:dev {:source-paths ["dev"]}}
+ :min-lein-version "2.0.0"
+ :resource-paths ["config"]
+ :main ^{:skip-aot true} server-sent-events.server)
View
47 samples/server-sent-events/src/server_sent_events/server.clj
@@ -0,0 +1,47 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns server-sent-events.server
+ (:gen-class) ; for -main method in uberjar
+ (:require [server-sent-events.service :as service]
+ [io.pedestal.service.http :as bootstrap]))
+
+(def service-instance
+ "Global var to hold service instance."
+ nil)
+
+(defn create-server
+ "Standalone dev/prod mode."
+ [& [opts]]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-server (merge service/service opts)))))
+
+(defn -main [& args]
+ (println "Creating server...")
+ (create-server)
+ (println "Server created. Awaiting connections.")
+ (bootstrap/start service-instance))
+
+
+;; Container prod mode for use with the io.pedestal.servlet.ClojureVarServlet class.
+
+(defn servlet-init [this config]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-servlet service/service)))
+ (.init (::bootstrap/servlet service-instance) config))
+
+(defn servlet-destroy [this]
+ (alter-var-root #'service-instance nil))
+
+(defn servlet-service [this servlet-req servlet-resp]
+ (.service ^javax.servlet.Servlet (::bootstrap/servlet service-instance)
+ servlet-req servlet-resp))
View
44 samples/server-sent-events/src/server_sent_events/service.clj
@@ -0,0 +1,44 @@
+(ns server-sent-events.service
+ (:require [io.pedestal.service.http :as bootstrap]
+ [io.pedestal.service.http.sse :as sse]
+ [io.pedestal.service.http.route :as route]
+ [io.pedestal.service.http.route.definition :refer [defroutes]]
+ [ring.util.response :as ring-resp]))
+
+(defn send-counter
+ "Counts down to 0, sending value of counter to sse context and
+ recursing on a different thread; ends event stream when counter
+ is 0."
+ [ctx count]
+ (sse/send-event ctx "count" (str count ", thread: " (.getId (Thread/currentThread))))
+ (Thread/sleep 2000)
+ (if (> count 0)
+ (future (send-counter ctx (dec count)))
+ (sse/end-event-stream ctx)))
+
+(defn sse-stream-ready
+ "Starts sending counter events to client."
+ [ctx]
+ (send-counter ctx 10))
+
+;; Wire root URL to sse event stream
+(defroutes routes
+ [[["/" {:get [::send-counter (sse/sse-setup sse-stream-ready)]}]]])
+
+;; You can use this fn or a per-request fn via io.pedestal.service.http.route/url-for
+(def url-for (route/url-for-routes routes))
+
+;; Consumed by server-sent-events.server/create-server
+(def service {:env :prod
+ ;; You can bring your own non-default interceptors. Make
+ ;; sure you include routing and set it up right for
+ ;; dev-mode. If you do, many other keys for configuring
+ ;; default interceptors will be ignored.
+ ;; :bootstrap/interceptors []
+ ::bootstrap/routes routes
+ ;; Root for resource interceptor that is available by default.
+ ::bootstrap/resource-path "/public"
+ ;; Either :jetty or :tomcat (see comments in project.clj
+ ;; to enable Tomcat)
+ ::bootstrap/type :jetty
+ ::bootstrap/port 8080})
View
18 samples/server-sent-events/test/server_sent_events/service_test.clj
@@ -0,0 +1,18 @@
+(ns server-sent-events.service-test
+ (:require [clojure.test :refer :all]
+ [io.pedestal.service.test :refer :all]
+ [io.pedestal.service.http :as bootstrap]
+ [server-sent-events.service :as service]))
+
+(def service
+ (::bootstrap/service-fn (bootstrap/create-servlet service/service)))
+
+(deftest home-page-test
+ (is (=
+ (:body (response-for service :get "/"))
+ "Hello World!")))
+
+(deftest about-page-test
+ (is (.contains
+ (:body (response-for service :get "/about"))
+ "Clojure 1.5")))
View
2 samples/server-with-links/.gitignore
@@ -0,0 +1,2 @@
+target/
+tmp/
View
24 samples/server-with-links/README.md
@@ -0,0 +1,24 @@
+## Description
+
+This is a sample app that demonstrates using a linker fn that
+generates a link based on your routing table.
+
+## Usage
+
+1. Start the application: `lein run`
+2. Go to [localhost:8080](http://localhost:8080/) to see a body with a link.
+
+
+License
+-------
+Copyright 2013 Relevance, Inc.
+Copyright 2014 Cognitect, Inc.
+
+The use and distribution terms for this software are covered by the
+Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+which can be found in the file epl-v10.html at the root of this distribution.
+
+By using this software in any fashion, you are agreeing to be bound by
+the terms of this license.
+
+You must not remove this notice, or any other, from this software.
View
52 samples/server-with-links/config/logback.xml
@@ -0,0 +1,52 @@
+<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
+<configuration scan="true" scanPeriod="10 seconds">
+
+ <!-- Simple file output -->
+ <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+
+ <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <!-- rollover daily -->
+ <fileNamePattern>logs/server-with-links-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+ <timeBasedFileNamingAndTriggeringPolicy
+ class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+ <!-- or whenever the file size reaches 64 MB -->
+ <maxFileSize>64 MB</maxFileSize>
+ </timeBasedFileNamingAndTriggeringPolicy>
+ </rollingPolicy>
+
+ <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->
+ <prudent>true</prudent>
+ </appender>
+
+
+ <!-- Console output -->
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ <!-- Only log level INFO and above -->
+ <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+ <level>WARN</level>
+ </filter>
+ </appender>
+
+
+ <!-- Enable FILE and STDOUT appenders for all log messages.
+ By default, only log at level INFO and above. -->
+ <root level="INFO">
+ <appender-ref ref="FILE" />
+ <appender-ref ref="STDOUT" />
+ </root>
+
+ <!-- For loggers in the these namespaces, log at all levels. -->
+ <logger name="user" level="ALL" />
+ <!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG
+ <logger name="pedestal" level="ALL" />
+ -->
+
+</configuration>
View
39 samples/server-with-links/dev/dev.clj
@@ -0,0 +1,39 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+;; dev mode in repl (can get prod mode by passing prod options to dev-init
+(ns dev
+ (:require [server-with-links.service :as service]
+ [server-with-links.server :as server]
+ [io.pedestal.service.http :as bootstrap]))
+
+(def service (-> service/service
+ (merge {:env :dev
+ ::bootstrap/join? false
+ ::bootstrap/routes #(deref #'service/routes)})
+ (bootstrap/default-interceptors)
+ (bootstrap/dev-interceptors)))
+
+(defn start
+ [& [opts]]
+ (server/create-server (merge service opts))
+ (bootstrap/start server/service-instance))
+
+(defn stop
+ []
+ (bootstrap/stop server/service-instance))
+
+(defn restart
+ []
+ (stop)
+ (start))
+
View
27 samples/server-with-links/project.clj
@@ -0,0 +1,27 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(defproject server-with-links "0.1.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.5.1"]
+ [io.pedestal/pedestal.service "0.1.2"]
+ [io.pedestal/pedestal.jetty "0.1.2"]
+ [ch.qos.logback/logback-classic "1.0.7"]
+ [org.slf4j/jul-to-slf4j "1.7.2"]
+ [org.slf4j/jcl-over-slf4j "1.7.2"]
+ [org.slf4j/log4j-over-slf4j "1.7.2"]]
+ :profiles {:dev {:source-paths ["dev"]}}
+ :resource-paths ["config"]
+ :main ^{:skip-aot true} server-with-links.server)
View
48 samples/server-with-links/src/server_with_links/server.clj
@@ -0,0 +1,48 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns server-with-links.server
+ (:require [server-with-links.service :as service]
+ [io.pedestal.service.http :as bootstrap]))
+
+(def service-instance
+ "Global var to hold service instance."
+ nil)
+
+(defn create-server
+ "Standalone dev/prod mode."
+ [& [opts]]
+ (alter-var-root #'service-instance
+ (constantly (bootstrap/create-server (merge service/service opts)))))
+
+(defn -main [& args]
+ (println "Creating server...")
+ (create-server)
+ (println "Server created. Awaiting connections.")
+ (bootstrap/start service-instance))
+
+
+;; Container prod mode for use with the pedestal.servlet.ClojureVarServlet class.
+
+(defn servlet-init [this config]
+ (require 'server-with-links.service)
+ (alter-var-root #'service-instance (bootstrap/create-servlet service/service))
+ (bootstrap/start service-instance)
+ (.init (::bootstrap/servlet service-instance) config))
+
+(defn servlet-destroy [this]
+ (bootstrap/stop service-instance)
+ (alter-var-root #'service-instance nil))
+
+(defn servlet-service [this servlet-req servlet-resp]
+ (.service ^javax.servlet.Servlet (::bootstrap/servlet service-instance)
+ servlet-req servlet-resp))
View
59 samples/server-with-links/src/server_with_links/service.clj
@@ -0,0 +1,59 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns server-with-links.service
+ (:require [io.pedestal.service.http :as bootstrap]
+ [io.pedestal.service.http.route :as route]
+ [io.pedestal.service.http.body-params :as body-params]
+ [io.pedestal.service.interceptor :refer [defhandler]]
+ [io.pedestal.service.http.route.definition :refer [defroutes]]
+ [ring.util.response :as ring-resp]))
+
+(defn link-to
+ "Uses pedestal.service.http.route/url-for fn to generate a link to a named route"
+ [text route]
+ (format "<a href='%s'>%s</a>"
+ (io.pedestal.service.http.route/url-for route)
+ text))
+
+(defhandler this-page
+ [request]
+ (-> (ring-resp/response (format "<body>
+This isn't what you're looking for. Go to %s.
+</body>"
+ (link-to "that" :that)))
+ (ring-resp/content-type "text/html")))
+
+(defhandler that-page
+ [request]
+ (-> (ring-resp/response "That page")
+ (ring-resp/content-type "text/html")))
+
+(defroutes routes
+ [[; Unnamed route
+ ["/" {:get this-page}]
+ ; Name a route to be able to generate its path later
+ ["/that" {:get [:that that-page]}]]])
+
+;; Consumed by server-with-links.server/create-server
+(def service {:env :prod
+ ;; You can bring your own non-default interceptors. Make
+ ;; sure you include routing and set it up right for
+ ;; dev-mode. If you do, many other keys for configuring
+ ;; default interceptors will be ignored.
+ ;; :bootstrap/interceptors []
+ ::bootstrap/routes routes
+ ;; Root for resource interceptor that is available by default.
+ ::bootstrap/resource-path "/public"
+ ;; Choose from [:jetty :tomcat].
+ ::bootstrap/type :jetty
+ ::bootstrap/port 8080})
View
25 samples/server-with-links/test/server_with_links/service_test.clj
@@ -0,0 +1,25 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(ns server-with-links.service-test
+ (:require [clojure.test :refer :all]
+ [io.pedestal.service.test :refer :all]
+ [io.pedestal.service.http :as bootstrap]
+ [server-with-links.service :as service]))
+
+(def service
+ (::bootstrap/service-fn (bootstrap/create-servlet service/service)))
+
+(deftest link-generates-correct-link
+ (is (.contains
+ (:body (response-for service :get "/"))
+ "Go to <a href='/that'>that</a>")))
View
2 samples/template-server/.gitignore
@@ -0,0 +1,2 @@
+target/
+tmp/
View
39 samples/template-server/README.md
@@ -0,0 +1,39 @@
+# A Simple Templates Demo
+
+Included (in no particular order):
+
+* [hiccup](https://github.com/weavejester/hiccup)
+* [enlive](https://github.com/cgrand/enlive)
+* [mustache](http://http://mustache.github.com/)
+* [stringtemplate](http://www.stringtemplate.org/)
+* [comb](https://github.com/weavejester/comb)
+
+## Run It!
+
+1. Start the application: `lein run`
+2. Go to [localhost:8080](http://localhost:8080/) to see links to pages rendered by a number of template engines.
+3. Read the templating code in `src/template_server/service.clj`.
+4. Learn more about Pedestal! See the [Links section below](#links).
+
+## Configuration
+
+To configure logging see config/logback.xml. By default, the app logs to stdout and logs/.
+To learn more about configuring Logback, read its [documentation](http://logback.qos.ch/documentation.html).
+
+## Links
+* [Other examples](https://github.com/pedestal/samples)
+
+
+License
+-------
+Copyright 2013 Relevance, Inc.
+Copyright 2014 Cognitect, Inc.
+
+The use and distribution terms for this software are covered by the
+Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+which can be found in the file epl-v10.html at the root of this distribution.
+
+By using this software in any fashion, you are agreeing to be bound by
+the terms of this license.
+
+You must not remove this notice, or any other, from this software.
View
52 samples/template-server/config/logback.xml
@@ -0,0 +1,52 @@
+<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
+<configuration scan="true" scanPeriod="10 seconds">
+
+ <!-- Simple file output -->
+ <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+
+ <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <!-- rollover daily -->
+ <fileNamePattern>logs/template-server-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+ <timeBasedFileNamingAndTriggeringPolicy
+ class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+ <!-- or whenever the file size reaches 64 MB -->
+ <maxFileSize>64 MB</maxFileSize>
+ </timeBasedFileNamingAndTriggeringPolicy>
+ </rollingPolicy>
+
+ <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->
+ <prudent>true</prudent>
+ </appender>
+
+
+ <!-- Console output -->
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
+ <encoder>
+ <pattern>%-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ <!-- Only log level INFO and above -->
+ <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+ <level>WARN</level>
+ </filter>
+ </appender>
+
+
+ <!-- Enable FILE and STDOUT appenders for all log messages.
+ By default, only log at level INFO and above. -->
+ <root level="INFO">
+ <appender-ref ref="FILE" />
+ <appender-ref ref="STDOUT" />
+ </root>
+
+ <!-- For loggers in the these namespaces, log at all levels. -->
+ <logger name="user" level="ALL" />
+ <!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG
+ <logger name="pedestal" level="ALL" />
+ -->
+
+</configuration>
View
39 samples/template-server/dev/dev.clj
@@ -0,0 +1,39 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+;; dev mode in repl (can get prod mode by passing prod options to dev-init
+(ns dev
+ (:require [template-server.service :as service]
+ [template-server.server :as server]
+ [io.pedestal.service.http :as bootstrap]))
+
+(def service (-> service/service
+ (merge {:env :dev
+ ::bootstrap/join? false
+ ::bootstrap/routes #(deref #'service/routes)})
+ (bootstrap/default-interceptors)
+ (bootstrap/dev-interceptors)))
+
+(defn start
+ [& [opts]]
+ (server/create-server (merge service opts))
+ (bootstrap/start server/service-instance))
+
+(defn stop
+ []
+ (bootstrap/stop server/service-instance))
+
+(defn restart
+ []
+ (stop)
+ (start))
+
View
17 samples/template-server/dev/user.clj
@@ -0,0 +1,17 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(defn dev
+ []
+ (require 'dev)
+ (in-ns 'dev)
+ #_(dev/start))
View
32 samples/template-server/project.clj
@@ -0,0 +1,32 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+;
+; You must not remove this notice, or any other, from this software.
+
+(defproject template-server "0.1.0-SNAPSHOT"
+ :description "Demonstrates Pedestal page rendering via different template libraries."
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.5.1"]
+ [io.pedestal/pedestal.service "0.1.5"]
+ [io.pedestal/pedestal.jetty "0.1.5"]
+ [ch.qos.logback/logback-classic "1.0.7"]
+ [org.slf4j/jul-to-slf4j "1.7.2"]
+ [org.slf4j/jcl-over-slf4j "1.7.2"]
+ [org.slf4j/log4j-over-slf4j "1.7.2"]
+ [hiccup "1.0.2"]
+ [enlive "1.0.1"]
+ [comb "0.1.0"]
+ [org.antlr/stringtemplate "4.0.2"]
+ [de.ubercode.clostache/clostache "1.3.1"]]
+ :profiles {:dev {:source-paths ["dev"]}}
+ :resource-paths ["config" "resources"]
+ :main ^{:skip-aot true} template-server.server)
View
6 samples/template-server/resources/public/comb.html
@@ -0,0 +1,6 @@
+<html>
+ <body>
+ <h1> Hello from Comb!</h1>
+ <p>This is not <%= name %></p>
+ </body>
+</html>
View
12 samples/template-server/resources/public/enlive-template.html
@@ -0,0 +1,12 @@
+<html>
+ <body>
+ <h1 id="the-title">Hello from Enlive!</h1>
+ <hr/>
+ <p>This page was rendered with Enlive!<p>
+ <br/>
+ <p id="the-text">This is where the message goes</p>
+ <p id="the-date">The date goes here</p>
+ </body>
+</html>
+
+
View
10 samples/template-server/resources/public/mustache-template.html
@@ -0,0 +1,10 @@
+<html>
+ <body>
+ <h1 id="the-title">{{title}}</h1>
+ <hr/>
+ <p>This page was rendered with Mustache!<p>
+ <br/>
+ <p id="the-text">{{text}}</p>
+ <p id="the-date">{{date}}</p>
+ </body>
+</html>
View
48 samples/template-server/src/template_server/server.clj
@@ -0,0 +1,48 @@
+; Copyright 2013 Relevance, Inc.
+; Copyright 2014 Cognitect, Inc.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
+; which can be found in the file epl-v10.html at the root of this distribution.
+;