<commit_before>#!/bin/sh

# * $1 == 'mongo' launch mongo shell with current config DB name
# * $1 == 'mongo-edit' launch mongo-edit current config DB name or
#         env $NODEJS_MONGO_DB may change default database name
# * any param "$1" will redirect node.js' output i.e:
#   1>>log/app_back_stdout.txt 2>>log/app_back_stderr.txt
# * if env $NODEJS_CONFIG is exported it is used and no hardcoded file is read
# * if env $NODEJS_BIN is exported then this binary name will be used instead of `node`
# * env $NODEJS_RELOAD app doesn't call `call_done_handlers()` when shuts down
#   i.e. doesn't stop `mongodb`

cd "${0%/*}" 2>/dev/null
set -e

# though git for windows is preferred a bundle of cygwin executables can be here
PATH=.:bin:$PATH

trap 'echo "
Unexpected Script Error! Use /bin/sh -x $0 to trace it.
"
set +e

trap "" 0
exit 0
' 0

normal_exit(){
    echo "
Normal Exit${1:- (backend is running)}
"
    set +e
    trap '' 0
    exit 0
}

trap 'normal_exit' HUP TERM INT

if [ "$NODEJS_CONFIG" ]
then
    echo '
^ config is already exported in "$NODEJS_CONFIG"'
else
    echo '
^ reading config in "$NODEJS_CONFIG" from file "./config/cfg_default.js"'
    NODEJS_CONFIG=`sed '' <./config/cfg_default.js`
    echo '^ exporting it for childs'
    export NODEJS_CONFIG
fi

case "$1" in
    mongo)
    sh app_modules/supromongod/etc/mongo-shell.sh
    trap '' 0
    exit 0
    ;;
    mongo-edit)
    # env $NODEJS_MONGO_DB may change default database name
    sh app_modules/supromongod/etc/mongo-edit.sh
    trap '' 0
    exit 0
    ;;
esac

