I have a defroutes like:
(defroutes app-routes
(context "first" []
(GET "/" [] "first first"))
(context "second" []
(GET "/" [] "first second")))
But when I try to visit /second I just get a 404. What am I supposed to do?
Minor, but important detail: you are missing leading slashes on the context declarations, so for example "/first", not "first".
(defroutes app-routes
(context "/first" []
(GET "/" [] "first first"))
(context "second" []
(GET "/" [] "first second")))
Related
When running this code :
(:use 'compojure.core)
(keys (ns-publics 'compojure.core))
(defroutes app-routes
(GET "/" [] "Hello World")
(route/resources "/")
(route/not-found "Not Found"))
I got this message:
CompilerException java.lang.RuntimeException: Unable to resolve symbol: defroutes in this context, compiling:(restful_clojure\routes.clj:5:1)
but when I run:
(keys (ns-publics 'compojure.core))
it shows that macro is defined:
(defroutes PUT POST routing routes make-route let-routes DELETE ANY let-request GET HEAD PATCH context OPTIONS)
Clojure has methods require, import, refer, and use. These are for working with different namespaces.
:use is a Keyword, which can behave like a function (in your example it should return nil), but does not do what you want.
The confusion likely arises from the fact that inside the ns macro, you can 'embed' the behavior of these functions using the corresponding keywords.
For more reading on namespaces, see this link.
I am working with Clojure, Ring and Compojure for some time now, but I am still fairly new.
What I need to know is how to make a configurable base route, for example:
/:base-route
/:base-route/user
/:base-route/settings
/:base-route can be different (api, company, stuff...) and will depend on an internal check. What I can't do is create a universal check for all routes to see if /:base-route has the appropriate value. For example /:base-route is configured to be /api, but the user tries /company/user -> the response must be 404.
Ok, so the way to do it is with a regular expression and a context:
(defroutes routes
(context ["/:base-route" :base-route (re-pattern base-route)] [base-route]
(GET "/user" [] (str "base: " base-route " user"))
(GET "/settings" [] (str "base: " base-route " settings"))))
I've created a new Compojure Leiningen project using lein new compojure test. Web server is run by lein repl and then
user=> (use 'ring.adapter.jetty)
user=> (run-jetty test.handler/app {:port 3000})
Routes and app handler specification is trivial:
(defroutes app-routes
(GET "/*.do" [] "Dynamic page")
(route/not-found "Not Found"))
(def app
(wrap-defaults app-routes site-defaults))
Now, after changing anything in app-routes definition (e.g. changing "Dynamic page" text to anything else, or modifying URI matching string), i do not get the updated text/routes in the browser. But, when changing app-routes definition slightly to
(defn dynfn [] "Dynamic page fn")
(defroutes app-routes
(GET "/*.do" [] (dynfn))
(route/not-found "Not Found"))
i do get dynamic updates when changing the return value of dynfn. Also, following the advice from this article and modifying the app definition to
(def app
(wrap-defaults #'app-routes site-defaults))
(note the #' that transparently creates a var for app-routes) also helps!
Why is that so? Is there any other way one could get a truly dynamic behaviour in defroutes?
Thanks!
#'app-routes is a reader macro that expands to (var app-routes). When a var is used as if it were a function, it is dereferenced anew on each invocation, and then the value returned by that deref is called.
If you were to supply app-routes as the argument, the compiler would give the dereferenced value to wrap-defaults, and when the var is updated, the previous value is not changed, so changing the var does not change the behavior of app.
The following repl transcript might be instructive:
user=> (defn foo [] "original")
#'user/foo
user=> (defn caller [f] #(f))
#'user/caller
user=> (def call-foo-value (caller foo))
#'user/call-foo-value
user=> (call-foo-value)
"original"
user=> (def call-foo-var (caller #'foo))
#'user/call-foo-var
user=> (call-foo-var)
"original"
user=> (defn foo [] "changed")
#'user/foo
user=> (call-foo-value)
"original"
user=> (call-foo-var)
"changed"
I'm using an AngularJS resource to basically $.ajax() some form data.
Post.put({user:$scope.getLoggedInUser(), action:"new"},{body:$scope.postBody, "public":$scope.postPublic}, function(post) {
On the form is a checkbox named "public."
I am using this function to merge the form data with the params from the URL:
(defn get-params [request]
(merge (:params request) (parse-string (slurp (request :body)) true)))
when I println from my route's handler like so I get a nice true when the checkbox is checked:
(println (:public (get-params request)))
However, when I pass the parameter (i.e. not the println) to another function in my controller that talks to the database, I do another println at the beginning of that function and get nil instead.
I've tried passing it as
(read-string x)
(boolean (Boolean/valueOf x))
(Boolean/valueOf x)
to no avail.
One thing that might be causing it (but I don't know why) is that I'm wrapping the request through authentication like this:
(auth? request #(create x y z))
where the (create) function creates a record in the database.
I can't get it to be true no matter what I've tried.
EDIT: more complete code
CONTROLLER
(defn auth? [request callback-true & [callback-false]]
(println (callback-true))
(let [login-response (auth/login request)]
(if (and (not (string? login-response))
login-response)
(callback-true)
(if callback-false
(callback-false)
(json-response (str "{\"auth\":" login-response "}"), 401)))))
(defn create [user logged-in-user body public?]
(if (= logged-in-user user)
(json-response (post-view/single-post (post/create user body public?)))
(json-response "{\"auth\":\"can't create post under different logged in user\"}" 401)))
(defroutes routes
....
(PUT "/api/:user/new" request
(println request)
(auth? request
#(create (:user (request :params))
(:user (request :session))
(:body (get-params request))
(:public (get-params request)))))
....
)
MODEL
(defn create [username body public?]
(println public?)
(when-not (and
(str/blank? body)
(str/blank? username))
(let [user-id (:id (get-id-from-username username))
new-post-id
(:id
(sql/with-connection db
(sql/insert-values :post
[:usr_id :body :public] [user-id body (Boolean/valueOf public?)])))]
(by-id new-post-id))))
Don't call get-params twice. (request :body) returns a stream, which can't be read twice.
I want to create a handler function which takes two inputs. One is a parameter taken from the url /name, and second is a param from the query string /name?x=3
(def my-app (app
[page-name] (handler page-name)))
(defn handler
[{:keys [params]} page-name]
(let [x (params "x")]
(-> (page-templ page-name x) response constantly)))
The above fails because the handler is expecting 2 params, however I am only passing one.
How do I get hold of the request map, and pass it to the handler ?
The request map in the above case contains a param named x.
It is best if you could dispatch on the page name, like that:
(app
[""] (index-page)
["login"] (serve-login))
Here functions index-page and serve-login return function of one argument.
(defn index-page[]
(fn [req] ..))
req is the request that will contain all the url parameters in key/value map. To get parameter value do this:
(-> req (get :params) (get :x))
So the full solution would look something like this:
(def my-app (app
["page1-name"] (handler)))
(defn handler []
(fn [req]
(let [x (-> req :params :x)]
(-> (page-templ page-name x) response))))
EDIT: Don't forget to wrap you application into (wrap-keyword-params) and (wrap-params), here's how you can do it:
(def my-wrapped-app
(-> my-app
(wrap-keyword-params)
(wrap-params))