Could not locate compojure in classpath - clojure

I am trying the various Getting started examples and I can get a basic hello world example working with basic HTML in the route as such
(ns hello-world
(:use compojure.core ring.adapter.jetty)
(:require [compojure.route :as route]))
(defroutes example
(GET "/" [] "<h1>Hello World Wide Web!</h1>"))
(run-jetty example {:port 8080})
But when I attempt to use the html helpers like so
(ns hello-world
(:use compojure ring.adapter.jetty)
(:require [compojure.route :as route]))
(defroutes example
(GET "/" []
(html [:h1 "Hello World"])))
(run-jetty example {:port 8080})
Then I get the following error
[null] Exception in thread "main" java.io.FileNotFoundException: Could not locate compojure__init.class or compojure.clj on classpath: (core.clj:1)

As W55tKQbuRu28Q4xv mentions in a comment, you use (:use compojure ...) in the second example. You should switch to (:use compojure.core ...) and then maybe pull in some additional dependencies for the other functionality that you use (like hiccup (<- this is a link to the GitHub repo), which is now a separate project, for the HTML-building DSL).
My guess is that you're trying to follow some tutorials written for Compojure 0.3 while using Compojure 0.4. The latter does not include the compojure namespace at all and has been slimmed down a lot, with the basic HTTP handling delegated to ring and various other pieces of functionality spun off to separate projects (like the aforementioned hiccup).
Fortunately there are good resources on the 0.3 -> 0.4 transition, e.g. this blog entry by Brenton Ashworth. If you can't find something that's been removed from Compojure proper, chances are you'll be able to learn where to find it now from that. See also this follow-up discussion on Compojure's Google group for errata and additional details.

I played around with a Compojure "Hello World" and had this problem (as well as many others that are getting muddled in my brain). Another complication is a lot of the Compojure documentation on the web is already out of date. Bottom line, these are the step you want to follow:
Have an up-to-date version of Leiningen. Make sure you follow the installation instructions on the github site. (Do not go through macports; their Leiningen is out of date.)
Follow Compojure instructions here.
Note that the file name is incorrect. It should be src/hello_www/core.clj NOT src/hello-www/core.clj.

Related

Adding Front End to Existing Clojure Terminal App

