Permalink
Browse files

added bits to run in jetty, added Procfile for Heroku, fixes to Readm…

…e and TODO, code cleanups
  • Loading branch information...
1 parent 3ad15fb commit a9f46a4fb728582b5a7be895df5082df671d21f5 @kornysietsma kornysietsma committed Jun 2, 2011
Showing with 24 additions and 25 deletions.
  1. +1 −0 Procfile
  2. +3 −1 README.md
  3. +6 −7 TODO.md
  4. +14 −17 src/twitter_example/core.clj
View
@@ -0,0 +1 @@
+web: lein run -m twitter-example.core
View
@@ -48,15 +48,17 @@ This app needs a valid Twitter application to run. This is pretty easy to confi
* Note the "key" and "secret" values on the twitter app page
Now, when you run the app, it will display a form for entering the key and secret - enter the values above, and from there on it should be authenticated.
+**NOTE** if you get this wrong, there is currently no error handling to speak of - you'll need to stop and restart the app to change credentials!
If you are running the app a lot, you might want to post the key/secret via the command line - you can do this with curl:
curl -i -X POST -d '{"key" : "xxxxxxx", "secret" : "yyyyyyy" }' http://127.0.0.1:3000/initialize.json
-*Note* it's important to specify `127.0.0.1` not `localhost` - Twitter don't let you set up an app on localhost, and the twitter example gets the host name from the request
+*Note* it's important to specify `127.0.0.1` not `localhost` - Twitter don't let you set up an app on localhost, and the application gets the host name from the request
### Run the app
* run "lein ring server" - this will build the app, deploy it to a local server on (probably) port 3000, and then display the home page in a browser. It will initially prompt you for twitter credentials - see above for details
+* alternateively, run "lein run -m twitter-example.core" which will run in a local jetty server on port 8080, a somewhat more production-like environment
## How it works
View
13 TODO.md
@@ -1,15 +1,14 @@
# TODO
## important
-* set up for heroku deployment - possibly prompt for twitter credentials on first run?
+* add error handling on initialize call - especially if credentials not provided, or incorrect.
* use weavejester's suggested routing improvements - currently commented out as I had some problems with them
-* remove hardcoded port - should be able to get the port from the request
-* consider reverting to non-snapshot version of clj-oauth (first attempt failed, should try again!)
+* consider reverting to non-snapshot version of clj-oauth (first attempt failed, should try again!) and sandbar
## longer term
-* make links on tweets go to actual messages on twitter.com
-* add a proper jetty runner, and maybe a way to build a war file?
+* refactor - several functions in core.clj are too long
+* add some unit tests
* split main file up - though it's nice for examples to be a single file, it's a bit unstructured
-* error handling! Especially, handling simple things like when the user clicks "no" on twitter!
-* get rid of stateful sessions - we only write to the session from one spot, so could use default sessions
+* error handling!
+* get rid of stateful sessions - we only write to the session from one spot, so could use default sessions instead of sandbar
* try to get rid of ruby/node dependencies
@@ -4,7 +4,8 @@
ring.middleware.json-params
ring.middleware.stacktrace
ring.middleware.session
- sandbar.stateful-session)
+ sandbar.stateful-session
+ [ring.adapter.jetty :only [run-jetty]])
(:require [compojure.route :as route]
[compojure.handler :as handler]
[clj-json.core :as json]
@@ -13,7 +14,7 @@
[ring.util.response :as response]
twitter))
-; config is constructed once, via client posting to /set-config.json
+; config is constructed once, via client posting to /initialize.json
; - client detects the need for config from a failed (401) auth request with :initialized = false
(def config (atom nil))
@@ -72,7 +73,6 @@
(defroutes main-routes
(GET "/" [] (resource "public/index.html"))
(POST "/initialize.json" [key secret :as request]
- (println "init:" key secret request)
(if @config
(-> (json-response {:message "already initialized!"}) (response/status 500))
(let [{:keys [server-name server-port]} request
@@ -104,26 +104,19 @@
(fn [request]
(if (re-matches #"/auth/.*" (:uri request))
(cond
- (nil? @config) (->
- (json-response {:initialized false, :authorized false})
- (response/status 401))
- (session-get :twitter-oauth) (
- let [oauth (session-get :twitter-oauth)]
- (handler (assoc request :twitter-oauth oauth)))
+ (nil? @config)
+ (->
+ (json-response {:initialized false, :authorized false})
+ (response/status 401))
+ (session-get :twitter-oauth)
+ (let [oauth (session-get :twitter-oauth)]
+ (handler (assoc request :twitter-oauth oauth)))
:else
(let [request-token (twitter-request-token)
auth-url (callback-uri request-token)]
(session-put! :request-token request-token)
(-> (json-response {:initialized true, :authorized false, :authUrl auth-url})
(response/status 401))))
-
- ; (if-let [oauth (session-get :twitter-oauth)]
- ; (handler (assoc request :twitter-oauth oauth))
- ; (let [request-token (twitter-request-token)
- ; auth-url (callback-uri request-token)]
- ; (session-put! :request-token request-token)
- ; (-> (json-response {:initialized => true, :authorized => false, :authUrl auth-url})
- ; (response/status 401))))
(handler request))))
(comment ; this implementation doesn't work at the moment - problems with symbols in nested routes.
@@ -152,3 +145,7 @@
(wrap-oauth)
(wrap-stateful-session)
(wrap-json-params)))
+
+(defn -main []
+ (let [port (Integer/parseInt (get (System/getenv) "PORT" "8080"))]
+ (run-jetty app {:port port})))

0 comments on commit a9f46a4

Please sign in to comment.