                  :body (html5 (views/main-view fiddle username))}
            :session (merge session
                            {:fiddle ns})))))
-   
+
    (GET "/view/:ns"
      {{:keys [ns as-of]} :params}
      (let [date (try (inst/read-instant-date as-of)

                         (let [db (db/save-fiddle conn
                                                  (util/fiddle (:cljs fiddle)
                                                               (:html fiddle)
                                                               (:css  fiddle)))] 
                           {:status :save-success
                            :date (subs (->> db d/basis-t d/t->tx (d/entity db) :db/txInstant pr-str)
                                        7 36)

         (assoc (res/redirect (if-let [fiddle (:fiddle session)]
                                (str "/fiddle/" fiddle)
                                "/"))
           :session (merge session {:token token
                                    :username username})))))

   (GET "/logout"
     []
     (assoc (res/redirect "/") :session nil))

   (context "/compiler" [] (closure/compile-routes conn))
   (context "/deps" [] (closure/deps-routes conn))

   (route/resources "/")
   (route/not-found "Not Found")))

(defn get-handler []
  (let [store (cookie/cookie-store {:key (env :session-secret)})
        db-uri (env :datomic-uri)
        conn (d/connect db-uri)]
    (-> (handler/site (app-routes conn)
                      {:session {:store store}})
        wrap-edn-params)))

(def app (get-handler))

(defn -main []
  (let [port (Integer/parseInt (or (env "PORT") "8080"))]
    (jetty/run-jetty (-> app
                         wrap-stacktrace)
                     {:port port :join? false})))

;; (.stop server)
