Skip to content

Instantly share code, notes, and snippets.

@nicolasff
Created December 19, 2008 12:55
Show Gist options
  • Save nicolasff/38000 to your computer and use it in GitHub Desktop.
Save nicolasff/38000 to your computer and use it in GitHub Desktop.
-module(server).
-export([start/0]).
% 1 - Download and install Mochiweb, make sure it is in the ebin path (-pa).
% 2 - Run with server:start()
% 3 - Point you browser to http://127.0.0.1:12345/something
% 4 - A primitive benchmark: "ab -c 1000 -n 10000 http://127.0.0.1:12345/test"
start() ->
Options = [
{max, 1000000},
{name, ?MODULE},
{port, 12345},
{loop, fun handle_query/1}],
mochiweb_http:start(Options).
handle_query(Req) ->
"/" ++ Path = Req:get(path),
Req:respond({200,
[{"Content-Type", "text-plain"}],
"Hello, you requested [" ++ Path ++ "]\r\n"}),
% Or chunked, for Comet streaming:
%
% Response = Req:ok({"text/plain; charset=utf-8", [], chunked}),
% Response:write_chunk("Hello, you requested [" ++ Path ++ "]\r\n"),
% Response:write_chunk([]),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment