ring

2021-04-14T00:50:39.122400Z

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

seancorfield 2021-04-14T01:31:38.124300Z

@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))

2021-04-14T01:39:02.125600Z

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?

seancorfield 2021-04-14T01:39:46.125800Z

Because the GET (and other macros) require a binding form.

seancorfield 2021-04-14T01:40:44.126Z

I tend to do this instead: (GET "/foo" [] #'my-handler) β€” [] don’t bind anything particular, pass the whole Ring request to my-handler

seancorfield 2021-04-14T01:41:16.126200Z

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.

seancorfield 2021-04-14T01:41:52.126400Z

See https://github.com/seancorfield/usermanager-example as a good example for getting started with Ring, Compojure, and a couple of other libs.

seancorfield 2021-04-14T01:42:59.127100Z

The [id :&lt;&lt; as-int] is relying on compojure.coercions to turn certain parameters into int etc.

2021-04-14T01:43:09.127300Z

Ill look into it. Why lie, im kinda walking in the dark

seancorfield 2021-04-14T01:45:12.127500Z

You can look at the difference between those binding forms like this:

(println (macroexpand '(GET "/" v (foo v))))
(println (macroexpand '(GET "/" [v] (foo v))))

πŸ‘ 1
seancorfield 2021-04-14T01:45:25.127700Z

(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