vonH
I want to try my hand at my first Elixir website.
Basically I want to use the functions in an Elixir website to extend a compiled Pascal application. I plan to embed a language like Lua, Python or Lisp in the app later on, but when it comes to using a web app in some kind of psuedo REST application Elixir will do fine.
For instance if I want to write an arbitrary function such as sum(a+b) I want to call the Elixir website with the URL http://localhost:8000/sum?a=4&b=5 and the website would return 9 in plain text.
Would a plain simple Elixir script running a webserver accomplish that I should I start with something like Phoenix? I wouldn’t mind something very basic which doesn’t involve a framework, but if the framework would be just as helpful in learning Elixir’s basics I wouldn’t mind.
With regards to Phoenix I think the docs are a good start, but outside Phoenix what libraries or routines should I get started with.
Thanks!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 31 Posts
OvermindDL1
Basically about every webserver in Elixir is built on Erlang’s Cowboy, the most common one is Plug, which gives a simplified interface on Cowboy, where Plug just gives you a set of simple composable functions to build a pipeline. Phoenix is just mainly more functions built on Plug to make things even easier, like templating, as well as it has added two major things, one being a dead-simple and fast Websocket support, and the other being a fantastic and simple PubSub, all of which is optional and composable like any normal Plug. You can strip down Phoenix as much as you want but even when well loaded down it remains blazing fast.
If you are just wanting to call a url like you’ve shown and you want it to return just the text answer, no html or anything, raw Plug is fine, but if you ever intend to ever do something more than that, html, websockets, anything, you should just start with Phoenix now especially as its generators help encourage good and proper coding conventions for Elixir.
vonH
Do you know of any small online projects that would help me in the non-Phoenix path?
OvermindDL1
I don’t know of any projects off the top of my head (though I know I’ve seen a few), but the Plug Docs themselves contain examples, like the Plug.Router is about the most simple router you’d have, like you could hook up a
sumpath there, or build one dynamically by writing your own router as a plug or whatever.A forewarning, you will end up re-writing a lot of Phoenix stuff since Phoenix is just a fleshing out on Plug (made by the same devs as well), but if you are doing this to learn the back-end stuff, not a bad way to go.
jeramyRR
Not to be a debby downer here, but Phoenix is so fast to setup that you’ve probably spent more time writing the question than it would take to get an endpoint up and running in Phoenix. It’s amazing how quickly you can get something up. It isn’t very resource intensive either so the argument really comes down to, “What do you want to implement yourself?”
OvermindDL1
Exactly this.
xlphs
If you are trying to cut down the number of dependencies, then cowboy and plug will do, here is my tic tac toe game for starters. And if you are interested, you can easily write a simple http server with help of
:erlang.decode_packet/3which parses HTTP headers for you.vonH
I have made some progress so far, based on the Plug documentation and this stackoverflow question - http://stackoverflow.com/questions/25370007/how-to-pass-multiple-parameters-in-a-url-to-plug
When I run it intending to utilize the sum function, which I renamed theta because I thought it conflicted with some function in the libraries I get the error
with the full output below. There are other bugs here but what is the main reason for the failure to recognize the function ?
There are a few more questions.
What code do I need to display all the parameters whether they match
sumor not, eg ?alpha=6&beta=7&rho=8?pi=9 etc.When I add a parameter in addition alpha and beta the
sumblock works fine. If I want to match exactly 2 parameters how would I right the `get “/sum” matching code.OvermindDL1
Well you are passing in strings to the theta function, then trying to add them, hence the
(ArithmeticError) bad argument in arithmetic expression, so you should convert the param strings into integers first.String.to_integer/1I think is what it is if you want a quick-fail one, otherwiseInteger.parse/1that tells you if successful or not.OvermindDL1
That would just be
conn.params.Are you wanting to fail if they pass in extra arguments as well?
vonH
What function can format the whole of the
conn.paramsvariable forsend_resp?No in the case of a function like sum which could be adding many numbers tthat wouldn’t be necessary, it would simply be enough to check that all the parameters were numeric.
But in the case of wanting a fixed number of parameters what would be the best way, or fixed with a set of names? I have found an example in this article - http://nicolas-bettenburg.com/articles/scrubbing-get-params-with-phoenix/ - but I am not quite sure how dependent it is on Phoenix.