I have a terminal app built in Clojure and connected to MySQL, but I would like to know the simplest way to add a front-end to it. I came across this post (that is 5 years old) that recommends ring, and set it up: Add webapp frontend to existing clojure app.
After adding deps to project, added handler function to core.clj:
(defn handler [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body "Hello World"})
Started a webserver and got output "Hello World."
Ok, now what? I am familiar with templates like Figwheel that allow you to build Clojurescript right out of the box (that doesn't come with a back-end?) and tools like Reagent that allow you to write React with Clojurescript, but what I am interested in is, how to add a front-end to an already existing terminal app, in whatever framework I choose, be it plain React, Clojurescript with Reagent, Angular, or even old-fashioned HTML/CSS/Javascript. It seems there are lots of templates but no instructions for how to do it from scratch.
Any of the following would have been acceptable answers: Selmer, Hiccup, Enlive.

Simple Clojure and CouchDB example for web app

I want to experiment writing a web app with Clojure and CouchDB, but I can't find many examples to help me get started. Searching around the web a bit I see that I could use this combination of libraries:
https://leiningen.org/
http://www.luminusweb.net/
https://github.com/clojure-clutch/clutch
The first two seem popular, but what about clutch, it doesn't look actively developed -- is it a good choice?
Assuming I use clutch, I'm struggling to find much example code, this seems to work, but is it the right approach?
Project creation:
lein new luminus couchdb-test
Add this in project.clj in :dependencies:
[com.ashafa/clutch "0.4.0"]
Run project:
lein run
And then in the REPL:
(require ['com.ashafa.clutch :as 'clutch])
;; Test connection to DB - I've created one called wiki
(clutch/get-database "wiki")
;; Get a list of all documents
(clutch/with-db "wiki"
(clutch/all-documents))
;; Output all docs with contents
(clutch/with-db "wiki"
(clutch/all-documents {:include_docs true}))
;; Output titles of all docs that have one
(map (fn [result] (:title (:doc result)))
(clutch/with-db "wiki"
(clutch/all-documents {:include_docs true})))
Any corrections, hints, or pointers to better examples would be great.

How can I communicate with the backend using ClojureScript and Figwheel?

Note: I'm a moderately experienced programmer in general and using clojure but have never done serious web development.
I set up a basic ClojureScript project using Chestnut and walked through the "Hello World" steps just fine. However I would really like to talk to my backend as well. For this purpose I redefined the Reagent code to be
(defn greeting []
[:input {:type "button"
:value (:text #app-state)
:on-click #(http/get {})}])
Which gets a 404 response when clicked. So at least I'm talking to somebody. I can also see evidence of my get-requests in the server.log file. However at this point I'm struggling with a number of conceptual points.
First of all http/get is a function defined in clj-http.client, which wasn't part of the Chestnut setup. It feels like I'm already off track if I have to go hunting for libraries to send something as basic as a get-request.
Secondly the file for the user namespace has the following lines predefined by Chestnut:
(def http-handler
(wrap-reload #'mypage.server/http-handler))
(defn run []
(figwheel/start-figwheel!))
I can't see any place where the http-handler is ever used. So I don't understand what that definition even does.
Also the way I understand Figwheel, when I call "run" it'll spin up a new web server which then a) serves the index.html and b) connects to my browser via some TCP port and starts pumping new JavaScript through that connection. This second part is very speculative on my part. If this is actually what happens my next question would be if Figwheel also needs to sit on the other side of that connection or if browsers have some common API that allows code reloading from the outside.
Lastly I can kinda tell that the ring routes and http-handler defined in the mypage/server.clj file (below) are being called somehow, since modifying these changes the error from the get-request, however it's a complete mystery to me how this works. The way I understand it the get-request I'm sending from the browser is sent to the Figwheel-server, the origin of the site. I have no reason to believe that Figwheel knows anything about the http-handlers I've defined in the server file.
(defroutes routes
(GET "/" _
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (io/input-stream (io/resource "public/index.html"))})
(resources "/"))
(def http-handler
(-> routes
(wrap-defaults api-defaults)
wrap-with-logger
wrap-gzip))
For a ClojureScript project (i.e. not Clojure), I believe you want this: https://github.com/r0man/cljs-http
I don't have a complete answer, but I do have a couple points that might help.
Clojurescript ultimately compiles down to JavaScript, and makes heavy use of (and has access to) the Google Closure library. So if you really want to, you can use JavaScript interop to just make an AJAX call from the client-side, just like you would in JS. The closure library provides a wrapper for this---see docs here https://developers.google.com/closure/library/docs/xhrio . But there are also several easy http and Ajax libraries for clojurescript, so why not use them? Another part of the joy and magic of clojurescript is that the Google closure optimizations that get applied do lovely things like strip out dead code, so I believe (and someone else can correct me if I'm wrong) that there is little (no?) production cost to putting some extra libraries in.
Figwheel is ultimately dev, not production, and doesn't depend on the http server that you set up for production use. Indeed, there are templates out there for front-end-only cljs projects that still use figwheel--- here's one example. Figwheel spins its own server to push changes to the browser, I'm not quite sure how it works.

Clojure ^shared in namespace

I am following the pedestal tutorial, and I noticed the ^shared annotation in the code, like below :
(ns ^:shared tutorial-client.behavior
(:require [clojure.string :as string]
[io.pedestal.app.messages :as msg]))
What is it useful for ?
The ^shared annotation is for indicating to the ClojureScript compiler to compile this .clj file, where it would normally ignore it. This lets you write shared code that can be run on the client and server (as long as it doesn't use platform specific code). This was before cljx and cljc files and AFAIK is specific to the Pedestal build process, not part of vanilla ClojureScript.
The supported way to write platform specific code is using Reader Conditionals, new in Clojure 1.7.
As a side note, Pedestal App is effectively deprecated, Pedestal Server is still maintained.
It indicates that code should be compiled both as clojure, for use server-side, and as clojurescript, for use in the browser.
From the pedestal wiki:
When compilation occurs, any Clojure namespaces marked :shared will
also be compiled to ClojureScript.
(ns ^:shared tutorial-client.behavior
(:require [clojure.string :as string]
[io.pedestal.app.messages :as msg]))
For now, these files must contain the common subset of Clojure and
ClojureScript. A new feature of Clojure 1.6, feature expressions, will
allow us to break free from this restriction.

What does "#'symbol" notation mean in clojure?

I'm learning clojure on Heroku using this tutorial. I've come across the same thing in other tutorials, as well.
Anyway, using the jetty adapter in ring, you have something like this:
(defroutes routes
...)
(defn start []
(ring/run-jetty #'routes {:port 8080 :join? false}))
I don't understand what #'routes means. If I replace it with just routes it seems to work fine. What does the #'symbol notation mean? It's been very difficult to research.
It's a reader macro. #'foo expands to (var foo). See Is pound-quote (hash-quote, #') in Clojure running the resolve and symbol functions?, Difference between Symbols and Vars in Clojure and http://clojure.org/vars where you can find in-depth discussion.