Hello guys! I started learning Clojure a month or two ago and want to try building a Clojure API, but I'm having trouble getting started. Can someone please help me?
What exactly do you mean by "Clojure API"? An HTTP API backed by a server implemented in Clojure?
@p-himik Yes, I want to make a simple web API CRUD for users, very simple. I want to make the all process in one JSON, but i really cannot to make this...
Plenty of materials on the topic, in different formats. Just a single article I know of: https://www.baeldung.com/clojure-ring A small full-stack app demo: https://github.com/seancorfield/usermanager-example Of course, these are not the ways to do it. There are always alternatives to every aspect. You might not like some particular aspects mentioned at the above links - that's fine. If that happens, you can always ask here or search for an alternative and see how it can be used instead. You might like everything but encounter some issues specific to your requirements later, or your taste might change - that's also fine, all part of a learning process.
this is still very complicated for me haha, but i will read this tor try learning the process. I guess to Clojure is very hard for start to learn man...
Clojure it not hard to learn by any means. But it can be made harder by some personal factors, like e.g. prior exposure only to non-FP languages. We can go through the first article step by step. Where do you start having problems?
Ah, also - if you have literally zero prior experience with Clojure, then building an HTTP API is probably not the best place to start.
You mentioned that you started with Clojure a month or two ago, but it doesn't make it clear what your current state is w.r.t. knowing the language.
Right, I make a curse of introduction of Clojure, and I know how to program on TypeScript, but I don't have many experience in general, because I started to working as Intern have 8 months. I understand the syntax and functional paradigm, but I believe that I really don't understand the all steps for make a API. My problems still that a try to create a POST route, I have a repository on GitHub, you can see for me please?
Sure.
Is very simple because i not have much knowledge haha, this is a link of repository: https://github.com/GustavoMaiaOliveira/api-crud-clojure and thank you for the attention you are giving me
Ah, you're using Pedestal. It uses different abstractions as compared to Ring (which is used in both of the links I shared above).
So you'd need to check Pedestal-related materials, not Ring-related ones.
As far as I can tell, the official Pedestal docs are quite thorough: https://pedestal.io/pedestal/0.6/guides/your-first-api.html
They also mention that you can seek help with Pedestal here in the #pedestal channel.
I myself don't know Pedestal at all so can hardly help here except for some generic things.
Like for example, your main namespace is named http-client and it uses http-server.
In general, an HTTP client is something that makes HTTP requests to an HTTP server. But in your case, it's just another namespace that actually starts the server. So the ns naming is definitely off.
Right, thanks for help bro!!!
I would go for the latest 0.8 release candidate of pedestal, because there where quite a few changes and I think the documentation has also been improved.
Sure, have a 0.8 version, I go to change this, thankss bro!
I just went through the updated pedestal docs myself yesterday and was favourably impressed. Besides the intro to the lib/API implementation, it was a useful intro to use of the repl in JVM clojure development ( I've only done CLJS before, and the repl flow is quite different)
Yes man, I really like the Pedestal Documentation, it's very complete!
What is the typical way of writing Clojure/Script code? Do you repl line by line when writing a function, or only repl eval once the full function has been written? What works for you best?
Find whatever granularity works for you, but the really important point is to have a very direct "connection" between your editor and your REPL: between typing a close paren and evaluating the corresponding form, you should not need to explicitly copy-paste anything, and it should not involve a mouse.
Just some keyboard shortcut (like Alt+Enter or Ctrl+Enter with VSCode/Calva on Linux/Windows).
For Clojure, I use the "reloaded" workflow with a bit of manual REPL evaluation here and there. So I make a set of necessary changes and experiment in my REPL during that time, and in the end I hit a single shortcut and all my changes are reloaded in a way that automatically restarts all the dependent components. For ClojureScript, I use automatic code reloading and almost never use REPL.
I only do Clojure (backend, not cljs) and I don't use any sort of reloaded workflow -- just pure REPL evaluation. I tend to start work inside a (comment ..) form and build up code, eval'ing everything as I go (`ctrl+alt+enter` for me), and lift that into a function when it's closer to working (and eval that defn) and then add various calls of it inside the (comment ..) form and eval those to confirm the behavior. Then I might turn some of those forms into actual tests to catch regressions later. I run tests via the REPL too.
But I'm really obsessive about eval'ing code as I write, more so than even saving the file. I'll often have 2 or 3 open files with unsaved changes while I'm working on code, only saving them all when I'm at a "done" point for some logic. At that point I could run the test suite from the command-line if I want.
I never type into the REPL. I always write code in the editor and send it to the REPL. And I'm currently in the Sean Corfield camp of not using a reloaded workflow. But currenly I'm building libraries or CLI tools, so a reloaded workflow isn't necessary for me.
There are quite a few videos out there showing various folks' REPL-based workflows (including one or two by me) which you might find interesting... search for repl driven development.
We have ~20 apps in a monorepo with a lot of shared code (150k lines total) and don't need reloaded for that either 🙂
I start with individual forms in a regular namespace file, possibly let-binding the inputs I expect my function to have, evaluating as the form grows. So not "line by line" but often "sexp by sexp".
This recent talk has some REPL driven development about 10 minutes in: https://www.youtube.com/watch?v=i_dUvhEIGBQ
It's often very different in ClojureScript because the REPL is different. There, it's more likely that I'll finish a function before trying it, though I try to resist that delaying urge.
> Do you repl line by line when writing a function, or only repl eval once the full function has been written? > Evaluate as small expressions as you need to. You want relevant information from REPL invocations. If you're getting errors you can't explain, take smaller steps. If you can predict each results, take larger steps. Over time, you'll find an effective middle ground!
> Do you repl line by line when writing a function Yes, sometimes I repl eval word by word even 😛