<commit_before>(in-ns 'appengine-magic.core)

(use 'appengine-magic.local-env-helpers
     '[appengine-magic.servlet :only [servlet]]
     '[ring.middleware.file :only [wrap-file]])

(require '[clojure.string :as str]
         '[appengine-magic.jetty :as jetty]
         '[appengine-magic.blobstore-upload :as blobstore-upload])

(import java.io.File)

(import com.google.apphosting.api.ApiProxy)

(defn default-war-root []
  (-> (clojure.lang.RT/baseLoader)
      (.getResource ".")
      .getFile
      java.net.URLDecoder/decode
      (File. "../resources")
      .getAbsolutePath))


(defn open-resource-stream [resource-name]
  (-> (ClassLoader/getSystemClassLoader) (.getResourceAsStream resource-name)))


(defn wrap-war-static [app, #^String war-root]
  (fn [req]
    (let [#^String uri (:uri req)]
      (if (.startsWith uri "/WEB-INF")
          (app req)
          ((wrap-file app war-root) req)))))


(defmacro def-appengine-app [app-var-name handler & {:keys [war-root]}]
  `(def ~app-var-name
        (let [handler# ~handler
              war-root-arg# ~war-root
              war-root# (if (nil? war-root-arg#)
                            (default-war-root)
                            war-root-arg#)]
          {:handler (-> handler#
                        (wrap-war-static war-root#))
           :war-root war-root#})))


(defn make-appengine-request-environment-filter []
  (reify javax.servlet.Filter
    (init [_ _])
    (destroy [_])
    (doFilter [_ req resp chain]
      (let [all-cookies (.getCookies req)
            login-cookie (when all-cookies
                           (let [raw (first (filter #(= "dev_appserver_login" (.getName %))
                                                    (.getCookies req)))]
                             (when raw (.getValue raw))))
            [user-email user-admin? _] (when login-cookie
                                         (str/split login-cookie #":"))
            thread-environment-proxy (make-thread-environment-proxy :user-email user-email
                                                                    :user-admin? user-admin?)]
        (ApiProxy/setEnvironmentForCurrentThread thread-environment-proxy))
      (.doFilter chain req resp))))


(defn start* [appengine-app & {:keys [port join?]}]
  (let [war-root (java.io.File. (:war-root appengine-app))
        handler-servlet (servlet (:handler appengine-app))]
    (appengine-init war-root port)
    (jetty/start
     {"/*" [(make-appengine-request-environment-filter)
            (com.google.apphosting.utils.servlet.TransactionCleanupFilter.)
            (com.google.appengine.api.blobstore.dev.ServeBlobFilter.)]}
     {"/" handler-servlet
      "/_ah/login" (com.google.appengine.api.users.dev.LocalLoginServlet.)
      "/_ah/logout" (com.google.appengine.api.users.dev.LocalLogoutServlet.)
      "/_ah/upload" (servlet (blobstore-upload/make-blob-upload-handler war-root))
      ;;"/_ah/img" (com.google.appengine.api.images.dev.LocalBlobImageServlet.)
      "/_ah/channel/jsapi" (com.google.appengine.api.channel.dev.ServeScriptServlet.)
      "/_ah/channel/dev" (com.google.appengine.api.channel.dev.LocalChannelServlet.)
      ;; These mappings are from webdefault.xml in appengine-local-runtime-*.jar.
      "/_ah/sessioncleanup" (com.google.apphosting.utils.servlet.SessionCleanupServlet.)
      "/_ah/admin" (com.google.apphosting.utils.servlet.DatastoreViewerServlet.)
      "/_ah/admin/" (com.google.apphosting.utils.servlet.DatastoreViewerServlet.)
      "/_ah/admin/datastore" (com.google.apphosting.utils.servlet.DatastoreViewerServlet.)
      "/_ah/admin/taskqueue" (com.google.apphosting.utils.servlet.TaskQueueViewerServlet.)
      "/_ah/admin/xmpp" (com.google.apphosting.utils.servlet.XmppServlet.)
      "/_ah/admin/inboundmail" (com.google.apphosting.utils.servlet.InboundMailServlet.)
      "/_ah/resources" (com.google.apphosting.utils.servlet.AdminConsoleResourceServlet.)
      "/_ah/adminConsole" (org.apache.jsp.ah.adminConsole_jsp.)
      "/_ah/datastoreViewerHead" (org.apache.jsp.ah.datastoreViewerHead_jsp.)
      "/_ah/datastoreViewerBody" (org.apache.jsp.ah.datastoreViewerBody_jsp.)
      "/_ah/datastoreViewerFinal" (org.apache.jsp.ah.datastoreViewerFinal_jsp.)
      "/_ah/entityDetailsHead" (org.apache.jsp.ah.entityDetailsHead_jsp.)
      "/_ah/entityDetailsBody" (org.apache.jsp.ah.entityDetailsBody_jsp.)
      "/_ah/entityDetailsFinal" (org.apache.jsp.ah.entityDetailsFinal_jsp.)
      "/_ah/taskqueueViewerHead" (org.apache.jsp.ah.taskqueueViewerHead_jsp.)
      "/_ah/taskqueueViewerBody" (org.apache.jsp.ah.taskqueueViewerBody_jsp.)
      "/_ah/taskqueueViewerFinal" (org.apache.jsp.ah.taskqueueViewerFinal_jsp.)
      "/_ah/xmppHead" (org.apache.jsp.ah.xmppHead_jsp.)
      "/_ah/xmppBody" (org.apache.jsp.ah.xmppBody_jsp.)
      "/_ah/xmppFinal" (org.apache.jsp.ah.xmppFinal_jsp.)
      "/_ah/inboundmailHead" (org.apache.jsp.ah.inboundMailHead_jsp.)
      "/_ah/inboundmailBody" (org.apache.jsp.ah.inboundMailBody_jsp.)
      "/_ah/inboundmailFinal" (org.apache.jsp.ah.inboundMailFinal_jsp.)}
     :port port
     :join? join?)))


(defn stop* [server]
  (appengine-clear)
  (jetty/stop server))


(defmacro start [appengine-app &
                 {:keys [server port join?]
                  :or {server '*server* port 8080 join? false}}]
  `(do (defonce ~server (atom nil))
       (reset! ~server (start* ~appengine-app :port ~port :join? ~join?))))


(defmacro stop [& {:keys [server] :or {server '*server*}}]
  `(stop* @~server))<commit_msg>Cleaned up appengine-magic.core/start and stop functions; implemented a serve function which tries to do the right thing to start and restart intelligently.<commit_after>(in-ns 'appengine-magic.core)

(use 'appengine-magic.local-env-helpers
     '[appengine-magic.servlet :only [servlet]]
     '[ring.middleware.file :only [wrap-file]])

(require '[clojure.string :as str]
         '[appengine-magic.jetty :as jetty]
         '[appengine-magic.blobstore-upload :as blobstore-upload])

(import java.io.File
        com.google.apphosting.api.ApiProxy)



;;; ----------------------------------------------------------------------------
;;; appengine-magic core API functions
;;; ----------------------------------------------------------------------------

(defn default-war-root []
  (-> (clojure.lang.RT/baseLoader)
      (.getResource ".")
      .getFile
      java.net.URLDecoder/decode
      (File. "../resources")
      .getAbsolutePath))


(defn open-resource-stream [resource-name]
  (-> (ClassLoader/getSystemClassLoader) (.getResourceAsStream resource-name)))


(defn wrap-war-static [app, #^String war-root]
  (fn [req]
    (let [#^String uri (:uri req)]
      (if (.startsWith uri "/WEB-INF")
          (app req)
          ((wrap-file app war-root) req)))))


(defmacro def-appengine-app [app-var-name handler & {:keys [war-root]}]
  `(def ~app-var-name
        (let [handler# ~handler
              war-root-arg# ~war-root
              war-root# (if (nil? war-root-arg#)
                            (default-war-root)
                            war-root-arg#)]
          {:handler (-> handler#
                        (wrap-war-static war-root#))
           :war-root war-root#})))


(defn make-appengine-request-environment-filter []
  (reify javax.servlet.Filter
    (init [_ _])
    (destroy [_])
    (doFilter [_ req resp chain]
      (let [all-cookies (.getCookies req)
            login-cookie (when all-cookies
                           (let [raw (first (filter #(= "dev_appserver_login" (.getName %))
                                                    (.getCookies req)))]
                             (when raw (.getValue raw))))
            [user-email user-admin? _] (when login-cookie
                                         (str/split login-cookie #":"))
            thread-environment-proxy (make-thread-environment-proxy :user-email user-email
                                                                    :user-admin? user-admin?)]
        (ApiProxy/setEnvironmentForCurrentThread thread-environment-proxy))
      (.doFilter chain req resp))))



;;; ----------------------------------------------------------------------------
;;; development server controls
;;; ----------------------------------------------------------------------------

(defonce *server* (atom nil))


(defn start [appengine-app & {:keys [port join?] :or {port 8080, join? false}}]
  (let [war-root (java.io.File. (:war-root appengine-app))
        handler-servlet (servlet (:handler appengine-app))]
    (appengine-init war-root port)
    (reset!
     *server*
     (jetty/start
      {"/*" [(make-appengine-request-environment-filter)
             (com.google.apphosting.utils.servlet.TransactionCleanupFilter.)
             (com.google.appengine.api.blobstore.dev.ServeBlobFilter.)]}
      {"/" handler-servlet
       "/_ah/login" (com.google.appengine.api.users.dev.LocalLoginServlet.)
       "/_ah/logout" (com.google.appengine.api.users.dev.LocalLogoutServlet.)
       "/_ah/upload" (servlet (blobstore-upload/make-blob-upload-handler war-root))
       ;;"/_ah/img" (com.google.appengine.api.images.dev.LocalBlobImageServlet.)
       "/_ah/channel/jsapi" (com.google.appengine.api.channel.dev.ServeScriptServlet.)
       "/_ah/channel/dev" (com.google.appengine.api.channel.dev.LocalChannelServlet.)
       ;; These mappings are from webdefault.xml in appengine-local-runtime-*.jar.
       "/_ah/sessioncleanup" (com.google.apphosting.utils.servlet.SessionCleanupServlet.)
       "/_ah/admin" (com.google.apphosting.utils.servlet.DatastoreViewerServlet.)
       "/_ah/admin/" (com.google.apphosting.utils.servlet.DatastoreViewerServlet.)
       "/_ah/admin/datastore" (com.google.apphosting.utils.servlet.DatastoreViewerServlet.)
       "/_ah/admin/taskqueue" (com.google.apphosting.utils.servlet.TaskQueueViewerServlet.)
       "/_ah/admin/xmpp" (com.google.apphosting.utils.servlet.XmppServlet.)
       "/_ah/admin/inboundmail" (com.google.apphosting.utils.servlet.InboundMailServlet.)
       "/_ah/resources" (com.google.apphosting.utils.servlet.AdminConsoleResourceServlet.)
       "/_ah/adminConsole" (org.apache.jsp.ah.adminConsole_jsp.)
       "/_ah/datastoreViewerHead" (org.apache.jsp.ah.datastoreViewerHead_jsp.)
       "/_ah/datastoreViewerBody" (org.apache.jsp.ah.datastoreViewerBody_jsp.)
       "/_ah/datastoreViewerFinal" (org.apache.jsp.ah.datastoreViewerFinal_jsp.)
       "/_ah/entityDetailsHead" (org.apache.jsp.ah.entityDetailsHead_jsp.)
       "/_ah/entityDetailsBody" (org.apache.jsp.ah.entityDetailsBody_jsp.)
       "/_ah/entityDetailsFinal" (org.apache.jsp.ah.entityDetailsFinal_jsp.)
       "/_ah/taskqueueViewerHead" (org.apache.jsp.ah.taskqueueViewerHead_jsp.)
       "/_ah/taskqueueViewerBody" (org.apache.jsp.ah.taskqueueViewerBody_jsp.)
       "/_ah/taskqueueViewerFinal" (org.apache.jsp.ah.taskqueueViewerFinal_jsp.)
       "/_ah/xmppHead" (org.apache.jsp.ah.xmppHead_jsp.)
       "/_ah/xmppBody" (org.apache.jsp.ah.xmppBody_jsp.)
       "/_ah/xmppFinal" (org.apache.jsp.ah.xmppFinal_jsp.)
       "/_ah/inboundmailHead" (org.apache.jsp.ah.inboundMailHead_jsp.)
       "/_ah/inboundmailBody" (org.apache.jsp.ah.inboundMailBody_jsp.)
       "/_ah/inboundmailFinal" (org.apache.jsp.ah.inboundMailFinal_jsp.)}
      :port port
      :join? join?))))


(defn stop []
  (when-not (nil? @*server*)
    (appengine-clear)
    (jetty/stop @*server*)
    (reset! *server* nil)))


(defn serve [appengine-app & {:keys [port] :or {port 8080}}]
  (stop)
  (start appengine-app :port port))
{'fraction_non_alphanumeric': 0.17439847671801972, 'fraction_numerical': 0.0010386013501817552, 'mean_word_length': 2.3925425719318847, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5881064', 'n_tokens_mistral': 3778, 'n_tokens_neox': 3678, 'n_words': 561}