I'm trying to build a simple web server based on Erlang, so far I'm able to start a server with below code. tutorial ref
-module(helloworld).
-export([
main/1,
run_server/0,
start/0,
service/3,
]).
main(_) ->
start(),
receive
stop -> ok
end.
run_server() ->
ok = inets:start(),
{ok, _} = inets:start(httpd, [
{modules, [
mod_alias,
mod_auth,
mod_esi,
mod_actions,
mod_cgi,
mod_dir,
mod_get,
mod_head,
mod_log,
mod_disk_log
]},
{port, 8000},
{server_name,"helloworld"},
{server_root,"/tmp"},
{document_root,"."},
{erl_script_alias, {"/erl", [helloworld]}},
{error_log, "error.log"},
{security_log, "security.log"},
{transfer_log, "transfer.log"},
{mime_types,[
{"html","text/html"}, {"css","text/css"}, {"js","application/x-javascript"} ]}
]).
start() -> run_server().
service(SessionID, _Env, _Input) -> mod_esi:deliver(SessionID, [
"Content-Type: text/html\r\n\r\n", "<html><body>Hello, World!</body></html>" ]).
I'm trying to get the response but getting the permission error.
You don't have permission to access /erl/hello_world:servie on this server.
I'm aiming to build Erlang based server which can read post request and store the post data in MYSQL.
If anyone can help me with instructions or with some code to start the Erlang server for reading POST request that will be so helpful.
Thanks
I used rebar3 to create an app:
Then I specified mysql-otp as a dependency so that I could use mysql. I basically ignored the OTP app, and I added some source code to start an inets server. See here.
Here's the module I used to start the inets httpd server, handle the request, and insert the post data into a mysql db:
my.erl:I put that code in the
srcdirectory of my app, which was namedmyserver. Here is my directory structure:Here is the
server.conffile:Then, to run the app I did:
I looked at that output to get the server's port:
55804, which I used to send a post request with curl:Then I stopped the inets httpd server:
Then I checked the mysql db for a new entry:
Success!
Here's my
rebar.configfile: