<commit_before>-module(iorio_app).

-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

-include("include/iorio.hrl").
-include_lib("permiso/include/permiso.hrl").

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
    % TODO: see where to start it
    file_handle_cache:start_link(),
    % TODO: check here that secret is binary and algorigthm is a valid one
    {ok, ApiSecret} = env(iorio, auth_secret),
    {ok, ApiAlgorithm} = env(iorio, auth_algorithm),

    AdminUsername = env(iorio, admin_username, "admin"),
    {ok, AdminPassword} = env(iorio, admin_password),

    AnonUsername = env(iorio, anon_username, "anonymous"),
    % default password since login in as anonymous is not that useful
    AnonPassword = env(iorio, anon_password, <<"<PASSWORD>">>),

    SessionDurationSecs = env(iorio, session_duration_secs, 3600),

    N = env(iorio, req_n, 3),
    W = env(iorio, req_w, 3),
    Timeout = env(iorio, req_timeout, 5000),

    AuthMod = env(iorio, auth_mod, permiso_rcore),
    AuthModOpts0 = env(iorio, auth_mod_opts, []),

    OnUserCreated = fun (Mod, State, #user{username=Username}) ->
                            ioriol_access:maybe_grant_bucket_ownership(Mod, State, Username),
                            Mod:user_join(State, Username, ?USER_GROUP)
                    end,
    AuthModOpts = [{user_created_cb, OnUserCreated}|AuthModOpts0],
    {ok, AccessLogic} = ioriol_access:new([{auth_mod, AuthMod},
                                           {auth_mod_opts, AuthModOpts},
                                           {secret, ApiSecret}]),

    CreateGroupsResult = create_groups(AccessLogic),
    lager:info("create groups ~p", [CreateGroupsResult]),

    GrantAdminUsers = fun (Username) ->
                              Res = ioriol_access:grant(AccessLogic, AdminUsername,
                                                        ?PERM_MAGIC_BUCKET, any,
                                                        ?PERM_ADMIN_USERS),
                              lager:info("assign admin users to ~p: ~p",
                                         [Username, Res]),
                              ioriol_access:maybe_grant_bucket_ownership(AccessLogic, AdminUsername)
                      end,
    create_user(AccessLogic, AdminUsername, AdminPassword, [?ADMIN_GROUP],
                GrantAdminUsers),
    create_user(AccessLogic, AnonUsername, AnonPassword, [],
                fun (_) -> ok end),

    setup_initial_permissions(AccessLogic, AdminUsername),

    BaseDispatchRoutes = [
               {"/listen", bullet_handler, [{handler, iorio_listen_handler},
                                            {access, AccessLogic}]},
               {"/streams/:bucket", iorio_rest_list, [{access, AccessLogic}]},
               {"/streams/:bucket/:stream", iorio_rest_stream,
                [{access, AccessLogic}, {n, N}, {w, W}, {timeout, Timeout}]},
               {"/buckets/", iorio_rest_list, [{access, AccessLogic}]},
               {"/access/:bucket/", iorio_rest_access, [{access, AccessLogic}]},
               {"/access/:bucket/:stream", iorio_rest_access, [{access, AccessLogic}]},

               {"/sessions", iorio_rest_session,
                [{access, AccessLogic}, {algorithm, ApiAlgorithm},
                 {session_duration_secs, SessionDurationSecs}]},
               {"/users/", iorio_rest_user, [{access, AccessLogic}]},
               {"/ping", iorio_rest_ping, []}
    ],

    UserDispatchRoutes = env(iorio, api_handlers,
                             [{"/ui/[...]", iorio_cowboy_static,
                               {priv_dir, iorio, "assets",
                                [{mimetypes, cow_mimetypes, all}]}}]),
    lager:info("configuring routes with following user provided routes: ~p",
               UserDispatchRoutes),
    DispatchRoutes = BaseDispatchRoutes ++ UserDispatchRoutes,

    Dispatch = cowboy_router:compile([{'_', DispatchRoutes}]),

    ApiPort = env(iorio, http_port, 8080),
    ApiAcceptors = env(iorio, http_acceptors, 100),
    {ok, _} = cowboy:start_http(http, ApiAcceptors, [{port, ApiPort}],
                                [{env, [{dispatch, Dispatch}]}]),

    SecureEnabled = env(iorio, https_enabled, false),
    SecureApiPort = env(iorio, https_port, 8443),

    if
        SecureEnabled ->
            lager:info("https api enabled, starting"),
            SSLCACertPath = env(iorio, https_cacert, notset),
            {ok, SSLCertPath} = env(iorio, https_cert),
            {ok, SSLKeyPath} = env(iorio, https_key),

            BaseSSLOpts = [{port, SecureApiPort}, {certfile, SSLCertPath},
                           {keyfile, SSLKeyPath}],

            SSLOpts = if SSLCACertPath == notset -> BaseSSLOpts;
                         true -> [{cacertfile, SSLCACertPath}|BaseSSLOpts]
                      end,

            {ok, _} = cowboy:start_https(https, ApiAcceptors, SSLOpts,
                                         [{env, [{dispatch, Dispatch}]}]);
        true ->
            lager:info("https api disabled"),
            ok<commit_msg>honor http.enabled, closes #46<commit_after>-module(iorio_app).

-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

-include("include/iorio.hrl").
-include_lib("permiso/include/permiso.hrl").

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
    % TODO: see where to start it
    file_handle_cache:start_link(),
    % TODO: check here that secret is binary and algorigthm is a valid one
    {ok, ApiSecret} = env(iorio, auth_secret),
    {ok, ApiAlgorithm} = env(iorio, auth_algorithm),

    AdminUsername = env(iorio, admin_username, "admin"),
    {ok, AdminPassword} = env(iorio, admin_password),

    AnonUsername = env(iorio, anon_username, "anonymous"),
    % default password since login in as anonymous is not that useful
    AnonPassword = env(iorio, anon_password, <<"<PASSWORD>">>),

    SessionDurationSecs = env(iorio, session_duration_secs, 3600),

    N = env(iorio, req_n, 3),
    W = env(iorio, req_w, 3),
    Timeout = env(iorio, req_timeout, 5000),

    AuthMod = env(iorio, auth_mod, permiso_rcore),
    AuthModOpts0 = env(iorio, auth_mod_opts, []),

    OnUserCreated = fun (Mod, State, #user{username=Username}) ->
                            ioriol_access:maybe_grant_bucket_ownership(Mod, State, Username),
                            Mod:user_join(State, Username, ?USER_GROUP)
                    end,
    AuthModOpts = [{user_created_cb, OnUserCreated}|AuthModOpts0],
    {ok, AccessLogic} = ioriol_access:new([{auth_mod, AuthMod},
                                           {auth_mod_opts, AuthModOpts},
                                           {secret, ApiSecret}]),

    CreateGroupsResult = create_groups(AccessLogic),
    lager:info("create groups ~p", [CreateGroupsResult]),

    GrantAdminUsers = fun (Username) ->
                              Res = ioriol_access:grant(AccessLogic, AdminUsername,
                                                        ?PERM_MAGIC_BUCKET, any,
                                                        ?PERM_ADMIN_USERS),
                              lager:info("assign admin users to ~p: ~p",
                                         [Username, Res]),
                              ioriol_access:maybe_grant_bucket_ownership(AccessLogic, AdminUsername)
                      end,
    create_user(AccessLogic, AdminUsername, AdminPassword, [?ADMIN_GROUP],
                GrantAdminUsers),
    create_user(AccessLogic, AnonUsername, AnonPassword, [],
                fun (_) -> ok end),

    setup_initial_permissions(AccessLogic, AdminUsername),

    BaseDispatchRoutes = [
               {"/listen", bullet_handler, [{handler, iorio_listen_handler},
                                            {access, AccessLogic}]},
               {"/streams/:bucket", iorio_rest_list, [{access, AccessLogic}]},
               {"/streams/:bucket/:stream", iorio_rest_stream,
                [{access, AccessLogic}, {n, N}, {w, W}, {timeout, Timeout}]},
               {"/buckets/", iorio_rest_list, [{access, AccessLogic}]},
               {"/access/:bucket/", iorio_rest_access, [{access, AccessLogic}]},
               {"/access/:bucket/:stream", iorio_rest_access, [{access, AccessLogic}]},

               {"/sessions", iorio_rest_session,
                [{access, AccessLogic}, {algorithm, ApiAlgorithm},
                 {session_duration_secs, SessionDurationSecs}]},
               {"/users/", iorio_rest_user, [{access, AccessLogic}]},
               {"/ping", iorio_rest_ping, []}
    ],

    UserDispatchRoutes = env(iorio, api_handlers,
                             [{"/ui/[...]", iorio_cowboy_static,
                               {priv_dir, iorio, "assets",
                                [{mimetypes, cow_mimetypes, all}]}}]),
    lager:info("configuring routes with following user provided routes: ~p",
               UserDispatchRoutes),
    DispatchRoutes = BaseDispatchRoutes ++ UserDispatchRoutes,

    Dispatch = cowboy_router:compile([{'_', DispatchRoutes}]),

    HttpEnabled = env(iorio, http_enabled, true),
    ApiPort = env(iorio, http_port, 8080),
    ApiAcceptors = env(iorio, http_acceptors, 100),
    if HttpEnabled ->
            lager:info("http api enabled, starting"),
           {ok, _} = cowboy:start_http(http, ApiAcceptors, [{port, ApiPort}],
                                       [{env, [{dispatch, Dispatch}]}]);
       true ->
           lager:info("http api disabled"),
           ok
    end,

    SecureEnabled = env(iorio, https_enabled, false),
    SecureApiPort = env(iorio, https_port, 8443),

    if
        SecureEnabled ->
            lager:info("https api enabled, starting"),
            SSLCACertPath = env(iorio, https_cacert, notset),
            {ok, SSLCertPath} = env(iorio, https_cert),
            {ok, SSLKeyPath} = env(iorio, https_key),

            BaseSSLOpts = [{port, SecureApiPort}, {certfile, SSLCertPath},
                           {keyfile, SSLKeyPath}],

            SSLOpts = if SSLCACertPath == notset -> BaseSSLOpts;
                         true -> [{cacertfile, SSLCACertPath}|BaseSSLOpts]
                      end,

            {ok, _} = cowboy:start_https(https, ApiAcceptors, SSLOpts,
                                         [{env, [{dispatch, Dispatch}]}]);
        true ->
            lager:info("https api disabled"),
            ok
{'fraction_non_alphanumeric': 0.1462798315395414, 'fraction_numerical': 0.004866635470285447, 'mean_word_length': 2.1699792346484723, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 33, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4180814', 'n_tokens_mistral': 3090, 'n_tokens_neox': 2989, 'n_words': 608}