usr local bin escript response HTTP 200 OK Server Erlang-powered Linux

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/local/bin/escript
response() -> <<
"HTTP/1.1 200 OK\r\n"
"Server: Erlang-powered (Linux)\r\n"
"Connection: close\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"Hello!">>.
server_loop(LSock) ->
{ok, Sock} = gen_tcp:accept(LSock),
spawn(fun() ->
{ok, Request} = gen_tcp:recv(Sock, 0),
ok = gen_tcp:send(Sock, response()),
gen_tcp:close(Sock)
end),
server_loop(LSock).
main(Args) ->
{ok, LSock} = gen_tcp:listen(8080, [binary, {active, false}]),
server_loop(LSock).