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.
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/3 which parses HTTP headers for you.
peerreynders
Have a look at Integer.parse/2. It returns a tuple and that is what case is matching on.
{i,""} -> i
So if v happens to be a string that only contains an integer then the result from Integer.parse/2 will match {i,""} - i being the integer that was parsed - the value that this clause evaluates to.
_ -> v
this clause is the “otherwise” or “default” clause - it simply evaluates to the value v that Integer.parse/2 was trying to parse but turned out to be something else other than an “integer string”.
You have to realize that @OvermindDL1 worked in Erlang before Elixir - in Erlang you live and breathe pattern matching, so he will justifiably leverage pattern matching in Elixir to the full extent possible. Coming from an imperative background this may look a bit strange at first but more often than not, it is a very concise and declarative way of tackling (data) problems.







