vonH
Simple Elixir web page - use Phoenix or not?
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!
Marked As Solved
OvermindDL1
So given those param’s and values (remember that the web browser only gives you strings, so you do not know if they can be parsed as numbers yet, you basically get them from the browser like %{"a" => "4", "b" => "2", "c" => "3", "d" => "apple", "e" => "5", "sum" => "11"}) and they are in the params argument to your routed function, then could do something like this:
# parse integers out if possible, else keep as strings
params =
params
|> Enum.map(fn {k, v} ->
v =
case Integer.parse(v) do
{i, ""} -> i
_ -> v
end
{k, v}
end)
# Add up the numbers:
summed =
Enum.reduce(params, 0, fn
({_, i}, acc) when is_int(i) -> i + acc
(_, acc) -> acc
end)
# And you can format the output however you want to...
Also Liked
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
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.
peerreynders
There was recently a whole topic around the bind thing.
The other thing about functional programming is thinking of code as expressions rather than statements.
If you look carefully at the unless example you’ll notice that the shell actually shows the evaluated value as nil. So while unless looks like a statement, it’s just a macro that either evaluates to the result of the supplied body or (implicitly) to nil, i.e. there always is an invisible alternative because an expression has to evaluate to something - even if that something happens to be nil.







