Search code examples
erlangaliasinets

Erlang: How to make an alias from 'erl_script_alias' to root url?


Here is the inets configuration file that I have:

[{port, 443}, 
 {server_name, "example.com"},
 {server_root, "./root/"},
 {document_root, "./htdocs/"},
 {socket_type, {essl, [{certfile, "/etc/letsencrypt/live/example.com/cert.pem"}, {keyfile, "/etc/letsencrypt/live/example.com/privkey.pem"}]}},
 {directory_index, ["index.html"]},
 {erl_script_alias, {"/erl",[functions]}},
 {erl_script_nocache, true},
 {script_alias, {"/cgi-bin/", "/home/example/site/cgi-bin/"}},
 {script_nocache,true}
].

With this configuration file I can access:

https://example.com/cgi-bin/something.cgi

and

https://example.com/erl/functions/function

I already know that changing {script_alias, {"/cgi-bin/"... to {script_alias, {"/"... gives me access to cgi script from https://example.com/, but how can I get the same behavior with erl_script_alias? Ex.: accessing https://example.com/ and getting access to /erl/functions/function?


Solution

  • Answering my own question:

    The solution that I found was to create a module to deal with "GET" requests of "/".

    -module(mod_index).
    -export([do/1]).
    -include_lib("inets/include/httpd.hrl").
    
    do(ModData) ->
        root_url(ModData#mod.method, ModData#mod.request_uri, ModData#mod.data).
    
    root_url("GET", "/", _) ->
        {proceed, [{response, {200,"Content-Type: text/html\r\n\r\nSomething"}}]};
    
    root_url(_, _, OldData) ->
        {proceed, OldData}.
    
    

    And adding the module into the configuration file with the default modules:

    [{port, 443}, 
     {server_name, "example.com"},
     {server_root, "./root/"},
     {document_root, "./htdocs/"},
     {socket_type, {essl, [{certfile, "/etc/letsencrypt/live/example.com/cert.pem"}, {keyfile, "/etc/letsencrypt/live/example.com/privkey.pem"}]}},
     {directory_index, ["index.html"]},
     {erl_script_alias, {"/erl",[functions]}},
     {erl_script_nocache, true},
     {script_alias, {"/cgi-bin/", "/home/example/site/cgi-bin/"}},
     {script_nocache,true},
     {modules, [mod_index, mod_alias, mod_auth, mod_esi, mod_actions, mod_cgi, mod_dir, mod_get, mod_head, mod_log, mod_disk_log]}
    ].
    

    As the documentation says, the modules' order matters.