Permalink
Browse files

working on initial setup

  • Loading branch information...
0 parents commit a7822df1489ef36deb226c7c6fc12f3cb3d1cfeb @zk committed Nov 28, 2010
@@ -0,0 +1,5 @@
+pom.xml
+*jar
+lib
+classes
+.DS_Store
17 README
@@ -0,0 +1,17 @@
+# nsfw
+
+FIXME: write description
+
+## Usage
+
+FIXME: write
+
+## Installation
+
+FIXME: write
+
+## License
+
+Copyright (C) 2010 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,4 @@
+#!/bin/sh
+NSFW_STANDALONE_LIB="/Users/zkim/napplelabs/nsfw/nsfw-1.0.0-SNAPSHOT-standalone.jar"
+
+echo $NSFW_STANDALONE_LIB
@@ -0,0 +1,13 @@
+(defproject nsfw "1.0.0-SNAPSHOT"
+ :description "No Such Framework -- Experimental"
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]
+ [ring "0.3.3"]
+ [net.cgrand/moustache "1.0.0-SNAPSHOT"]
+ [clj-stacktrace "0.2.0"]
+ [hiccup "0.3.0"]
+ [org.danlarkin/clojure-json "1.2-SNAPSHOT"]
+ [fleet "0.9.3"]]
+ :dev-dependencies [[swank-clojure "1.2.0"]])
+
+
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>NSFW 404</title>
+ </head>
+ <body>
+ <h1>NSFW 404</h1>
+ <p>There was a problem finding what you were looking for.</p>
+
+ <!--
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ FILLER for chrome 404 min size
+ -->
+ </body>
+</html>
@@ -0,0 +1,30 @@
+(ns <(str project-name)>.boot
+ (:use [ring.middleware file file-info params nested-params keyword-params session]
+ [ring.middleware.session.memory]
+ [clojure.stacktrace]
+ [clojure.pprint :only (pprint)]
+ [nsfw.util :only (web-stacktrace)]
+ [nsfw.middleware])
+ (:require [<(str project-name)>.routes]
+ [nsfw.server]))
+
+(def sessions (atom {}))
+
+(defn entry-handler [req]
+ (try
+ ((-> <(str project-name)>.routes/routes
+ (wrap-keyword-params)
+ (wrap-nested-params)
+ (wrap-params)
+ (wrap-always-session)
+ (wrap-session {:store (memory-store sessions)})
+ (wrap-file-info)
+ (wrap-file "resources/public")
+ (wrap-log-request)) req)
+ (catch Exception e
+ {:status 500
+ :headers {"Content-Type" "text/html"}
+ :body (web-stacktrace e req)})))
+
+(defn start-server [& [port]]
+ (nsfw.server/start (var entry-handler) (if port port 8080)))
No changes.
@@ -0,0 +1,7 @@
+(ns <(str project-name)>.routes
+ (:use [net.cgrand.moustache :only (app)]
+ [hiccup core]
+ [nsfw render]))
+
+(def routes
+ (app [""] (fn [r] (render :text "hello from nsfw."))))
@@ -0,0 +1 @@
+<!DOCTYPE html><html><head><title>404 - NSFW</title></head><body><h1>404</h1><p>Sorry, NSFW couldn't find the page you're looking for</p> </body></html>
@@ -0,0 +1 @@
+(ns nsfw.core)
@@ -0,0 +1,75 @@
+(ns nsfw.gen
+ (:require [clojure.string :as string]
+ [fleet])
+ (:import [java.io File]))
+
+(defn project-root [] "/Users/zkim/napplelabs/test-nsfw/my-proj/")
+
+(defn file-exists [f-str]
+ (.exists (File. f-str)))
+
+(defn read-project-name []
+ (let [f-str (str (project-root) "/project.clj")]
+ (if (file-exists f-str)
+ (let [rdr (clojure.lang.LineNumberingPushbackReader. (java.io.FileReader. f-str))
+ pdef (read rdr)]
+ (str (second pdef)))
+ {:success false})))
+
+(defn project-name-underscore []
+ (string/replace (read-project-name) #"-" "_"))
+
+(defn mkdir [path-coll]
+ (let [f (File. (apply str (project-root) (interpose File/separator path-coll)))]
+ (if (and (.exists f))
+ {:success false :status "exists" :path (.getPath f)}
+ (do
+ (.mkdirs f)
+ {:success true :status "created" :path (.getPath f)}))))
+
+(defn resource-to-str [resource]
+ (let [sb (StringBuilder.)]
+ (with-open [stream (.getResourceAsStream (.getContextClassLoader (Thread/currentThread)) resource)]
+ (loop [c (.read stream)]
+ (if (neg? c)
+ (str sb)
+ (do
+ (.append sb (char c))
+ (recur (.read stream))))))))
+
+(defn report-path-gen [status path]
+ (println " " status " | " path))
+
+(defn copy-file [template target-path force]
+ (let [target-path (str (project-root) target-path)
+ project-name (read-project-name)]
+ (if (and (file-exists target-path) (not force))
+ (report-path-gen "exists" target-path)
+ (do
+ (spit target-path ((fleet/fleet [project-name] template) project-name))
+ (report-path-gen "created" target-path)))))
+
+(defn proj-src-path [s]
+ (str "src/" (project-name-underscore) "/" s))
+
+(defn project [& [force]]
+ (let [dirs [["resources"]
+ ["resources" "public"]
+ ["resources" "public" "css"]
+ ["resources" "public" "js"]
+ ["resources" "public" "images"]
+ ["src" "main"]]]
+ (println "Initializing Project")
+ (doseq [d dirs]
+ (let [res (mkdir d)]
+ (println " " (:status res) " | " (:path res))))
+ (println)
+ (copy-file (resource-to-str "nsfw/bootstrap.tpl.clj") (proj-src-path "boot.clj") force)
+ (copy-file (resource-to-str "nsfw/routes.tpl.clj") (proj-src-path "routes.clj") force)
+ (copy-file (resource-to-str "nsfw/404.tpl.html") "resources/public/404.html" force)
+ (println)))
+
+(project true)
+
+
+
@@ -0,0 +1,18 @@
+(ns nsfw.middleware)
+
+(defn wrap-log-request [handler]
+ (fn [r]
+ (when (not= "/favicon.ico" (:uri r))
+ (let [start (System/nanoTime)
+ res (handler r)
+ diff (/ (- (System/nanoTime) start) 1000000.0)]
+ (println "[" (:uri r) "] - " (java.util.Date.))
+ #_(println (let [writer (java.io.StringWriter. )]
+ (pprint r writer)
+ (str writer)))
+ (println "^^^ took" diff "ms")
+ res))))
+
+(defn wrap-always-session [handler]
+ (fn [r]
+ (merge {:session (:session r)} (handler r))))
@@ -0,0 +1,32 @@
+(ns nsfw.render
+ (:require [org.danlarkin.json :as json]
+ [ring.util.response :as resp]))
+
+(defn render-dispatch [& args]
+ (if (keyword? (first args))
+ (first args)
+ :template))
+
+(defmulti render render-dispatch)
+
+(defmethod render :template [template & args]
+ {:status 200
+ :headers {"Content-Type" "text/html;charset=utf-8"}
+ :body (apply template args)})
+
+(defmethod render :text [_ text & args]
+ (merge {:status 200
+ :headers {"Content-Type" "text/html;charset=utf-8"}
+ :body text}
+ (apply hash-map args)))
+
+(defmethod render :json [_ map & args]
+ (merge {:status 200
+ :headers {"Content-Type" "application/json; charset=utf-8"}
+ :body (json/encode map)}
+ (apply hash-map args)))
+
+(defmethod render :resource [_ resource-name & args]
+ (merge (resp/resource-response (str "public/" resource-name))
+ (apply hash-map args)))
+
@@ -0,0 +1,18 @@
+(ns nsfw.server
+ (:require [ring.adapter.jetty :as jetty]))
+
+(def server nil)
+
+(defn stop []
+ (when server
+ (.stop server)))
+
+(defn start [entry-handler & [port]]
+ (stop)
+ (alter-var-root
+ (var server)
+ (fn [val]
+ (jetty/run-jetty entry-handler {:port (if port port 8080) :join? false}))))
+
+
+
@@ -0,0 +1,12 @@
+(ns nsfw.util
+ (:require [clj-stacktrace.repl :as stacktrace]
+ [clojure.string :as string]))
+
+(defn web-stacktrace [e req]
+ (str "<html><body>"
+ "<h1>500 - " (.getMessage e) "</h1>"
+
+ "<pre>" (stacktrace/pst-str e) "</pre>"
+
+ "<pre>" (string/replace (str req) #", " "\n") "</pre>"
+ "</html></body>"))
@@ -0,0 +1,5 @@
+(ns routes
+ (:use [net.cgrand.moustache :only (app)]
+ [nsfw.util :only (reload-handlers)]
+ [hiccup.core]
+ [clojure.contrib.json :only (json-str)]))
@@ -0,0 +1,6 @@
+(ns nsfw.test.core
+ (:use [nsfw.core] :reload)
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))

0 comments on commit a7822df

Please sign in to comment.