Browse Source

Initial Pedestal commit, json works manually

Zane Kansil 10 years ago
parent
commit
808ce8e098

+ 5 - 0
frameworks/Clojure/pedestal-api/.gitignore

@@ -0,0 +1,5 @@
+target/
+tmp/
+logs/
+.lein-repl-history
+.nrepl-port

+ 8 - 0
frameworks/Clojure/pedestal-api/README.md

@@ -0,0 +1,8 @@
+# Pedestal Benchmarking Test
+
+This is the Pedestal portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
+
+## Test URLs
+### JSON Encoding Test
+
+http://localhost:8080/

+ 52 - 0
frameworks/Clojure/pedestal-api/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/pedestal-api-%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="io.pedestal" level="ALL" />
+  -->
+
+</configuration>

+ 19 - 0
frameworks/Clojure/pedestal-api/project.clj

@@ -0,0 +1,19 @@
+(defproject pedestal-api "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.6.0"]
+                 [io.pedestal/pedestal.service "0.3.1"]
+
+                 [io.pedestal/pedestal.jetty "0.3.1"]
+                 [ch.qos.logback/logback-classic "1.1.2" :exclusions [org.slf4j/slf4j-api]]
+                 [org.slf4j/jul-to-slf4j "1.7.7"]
+                 [org.slf4j/jcl-over-slf4j "1.7.7"]
+                 [org.slf4j/log4j-over-slf4j "1.7.7"]
+                 [org.clojure/data.json "0.2.5"]]
+  :min-lein-version "2.0.0"
+  :resource-paths ["config", "resources"]
+  :profiles {:dev {:aliases {"run-dev" ["trampoline" "run" "-m" "pedestal-api.server/run-dev"]}
+                   :dependencies [[io.pedestal/pedestal.service-tools "0.3.1"]]}}
+  :main ^{:skip-aot true} pedestal-api.server)

+ 33 - 0
frameworks/Clojure/pedestal-api/src/pedestal_api/server.clj

@@ -0,0 +1,33 @@
+(ns pedestal-api.server
+  (:gen-class) ; for -main method in uberjar
+  (:require [io.pedestal.http :as server]
+            [pedestal-api.service :as service]))
+
+;; This is an adapted service map, that can be started and stopped
+;; From the REPL you can call server/start and server/stop on this service
+(defonce runnable-service (server/create-server service/service))
+
+(defn run-dev
+  "The entry-point for 'lein run-dev'"
+  [& args]
+  (println "\nCreating your [DEV] server...")
+  (-> service/service ;; start with production configuration
+      (merge {:env :dev
+              ;; do not block thread that starts web server
+              ::server/join? false
+              ;; Routes can be a function that resolve routes,
+              ;;  we can use this to set the routes to be reloadable
+              ::server/routes #(deref #'service/routes)
+              ;; all origins are allowed in dev mode
+              ::server/allowed-origins {:creds true :allowed-origins (constantly true)}})
+      ;; Wire up interceptor chains
+      server/default-interceptors
+      server/dev-interceptors
+      server/create-server
+      server/start))
+
+(defn -main
+  "The entry-point for 'lein run'"
+  [& args]
+  (println "\nCreating your server...")
+  (server/start runnable-service))

+ 27 - 0
frameworks/Clojure/pedestal-api/src/pedestal_api/service.clj

@@ -0,0 +1,27 @@
+(ns pedestal-api.service
+  (:require [io.pedestal.http :as bootstrap]
+            [io.pedestal.http.route :as route]
+            [io.pedestal.http.body-params :as body-params]
+            [io.pedestal.http.route.definition :refer [defroutes]]
+            [ring.util.response :as ring-resp]
+            [clojure.data.json :as json]))
+
+(defn about-page
+  [request]
+  (ring-resp/response (format "Clojure %s - served from %s"
+                              (clojure-version)
+                              (route/url-for ::about-page))))
+
+(defn json-test
+  [request]
+  (ring-resp/response (json/write-str {:hello "world"})))
+
+(defroutes routes
+  [[["/json" {:get json-test}]]])
+
+;; How the server will look, not the code to start it up
+(def service {:env :prod
+              ::bootstrap/routes routes
+              ::bootstrap/resource-path "/public"
+              ::bootstrap/type :jetty
+              ::bootstrap/port 8080})

+ 34 - 0
frameworks/Clojure/pedestal-api/test/pedestal_api/service_test.clj

@@ -0,0 +1,34 @@
+(ns pedestal-api.service-test
+  (:require [clojure.test :refer :all]
+            [io.pedestal.test :refer :all]
+            [io.pedestal.http :as bootstrap]
+            [pedestal-api.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"
+        "Strict-Transport-Security" "max-age=31536000; includeSubdomains"
+        "X-Frame-Options" "DENY"
+        "X-Content-Type-Options" "nosniff"
+        "X-XSS-Protection" "1; mode=block"})))
+
+
+(deftest about-page-test
+  (is (.contains
+       (:body (response-for service :get "/about"))
+       "Clojure 1.6"))
+  (is (=
+       (:headers (response-for service :get "/about"))
+       {"Content-Type" "text/html;charset=UTF-8"
+        "Strict-Transport-Security" "max-age=31536000; includeSubdomains"
+        "X-Frame-Options" "DENY"
+        "X-Content-Type-Options" "nosniff"
+        "X-XSS-Protection" "1; mode=block"})))
+