Menu
  • HOME
  • TAGS

How to get the client IP address in ring-clojure?

clojure,ring

Getting :remote-addr from the request object is typically the correct way to go unless you're sitting behind a load balancer. In that case your load balance may add a header e.g. x-forwarded-for to the request. Which would make something like this appropriate. (or (get-in request [:headers "x-forwarded-for"]) (:remote-addr request)) ...

How to handle session in Clojure web application tests written with ring-mock?

unit-testing,session,clojure,ring

As per the suggestion by James Reeves through twitter, I was able to solve this problem by using peridot. It has session handling for writing tests.

Using websockets in Clojure Ring app deployed to Tomcat

tomcat,clojure,websocket,elastic-beanstalk,ring

Yes, http-kit is meant to be used in lieu of Tomcat, but you can use a reverse proxy like nginx. If that's something you really don't want to do, you could consider using Pedestal with SSE instead of websockets. Other than that, it doesn't look like there are any ring...

How to run compojure rest server?

jar,clojure,ring

You need to invoke lein ring uberjar to generate a self-start server you can run with java -jar target/uberjar/yourproject-0.1.0-SNAPSHOT-standalone.jar I suggest you to use the standalone one to avoid dealing with dependencies Edited to include the specific command...

Specify the ClojureScript file I want to include

javascript,clojurescript,compojure,ring,cljsbuild

The compiler is optimized for generating one file per app. You could use multiple builds to do something like what you want, but you would have common code and blobs in both pages (builds), which is not optimal.

Get URL parameters from ring

clojure,ring

ring.middleware.params/parse-params expects only to receive a query parameter string so you need to remove everything before and including the ?, something like this will work: (#'ring.middleware.params/parse-params (second (.split "http://www.youtube.com/watch?v=P76Vbsk_3J0" "\\?")) "UTF-8") ;=> {"v" "P76Vbsk_3J0"} Note that you are using a function that is not a part of the public API....

is there a var_dump like function in clojure?

clojure,jsoup,ring

To print to a string, use pr-str. Or use println to print to the REPL. (pr-str {:a 1}) ;=> "{:a 1}" (println {:a 1}) ;=> nil ***REPL contains output*** If you want more options and pretty printing, you could try clojure.pprint or fipp (require '[clojure.pprint :refer [pprint]]) (pprint {:a 1});...

Clojure: wrap-ssl-redirect on heroku?

https,clojure,ring

Long live lib-noir and its wrap-force-ssl fn. I just needed to change it a bit to fit my needs (I have a SSL cert. for one subdomain even if my clojure app handles more then one domain) Here's my fn: (defn wrap-force-ssl "Almost like in lib-noir. If the request's scheme...

Lein ring uberjar

clojure,leiningen,ring

That's looking wrong. :ring {:handler (webdev.core/-main) :port 8000} (webdev.core/-main) means running your main function and delegating the result of the main Function as the handler to the ring-plugin. Try using something more like this when using lein ring ... :ring {:handler webdev.core/app :port 8000} ...

lein ring server with nrepl doesn't honour cider-nrepl

emacs,clojure,leiningen,ring,cider

Use latest lein-ring plugin version 0.9.2 and add :nrepl-middleware containing vector of nrepl-middlewares to :repl-options in your project.clj For example, I create the project by lein new compojure-app my-app. Then, I tested it by creating a empty leiningen profile in ~/.lein/profiles.clj ex. {:yolo {}} and starting ring server by lein...

How to add keys to a map / create keys in current map clojure

clojure,ring

The built in function clojure.set/rename-keys will do this nicely, though like all things in clojure there are several approaches. So lets walk through the process. What you have is very close to working. If I just lookup each by it's original string key, we can see it work: user> (def...

Dynamic handler update in Clojure Ring/Compojure REPL

clojure,read-eval-print-loop,ring,compojure

#'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...

How to apply a ring-middleware to specific group of routes?

clojure,compojure,ring

Here is comporoute, a ring handler without any macro magic, aimed at composability and extensibility. Even though it's in early alpha state it has precise docstrings already. It has a feature called inner middleware to solve the issue you are having. You may (and should) use it only for what...

Can `lein ring server` command have other params?

clojure,ring

Your use-case is exactly what Leiningen profiles are for. For example lein with-profile test ring server ...

NullPointerException when lein ring server on Aleph+Ring

clojure,ring,compojure,aleph

The Lein-Ring plugin uses an embedded Jetty web server, while Aleph uses the asynchronous Netty web server. The aleph.http/wrap-aleph-handler middleware is designed only to work with a Netty server started with aleph.http/start-http-server function.

Compojure with configurable base route

clojure,ring,compojure

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")))) ...

How to integrate figwheel with ring server to get back-end auto-reload?

clojure,clojurescript,ring

The ring middleware wrap-reload will do this for you. There is also a very nice leiningen template called Chestnut which will set up a project for you with Figwheel and an auto reloading Ring backend. This question shows an example of wrap-reload usage Compojure development without web server restarts...

How can I use ring anti-forgery / CSRF token with latest version ring/compojure?

clojure,ring,compojure,csrf-protection

The problem was that ring-defaults (which replaces the compojure.handler namespace in compojure >= 1.2) automatically uses ring anti-forgery in the usual mode of use: (defroutes app-routes (GET "/" [] (generate-string {:csrf-token *anti-forgery-token*})) (POST "/send" [email] "ok") (resources "/") (not-found "Not Found")) (def app (-> app-routes (wrap-defaults site-defaults))) So two anti-forgery...

SOLVED: implementing oauth2 in compojure, how do I wait for the second oauth2 callback before responding to the user's request?

clojure,oauth-2.0,ring,compojure,luminus

Based on the documentation for Google's OAuth2 for web servers here, the flow consists of the following steps: Your application redirects a browser to a Google URL; the URL includes query parameters that indicate the type of access being requested. The result is an authorization code, which Google returns to...

Accessing session data in ring middleware

clojure,ring,compojure

Found a solution. Turns out this problem is 'somewhat' related to Compojure/Ring: Why doesn't a session with cookie-store survive a server restart?, which explains that 2 session middleware are being used: One by compojure, one by wrap-session. After changing both middleware to the same storage engine, the atom is filled...

Registering multiple handlers while running server

clojure,ring,compojure,http-kit

You can use compojure's routes function. You can also pass several handlers to defroutes, an example is provided below: (defroutes get-routes (GET "/events" [] "Event API") (GET "/" [] "Welcome")) (defroutes post-routes (POST "/events" [] "Post Event API")) (def all-routes (routes get-routes post-routes)) (defn -main [] (run-server all-routes {:port 5000}))...

How to use compojure from Intellij

intellij-idea,clojure,ring,compojure

You want to run the server from inside the repl? Add [ring/ring-jetty-adapter "1.3.1"] as a dependency In the REPL: (require 'ring.adapter.jetty) (require 'quals.core.handler) ; require YOUR ns containing the handler (ring.adapter.jetty/run-jetty quals.core.handler/app {:port 3004}) You can see all the parameters you can pass here: http://mmcgrana.github.io/ring/ring.adapter.jetty.html There you have it, the...

ring redirect after login

redirect,clojure,ring

response is return a body. you code is (response requri),but the param of the funtion reponse is html body,not a uri,you can use the this function like this (ns foo (:require [ring.util.response :as response])) (def requi "/") (-> (response/redirect requri) (assoc :session new-session) (assoc :headers {"Content-Type" "text/html"})) ps: if you...

How do I use Ring to output my post data in a request?

clojure,ring,compojure

compojure's destructuring tries to access the query/form parameter :req in your example, not the whole request. You have two possibilities: (POST "..." req ...) and (POST "..." [something :as req] ...) Both store the request in req, the second variant allows you to still use destructuring , though....

Clojure: Use Rook and Ring to provide a REST interface and serve static HTML/JS

rest,clojure,ring

I'm not very familiar with rook, and after taking a quick skim of the docs and a bit of the code I don't see static resources baked into it. If this is in error, please forgive me. It looks like rook does a lot, but in the end it's still...

Best way to customize Swagger / Compojure 404 response

clojure,swagger,ring,compojure

There is no definition in the Swagger Spec for the "missed everything else" default handler of Compojure. Swagger is about documenting existing apis, not the non-existing. Just discussed about this at #swagger on freenode. Still, you can describe the default routes manually with compojure-api, created a sample here: https://gist.github.com/ikitommi/cdf19eeaf4918efb051a. Note:...

Clojure Ring: How to determine if development server is running?

clojure,leiningen,ring

The idiomatic way to do this is to use leiningen profiles, using the :dev profile. You can then ensure inside the dev profile that your ClojureScript build is happening without optimization, cf. the leiningen cljsbuild compilation profiles. If you want to be able to identify the devserver running, you can...

Timeout with Python Requests + Clojure HttpKit Server but not Ring server

python,clojure,python-requests,ring,http-kit

I found how to fix this, but no satisfactory explanation. I was using wrap-json-response Ring middleware to take a HashMap and convert it to JSON. I switched to doing my own conversion in my handler with json/write-str, and this fixes it. At a guess it might be something to do...

Breaking down the ring middleware scenario

clojure,middleware,ring

because ring just uses function composition for middleware you can simply wrap friend around the call to wrap defaults: (def app (my-additional-mieddleware (wrap-defaults app-routes site-defaults) arguments to my additional middleware)) or you can thread it (for instance when you have several middlewares): (def app (-> (wrap-defaults app-routes site-defaults) (friend-stuff arg...

How can one manage front-end library dependencies in Clojure?

twitter-bootstrap,clojure,ring,compojure

Bower fulfils my requirements. Prerequisites are installing Bower through NPM and lein-bower Leiningen plugin. Below are the configuration details I had to add to project.clj: :plugins [[lein-bower "0.5.1"]] :bower-dependencies [[bootstrap "3.3.2"]] :bower {:directory "resources/public/lib"} @Aleš Roubíček - thanks for your comment....

Use a get parameter as a function name

clojure,ring

It seems a bit of a risky proposition from a security and maintainability standpoint to let the Internet call any symbol in your namespace as a function, so my thought would be to instead do something like: Prepare a white-list of functions users should be able to request, in the...

Is there some way to let compojure support type conversion automacally?

clojure,type-conversion,ring,compojure

This is not currently possible with only Compojure. You could use Prismatic schema coercion. (require '[schema.core :as s]) (require '[schema.coerce :as c]) (require '[compojure.core :refer :all]) (require '[ring.middleware.params :as rparams]) (def data {:para1 s/Str :para2 s/Int s/Any s/Any}) (def data-coercer (c/coercer data c/string-coercion-matcher )) (def get-uri (GET "/uri" r (let...

Parsing Facebook's Signed Request in Clojure

facebook,clojure,leiningen,compojure,ring

I think the basic answer to your question is that functional programming is more about input and output (think of a mathematical function), whereas imperative programming tends to be more about side effects and mutable data. Instead of thinking, "what do I need to do?", think, "what kind of data...

Making sure a var is bound in Clojure

clojure,nullpointerexception,ring

Use when-let or the similar if-let to check whether you actually have a session or not: (defn logged-in-verify [ring-handler] (fn new-ring-handler [request] ;;verify that the scrypt hash of email and timestamp matches. (if-let [session (:session request)] (let [email (:ph-auth-email session) token (:ph-auth-token session) timestamp (:ph-auth-timestamp session)] (if (scryptgen/check (str email...

Unable to parse out EDN in this server request

clojure,ring

EDIT: As an answer to the updated question, the wrap-edn-params function consumes the body of the request by fully reading the :body InputStream. Compojure routes passes the request to each parameter handler until a non nil value is returned. In this case, whichever handler is passed to routes as the...

Compojure app runs locally, but cannot find main class lein when deployed to Heroku

heroku,clojure,leiningen,ring

Your procfile should be something like this: web: lein ring server-headless $PORT To check your application can run properly on heroku, you can use a foreman port, which makes use of the procfile. These days I am using gaffer, and the documentation on Procfile for Heroku is here....

Midje provided not stubbing function in Compojure / Ring handler

unit-testing,clojure,ring,compojure,midje

I discovered today that I had my scopes confused - the let block introducing response was outside of the fact call that included the provided. Thus the response was created before the provided was invoked. Working code which passed this early test instead used the against-background call (facts "It returns...

Cemerick Friend Interactive Form - Unrecognized body

authentication,clojure,ring

Turns out the problem was the order of my middlewares. I needed to wrap-json-body after friend-authenticate.

How do I add webjars resources to lib-noir's app-handler?

clojure,ring,compojure,webjars,lib-noir

This seems to do it: (defroutes app-routes (route/resources "/") (route/resources "/" {:root "META-INF/resources/"}) (route/not-found "Not Found")) ...