# JS config sample to parse:
#    backend:{
#        file: './app_main/app_back.js',
#        job_port: 3007,// app/api
# here>> ctl_port: 3008,// controlling
#        ctl_on_done: null,// set app module handlers for ctl close/app exit
#        init_timeout: 123
#    }
BACKEND_PORT=${NODEJS_CONFIG##*ctl_port:}
BACKEND_PORT=${BACKEND_PORT## }
BACKEND_PORT=${BACKEND_PORT%%,*}

A=${NODEJS_CONFIG##*backend:{}
A=${A##*file:[ \'\"][\'\"]}
A=${A%%[\'\"],*} # app_main/app_back.js

if [ "$NODEJS_BIN" ]
then echo '^ $NODEJS_BIN is set, run "'"$NODEJS_BIN"'"' && BACKEND="$NODEJS_BIN $A"
else echo '^ $NODEJS_BIN is not set, run `node`' && BACKEND="node $A"
fi

A=${A##*/}
A=${A%%.js*} # app_back

echo '
^ running Node.JS backend
^ command: `'"$BACKEND"'`
^ ctlport: "'"$BACKEND_PORT"'"
'

# lftp way of http access (obsolete): `_lftp_http 1 'cmd_exit'`
_lftp_http() { # $1=timeout $2=cmd
    { # http head request with contentlength=0 reply
        echo "[lftp->nodeJS:$JSAPPCTLPORT] sending '$2'"
        lftp -c '
set cmd:long-running 2
set net:idle 2
set net:timeout 2
set net:max-retries 2
set net:reconnect-interval-base '"$1"'
set net:reconnect-interval-multiplier 1

cd http://127.0.0.1:'"$BACKEND_PORT"'/ && cat '"$2"' && exit 0 || exit 1
'
    } 0</dev/null
    return $?
}

_http() { # $1=cmd $2=timeout
    node './_http.js' "http://127.0.0.1:$BACKEND_PORT/$1" "$2"
}

[ "$1" ] && {
    echo 'Logging in "./log/"'
    [ -d './log/' ] || {
        echo 'Creating "./log/"'
        mkdir log
    }
    exec 7>>log/${A}_stdout.txt 8>>log/${A}_stderr.txt
} || {<commit_msg>_devel_backend.sh: print node.js version<commit_after>#!/bin/sh

# * $1 == 'mongo' launch mongo shell with current config DB name
# * $1 == 'mongo-edit' launch mongo-edit current config DB name or
#         env $NODEJS_MONGO_DB may change default database name
# * any param "$1" will redirect node.js' output i.e:
#   1>>log/app_back_stdout.txt 2>>log/app_back_stderr.txt
# * if env $NODEJS_CONFIG is exported it is used and no hardcoded file is read
# * if env $NODEJS_BIN is exported then this binary name will be used instead of `node`
# * env $NODEJS_RELOAD app doesn't call `call_done_handlers()` when shuts down
#   i.e. doesn't stop `mongodb`

cd "${0%/*}" 2>/dev/null
set -e

# though git for windows is preferred a bundle of cygwin executables can be here
PATH=.:bin:$PATH

trap 'echo "
Unexpected Script Error! Use /bin/sh -x $0 to trace it.
"
set +e

trap "" 0
exit 0
' 0

normal_exit(){
    echo "
Normal Exit${1:- (backend is running)}
"
    set +e
    trap '' 0
    exit 0
}

trap 'normal_exit' HUP TERM INT

if [ "$NODEJS_CONFIG" ]
then
    echo '
^ config is already exported in "$NODEJS_CONFIG"'
else
    echo '
^ reading config in "$NODEJS_CONFIG" from file "./config/cfg_default.js"'
    NODEJS_CONFIG=`sed '' <./config/cfg_default.js`
    echo '^ exporting it for childs'
    export NODEJS_CONFIG
fi

case "$1" in
    mongo)
    sh app_modules/supromongod/etc/mongo-shell.sh
    trap '' 0
    exit 0
    ;;
    mongo-edit)
    # env $NODEJS_MONGO_DB may change default database name
    sh app_modules/supromongod/etc/mongo-edit.sh
    trap '' 0
    exit 0
    ;;
esac

# JS config sample to parse:
#    backend:{
#        file: './app_main/app_back.js',
#        job_port: 3007,// app/api
# here>> ctl_port: 3008,// controlling
#        ctl_on_done: null,// set app module handlers for ctl close/app exit
#        init_timeout: 123
#    }
BACKEND_PORT=${NODEJS_CONFIG##*ctl_port:}
BACKEND_PORT=${BACKEND_PORT## }
BACKEND_PORT=${BACKEND_PORT%%,*}

A=${NODEJS_CONFIG##*backend:{}
A=${A##*file:[ \'\"][\'\"]}
A=${A%%[\'\"],*} # app_main/app_back.js

if [ "$NODEJS_BIN" ] # for `node-debug`
then echo '^ $NODEJS_BIN is set, run "'"$NODEJS_BIN"'"' && BACKEND="$NODEJS_BIN"
else echo '^ $NODEJS_BIN is not set, run `node`' && BACKEND=node
fi

echo '
^ running backend on Node.JS '"`$BACKEND -v`"'
^ command: `'"$BACKEND"'`
^ ctlport: "'"$BACKEND_PORT"'"
'

BACKEND="$BACKEND $A"
A=${A##*/}
A=${A%%.js*} # app_back

# lftp way of http access (obsolete): `_lftp_http 1 'cmd_exit'`
_lftp_http() { # $1=timeout $2=cmd
    { # http head request with contentlength=0 reply
        echo "[lftp->nodeJS:$JSAPPCTLPORT] sending '$2'"
        lftp -c '
set cmd:long-running 2
set net:idle 2
set net:timeout 2
set net:max-retries 2
set net:reconnect-interval-base '"$1"'
set net:reconnect-interval-multiplier 1

cd http://127.0.0.1:'"$BACKEND_PORT"'/ && cat '"$2"' && exit 0 || exit 1
'
    } 0</dev/null
    return $?
}

_http() { # $1=cmd $2=timeout
    node './_http.js' "http://127.0.0.1:$BACKEND_PORT/$1" "$2"
}

[ "$1" ] && {
    echo 'Logging in "./log/"'
    [ -d './log/' ] || {
        echo 'Creating "./log/"'
        mkdir log
    }
    exec 7>>log/${A}_stdout.txt 8>>log/${A}_stderr.txt
} || {
{'fraction_non_alphanumeric': 0.1662722826944313, 'fraction_numerical': 0.02019245937845086, 'mean_word_length': 3.2895805142083896, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4179720', 'n_tokens_mistral': 2513, 'n_tokens_neox': 2382, 'n_words': 670}