Permalink
Browse files

Virgin pedestal-service app [IMMUTANT-253]

  • Loading branch information...
jcrossley3 committed Mar 25, 2013
1 parent eb02bac commit 628c62d08b7a9f040f08c32100d3310e347c0dd3
@@ -0,0 +1,3 @@
+target/
+tmp/
+logs/
@@ -0,0 +1,20 @@
+# hello
+
+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/hello/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/hello/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)
@@ -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/hello-%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>
@@ -0,0 +1,38 @@
+; Copyright 2013 Relevance, 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]
+ [hello.service :as service]
+ [hello.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))
+
@@ -0,0 +1,5 @@
+(defn dev
+ []
+ (require 'dev)
+ (in-ns 'dev)
+ #_(dev/start))
@@ -0,0 +1,22 @@
+(defproject hello "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.1"]
+
+ ;; Remove this line and uncomment the next line to
+ ;; use Tomcat instead of Jetty:
+ [io.pedestal/pedestal.jetty "0.1.1"]
+ ;; [io.pedestal/pedestal.tomcat "0.1.1"]
+
+ ;; 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} hello.server)
@@ -0,0 +1,44 @@
+; Copyright 2013 Relevance, 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 hello.server
+ (:gen-class) ; for -main method in uberjar
+ (:require [hello.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]
+ (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))
@@ -0,0 +1,38 @@
+(ns hello.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)]
+ ["/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 hello.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})
@@ -0,0 +1,18 @@
+(ns hello.service-test
+ (:require [clojure.test :refer :all]
+ [io.pedestal.service.test :refer :all]
+ [io.pedestal.service.http :as bootstrap]
+ [hello.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")))

0 comments on commit 628c62d

Please sign in to comment.