<commit_before>%% @author author <<EMAIL>>
%% @copyright YYYY author.

%% @doc Web server for playdar.

-module(playdar_web).
-author('author <<EMAIL>>').

-export([start/1, stop/0, loop/2]).

%% External API

start(Options) ->
    {DocRoot, Options1} = get_option(docroot, Options),
    {Port, Options2} = get_option(port, Options1),
    {Ip, Options3} = get_option(ip, Options2),
    Loop = fun (Req) -> ?MODULE:loop(Req, DocRoot) end,
    MochiOpts = [   {max, 20},
                    {port, 60210},
                    {ip, Ip},
                    %{docroot, DocRoot},
                    {name, ?MODULE}, 
                    {loop, Loop}
                    | Options3
                ],
    %io:format("~p~n",[MochiOpts]),
    mochiweb_http:start(MochiOpts).
                

stop() ->
    mochiweb_http:stop(?MODULE).

loop(Req, DocRoot) ->
    io:format("GET ~p~n", [Req:get(raw_path)]),
    "/" ++ Path = Req:get(path),
    
    case Path of
        "" -> 
            Req:ok({"text/html", "<h1>Playdar</h1>Playdar-erlang is running"});
        
        % serving a file that was found by a query, based on SID:
        "sid/" ++ SidL ->
            Sid = list_to_binary(SidL),
            case resolver:sid2pid(Sid) of
                undefined ->
                    Req:not_found();
                Qpid ->
                    Ref = make_ref(),
                    A = qry:result(Qpid, Sid),
                    stream_reader:start_link(A, self(), Ref),
                    stream_result(Req, Ref)
            end;
        
        % else hand off to module:
        _ -> 
            Parts = string:tokens(Req:get(path),"/"),
            Mod = hd(Parts),
            Modules = ["api"], % modules whitelist (TODO, use loaded plugins)
            case lists:foldl( fun(X,A) -> case X of Mod -> A+1 ; _ -> A end end,
                              0, Modules) of
                0 ->
                    Req:not_found();
                _ ->
                    Module = list_to_atom("playdar_http_" ++ Mod),
                    % TODO filter/verify valid module names
                    Module:http_req(Req)
           end
    end.

<commit_msg>api support built in<commit_after>%% @author author <<EMAIL>>
%% @copyright YYYY author.

%% @doc Web server for playdar.

-module(playdar_web).
-author('author <<EMAIL>>').

-export([start/1, stop/0, loop/2]).

%% External API

start(Options) ->
    {DocRoot, Options1} = get_option(docroot, Options),
    {Port, Options2} = get_option(port, Options1),
    {Ip, Options3} = get_option(ip, Options2),
    Loop = fun (Req) -> ?MODULE:loop(Req, DocRoot) end,
    MochiOpts = [   {max, 20},
                    {port, 60210},
                    {ip, Ip},
                    %{docroot, DocRoot},
                    {name, ?MODULE}, 
                    {loop, Loop}
                    | Options3
                ],
    %io:format("~p~n",[MochiOpts]),
    mochiweb_http:start(MochiOpts).
                

stop() ->
    mochiweb_http:stop(?MODULE).

loop(Req, DocRoot) ->
    io:format("GET ~p~n", [Req:get(raw_path)]),
    "/" ++ Path = Req:get(path),
    
    case Path of
        "" -> 
            Req:ok({"text/html", "<h1>Playdar</h1>Playdar-erlang is running"});
        
        % serving a file that was found by a query, based on SID:
        "sid/" ++ SidL ->
            Sid = list_to_binary(SidL),
            case resolver:sid2pid(Sid) of
                undefined ->
                    Req:not_found();
                Qpid ->
                    Ref = make_ref(),
                    A = qry:result(Qpid, Sid),
                    stream_reader:start_link(A, self(), Ref),
                    stream_result(Req, Ref)
            end;

        % hardcoded support for api/
        "api/" ++ _ ->
            playdar_http_api:http_req(Req);
        
        % else hand off to module:
        _ -> 
            Parts = string:tokens(Req:get(path),"/"),
            Mod = hd(Parts),
            Modules = [], % modules whitelist (TODO, use loaded plugins)
            case lists:foldl( fun(X,A) -> case X of Mod -> A+1 ; _ -> A end end,
                              0, Modules) of
                0 ->
                    Req:not_found();
                _ ->
                    Module = list_to_atom("playdar_http_" ++ Mod),
                    % TODO filter/verify valid module names
                    Module:http_req(Req)
           end
    end.


{'fraction_non_alphanumeric': 0.1402755816580077, 'fraction_numerical': 0.009939010616670432, 'mean_word_length': 1.432967032967033, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1433995', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1344, 'n_words': 304}