Skip to content

Instantly share code, notes, and snippets.

View jcomellas's full-sized avatar

Juan Jose Comellas jcomellas

  • Buenos Aires, Argentina
View GitHub Profile
@jcomellas
jcomellas / common_log.erl
Last active December 9, 2015 21:48
Cowboy onresponse callback for common access log
%% Use the following function as onresponse callback when creating the
%% cowboy listener environment to log HTTP requests in common log format.
-define(HDR_X_FORWARDED_FOR, "x-forwarded-for").
-spec common_http_log(cowboy:status(), cowboy:headers(), iodata(), cowboy_req:req()) -> cowboy_req:req().
common_http_log(Status, _Headers, Body, Req0) ->
%% 127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326
[Method, Version, Path, Qs0] = cowboy_req:get([method, version, path, qs], Req0),
{{IpAddr, _Port}, Req1} = cowboy_req:peer(Req0),
Addr = inet:ntoa(IpAddr),
@jcomellas
jcomellas / gist:732cb5ddacb1ccdfa349
Created May 21, 2015 11:44
Elixir tuple map function
# Map over a tuple in normal order
def tuple_map1(tuple, function), do:
tuple_map(tuple, function, 0, [])
def tuple_map1(tuple, function, index, acc) when index < tuple_size(tuple) do
item = function.(elem(tuple, index))
tuple_map1(tuple, function, index + 1, [item | acc])
end
def tuple_map1(tuple, function, index, acc) do
Enum.reverse(acc)
@jcomellas
jcomellas / dynamic_typespec.exs
Created May 15, 2015 01:13
Elixir macro that dynamically adds type specs
# Given a module defined in this way:
defmodule HL7.Composite do
# [...]
defmodule EI do
use HL7.Composite.Def
composite do
component :id, type: :binary, default: ""
component :namespace_id, type: :binary, default: ""
component :universal_id, type: :binary, default: ""
@jcomellas
jcomellas / rebar.config.script
Created June 27, 2013 20:28
rebar script to dynamically set the CloudI path in rebar.config
%% Script that add the CloudI Erlang applications from an installed instance to
%% rebar's lib_dirs configuration setting (code path).
%% If the 'CLOUDI_DIR' environment variable is set, then we add its "lib"
%% subdirectory to the code path. If not, we check for the CloudI directory in the
%% /usr/local/lib path.
LibDir = case os:getenv("CLOUDI_DIR") of
Dir when Dir =:= false; Dir =:= "" ->
%% Look for the CloudI directory in /usr/local/lib
case os:type() of
{unix, _Osname} ->
@jcomellas
jcomellas / lists_join.erl
Created March 4, 2013 18:46
Join a list of elements in Erlang, adding a separator between them
%% @doc Join a a list of elements adding a separator between
%% each of them.
-spec join(iolist(), Sep :: term()) -> iolist().
join([Head | Tail], Sep) ->
join_list_sep(Tail, Sep, [Head]);
join([], _Sep) ->
[].
-spec join_list_sep(iolist(), Sep :: term(), Acc :: iolist()) -> iolist().
join_list_sep([Head | Tail], Sep, Acc) ->
@jcomellas
jcomellas / netutil.erl
Created February 22, 2011 23:17
Functions to obtain IP addresses from subnets in Erlang.
-module(netutil).
-author('Juan Jose Comellas <juanjo@comellas.org>').
%% API
-export([get_local_ip_from_subnet/1, get_ip_from_subnet/2,
cidr_network/1, cidr_netmask/1]).
%% @type ipv4() = {integer(), integer(), integer(), integer()}