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.
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.
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 sum path 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.
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?”
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.
defmodule MyRouter do
import Plug.Conn
use Plug.Router
# import TemplateFunctions
@alpha "alpha"
@beta "beta"
plug :match
plug :dispatch
def theta(a, b) do
a + b
end
get "/hello" do
send_resp(conn, 200, "world")
end
get "/hello/:name" do
send_resp(conn, 200, "hello #{name}")
end
get "/sum" do
conn = fetch_query_params(conn)
%{ @alpha => alpha, @beta => beta } = conn.params
# send_resp(conn, 200, " sum of #{alpha} and #{beta} is " ++ sum(alpha,beta))
gamma = theta(alpha, beta)
send_resp(conn, 200, " sum of #{alpha} and #{beta}")
end
get "/hello/*glob" do
send_resp(conn, 200, "route after /hello: #{inspect glob}")
end
match _ do
send_resp(conn, 404, "oops")
end
def theta2(a, b) do
a + b
end
end
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
** (exit) an exception was raised:
** (ArithmeticError) bad argument in arithmetic expression
myfuncs/funcs_router.ex:14: MyRouter.theta/2
myfuncs/funcs_router.ex:29: anonymous fn/1 in MyRouter.do_match/4
with the full output below. There are other bugs here but what is the main reason for the failure to recognize the function ?
vonH@ac02:~/DevProjects/learnphoenix/tsys_functions$ iex -S mix
Erlang/OTP 19 [erts-8.3] [source-d5c06c6] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:
false]
Interactive Elixir (1.4.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c "myfuncs/funcs_router.ex"
warning: variable "gamma" is unused
myfuncs/funcs_router.ex:29
[MyRouter]
iex(2)> {:ok, _} = Plug.Adapters.Cowboy.http MyRouter, []
{:ok, #PID<0.185.0>}
iex(3)>
20:03:53.003 [error] #PID<0.289.0> running MyRouter terminated
Server: localhost:2001 (http)
Request: GET /sum?alpha=4&beta=6
** (exit) an exception was raised:
** (ArithmeticError) bad argument in arithmetic expression
myfuncs/funcs_router.ex:14: MyRouter.theta/2
myfuncs/funcs_router.ex:29: anonymous fn/1 in MyRouter.do_match/4
myfuncs/funcs_router.ex:1: MyRouter.plug_builder_call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
There are a few more questions.
What code do I need to display all the parameters whether they match sum or not, eg ?alpha=6&beta=7&rho=8?pi=9 etc.
When I add a parameter in addition alpha and beta the sum block works fine. If I want to match exactly 2 parameters how would I right the `get “/sum” matching code.
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/1 I think is what it is if you want a quick-fail one, otherwise Integer.parse/1 that tells you if successful or not.
What function can format the whole of the conn.params variable for send_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.
Depends on what kind of response you want to send. HTML? JSON (Poison makes that trivial then)? Etc…?
You could map over them.
For a sum function instead of specific arguments I’d probably pass them in as an array, like: http://localhost:4000/sum?input[]=10&input[]=12&input[]=20
Which would output whatever 10+12+20 is or so (via Enum.reduce over the list). You really only need specific named args if order or names matter, which for a sum it would not.
I am thinking along the lines of PHP’s print_r or function, dpr if you are familiar with Drupal. It is more like PHP’s explode although the type may be more complicated. Do you know of some samples?
I’ve got an awful let to learn.
This project is my very first Elixir project and I am learning Elixir as I go along. I am trying not to make the mistake I made years ago with Ruby on Rails, when I tried learn RoR without knowing Ruby well enough first and abandoned it, but it Elixir looks much simpler and it may be the docs.
Well inspect/1 converts anything to something as a readable string, and IO.inspect/1 does that while also printing it to the standard out, you might want inspect/1 if you are just dumping it back though?
If I have to handle an unknown number of query parameters how would I handle that?
Say the query parameters are ?a=1&b=2?c=3?d=“apple”?e=5 etc?
What I would like to do is to list each parameter and its type then sum the numeric ones,
so in this instance the output would
| param | value | type |
|-------|-------|--------|
| a | 4 | number |
| b | 2 | number |
| c | 3 | number |
| d | apple | string |
| e | 5 | number |
| sum | 11 | number |
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...
Enum.reduce is being passed an anonymous function with two function clauses - each function head matching a different pattern. (i,acc) when is_int(i) -> i+ acc
is the first function clause - when starts the guard clause of the pattern, checking the type of i (with the built-in-function (BIF) is_int/1) - so if i is an integer, this function clause will evaluate to i + acc. (_,acc) -> acc
is the next function clause where the head gets a chance to match if the previous head matches failed - the _ (underscore) simply indicates that you don’t care about that value - this function clause will simply evaluate to the acc value without further modification.