Hey guys I know this code isnt ideal. Eventually I would like to add Authentication/Authorization with an actual database such as postgres, but thats a different beast for a different day. I dont understand why (get-login-infoV2) is printing false. (def users {:email "user1" :password "fakepass"}) (defn get-login-infoV2 [req] (let [{:keys [params]} req param-name (get params "name") email (get params "email") password (get params "password") user-exists (does-user-exist? users email password)] (html [:h1 (str user-exists) " lost"]))) (defn login [] (html [:div (form/form-to [:post "get-login-info"] [:label {:for "email"} "Email:"] [:br] [:input {:type "text" :id "email" :name "email" :placeholder "email"}] [:br] [:label {:for "password"} "Password:"] [:br] [:input {:type "text" :id "password" :name "password" :placeholder "password"}] [:br] [:br] [:input {:type "submit" :value "Submit"}])])) (defn main [req] "<div> <h1>Hello Web Page with Routing!</h1> <p>What would you like to do?</p> <p><a href='./get-form.html'>Submit a GET request</a></p> <p><a href='./post-form.html'>Submit a POST request</a></p> <p><a href='./login'>login</a></p> </div>") (defroutes routes (GET "/" [req] (main req)) (GET "/login" [] (login)) (GET "/doesnt-exist" [] (doesnt-exist)) (GET "/does-exist" [] (does-exist)) ;(GET "/test" (test1)) (POST "/get-login-info" [req] (get-login-infoV2 req)) ;(GET "/does-user-exist?" req (does-user-exist?)) (not-found (not-found1))) (def app (-> routes p/wrap-params)) (defonce server (run-jetty #'app {:port 8080 :join? false})) Thanks in advance! actual code https://github.com/SantoHerrera/learning-compojure/blob/main/src/learning_compojure/core.clj
@santosx99xx You want
(POST "/get-login-info" req (get-login-infoV2 req))
not
(POST "/get-login-info" [req] (get-login-infoV2 req))
The first form binds the whole request to req
, the second form only binds named pieces of the URI, such as if you had (GET "/example/:id" [id] (do-stuff id))
Thanks, it was driving me a little crazy. Another question would be, how come I need to have req or [] or [req]. If I just have something like (GET "/login" (login)), I get an error?
Because the GET
(and other macros) require a binding form.
I tend to do this instead: (GET "/foo" [] #'my-handler)
β []
donβt bind anything particular, pass the whole Ring request to my-handler
The #'
just means you get the Var, not the function value, so you can still redefine the function while the server is running and changes get picked up.
See https://github.com/seancorfield/usermanager-example as a good example for getting started with Ring, Compojure, and a couple of other libs.
In particular: https://github.com/seancorfield/usermanager-example/blob/develop/src/usermanager/main.clj#L128-L134
The [id :<< as-int]
is relying on compojure.coercions
to turn certain parameters into int
etc.
Ill look into it. Why lie, im kinda walking in the dark
You can look at the difference between those binding forms like this:
(println (macroexpand '(GET "/" v (foo v))))
(println (macroexpand '(GET "/" [v] (foo v))))
(compojure.core/make-route :get #clout.core.CompiledRoute{:source /, :re #"/", :keys [], :absolute? false} (clojure.core/fn [request__2341__auto__] (compojure.core/let-request [v request__2341__auto__] (foo v))))
nil
(compojure.core/make-route :get #clout.core.CompiledRoute{:source /, :re #"/", :keys [], :absolute? false} (clojure.core/fn [request__2341__auto__] (compojure.core/let-request [[v] request__2341__auto__] (foo v))))
nil