Fork me on GitHub
#ring
<
2021-04-14
>
fullNameHere00:04:39

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

seancorfield01:04:38

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

fullNameHere01:04:02

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?

seancorfield01:04:46

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

seancorfield01:04:44

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

seancorfield01:04:16

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.

seancorfield01:04:52

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

seancorfield01:04:59

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

fullNameHere01:04:09

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

seancorfield01:04:12

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

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

👍 3
seancorfield01:04:25

(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