vonH

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

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

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

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

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.

Last Post!

samanta

samanta

http://hotpyn.com/2018/02/04/php-elixir/

Hello, I wrote a blog post showing how to build a simple website using Plug. Github demo is available below.

https://github.com/hotpyn/simple-site

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

We're in Beta

About us Mission Statement