-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathappengine.clj
More file actions
64 lines (57 loc) · 2.08 KB
/
appengine.clj
File metadata and controls
64 lines (57 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(ns cae.appengine
(:require [cae.core :as handler])
(:use ring.adapter.jetty))
(def myport 8080)
(def myserver (atom nil))
(defmacro with-app-engine
"testing macro to create an environment for a thread"
([body]
`(with-app-engine env-proxy ~body))
([proxy body]
`(last (doall [(com.google.apphosting.api.ApiProxy/setEnvironmentForCurrentThread ~proxy)
~body]))))
(defn login-aware-proxy
"returns a proxy for the google apps environment that works locally"
[request]
(let [email (:email (:session request))]
(proxy [com.google.apphosting.api.ApiProxy$Environment] []
(isLoggedIn [] (boolean email))
(getAuthDomain [] "")
(getRequestNamespace [] "")
(getDefaultNamespace [] "")
(getAttributes [] (java.util.HashMap.))
(getEmail [] (or email ""))
(isAdmin [] true)
(getAppId [] "local"))))
(defn environment-decorator
"decorates the given application with a local version of the app engine environment"
[application]
(fn [request]
(with-app-engine (login-aware-proxy request)
(application request))))
(defn init-app-engine
"Initialize the app engine services."
([]
(init-app-engine "."))
([dir]
(let [factory (com.google.appengine.tools.development.ApiProxyLocalFactory.)
env (reify com.google.appengine.tools.development.LocalServerEnvironment
(getAppDir [this] (java.io.File. dir))
(getAddress [this] "localhost")
(getHostName [this] "localhost")
(getPort [this] myport)
(waitForServerToStart [this])
(enforceApiDeadlines [this] true)
(simulateProductionLatencies [this] true))
delegate (.create factory env)]
(com.google.apphosting.api.ApiProxy/setDelegate delegate))))
(defn start-it []
(if (not @myserver)
(init-app-engine))
(reset! myserver
(run-jetty (environment-decorator #'handler/reload-handler)
{:port myport :join? false}))
(.start @myserver))
(defn stop-it []
(.stop @myserver)
(reset! myserver nil))