From 18d070a950b57ee4326079cdc3c98a87cc0e4a44 Mon Sep 17 00:00:00 2001 From: dzove855 Date: Mon, 28 Jan 2019 15:23:22 +0100 Subject: [PATCH 01/11] push yoctapi --- config/app.sh | 5 +- config/dbconnector.sh | 17 ----- config/htpasswd | 1 - config/htpasswd.sh | 1 - config/logger.sh | 3 - config/mysql.sh | 13 ---- config/route.sh | 3 +- config/yoctapi.sh | 32 +++++++++ func/dbconnector.sh | 128 ------------------------------------ func/mysql.sh | 43 ------------ lib/yoctapi.sh | 144 +++++++++++++++++++++++++++++++++++++++++ lighttpd.conf.esm.dist | 35 ---------- logging/audit.sh | 21 ------ logging/logger.sh | 10 --- public/main.sh | 38 +++-------- 15 files changed, 186 insertions(+), 308 deletions(-) delete mode 100644 config/dbconnector.sh delete mode 100644 config/htpasswd delete mode 100644 config/htpasswd.sh delete mode 100644 config/mysql.sh create mode 100644 config/yoctapi.sh delete mode 100644 func/dbconnector.sh delete mode 100644 func/mysql.sh create mode 100644 lib/yoctapi.sh delete mode 100644 lighttpd.conf.esm.dist delete mode 100644 logging/audit.sh delete mode 100644 logging/logger.sh diff --git a/config/app.sh b/config/app.sh index 22510ee..233d751 100644 --- a/config/app.sh +++ b/config/app.sh @@ -1,4 +1,4 @@ -application_name="yosh" +application_name="yoctapi" html_dir="${DOCUMENT_ROOT%/}/../html" css_dir="${DOCUMENT_ROOT%/}/../css" js_dir="${DOCUMENT_ROOT%/}/../js" @@ -6,6 +6,3 @@ img_dir="${DOCUMENT_ROOT%/}/../img" fonts_dir="${DOCUMENT_ROOT%/}/../fonts" api_dir="${DOCUMENT_ROOT%/}../api" etc_conf_dir="/etc/$application_name/" -login_method="auth::request" -login_page="login" -login_unauthorized="html::print::out ${html_dir}/login.html" diff --git a/config/dbconnector.sh b/config/dbconnector.sh deleted file mode 100644 index 3cd3b26..0000000 --- a/config/dbconnector.sh +++ /dev/null @@ -1,17 +0,0 @@ -declare -A DB_GET -declare -A DB_PUT -declare -A DB_DELETE -declare -A DB_POST -declare -A DB_CONNECT - -DB_CONNECT['table':'slave_mysql_dbname']="dbname" -DB_CONNECT['table':'slave_mysql_host']="host" -DB_CONNECT['table':'slave_mysql_user']="user" -DB_CONNECT['table':'slave_mysql_port']="3306" -DB_CONNECT['table':'slave_mysql_password']="password" - -DB_GET['table':'where']="service" - -### Update ### -DB_PUT['table':'where']="id" -DB_DELETE['table':'where']="id" diff --git a/config/htpasswd b/config/htpasswd deleted file mode 100644 index 6265114..0000000 --- a/config/htpasswd +++ /dev/null @@ -1 +0,0 @@ -yoctu:$apr1$r31....$LWAl/xxE/MI8X7bwypvQh0:admin diff --git a/config/htpasswd.sh b/config/htpasswd.sh deleted file mode 100644 index 15ef166..0000000 --- a/config/htpasswd.sh +++ /dev/null @@ -1 +0,0 @@ -htpasswd_file="${DOCUMENT_ROOT%/}/../config/htpasswd" diff --git a/config/logger.sh b/config/logger.sh index df5267b..e69de29 100644 --- a/config/logger.sh +++ b/config/logger.sh @@ -1,3 +0,0 @@ -declare -A LOGGING - LOGGING['file':'method']="file" - LOGGING['file':'tag']="log" diff --git a/config/mysql.sh b/config/mysql.sh deleted file mode 100644 index bc1a50e..0000000 --- a/config/mysql.sh +++ /dev/null @@ -1,13 +0,0 @@ -# Slave db only for read -slave_mysql_user="" -slave_mysql_password="" -slave_mysql_host="" -slave_mysql_port="" -slave_mysql_dbname="" - -# master db -master_mysql_user="" -master_mysql_password="" -master_mysql_host="" -master_mysql_port="" -master_mysql_dbname="" diff --git a/config/route.sh b/config/route.sh index f97d789..68e87a2 100755 --- a/config/route.sh +++ b/config/route.sh @@ -5,11 +5,10 @@ #ROUTE['/':'GET':'connect']="html::print::out ${html_dir}/home.html" api_command="Api::call::function" -default_api_function="dbconnector" +default_api_function="Yoctapi::api::main" # router="route::api::mode" # router="route::check" -router="Route::api::mode" # Defaults routes #AUTH['/':'GET']="htpasswd" diff --git a/config/yoctapi.sh b/config/yoctapi.sh new file mode 100644 index 0000000..4bb1d3e --- /dev/null +++ b/config/yoctapi.sh @@ -0,0 +1,32 @@ +[public:assoc] YOCTAPI + +# Read Credentials +YOCTAPI['route':'MATCHER':'read':'credentials':'host']="localhost" +YOCTAPI['route':'MATCHER':'read':'credentials':'user']="USER" +YOCTAPI['route':'MATCHER':'read':'credentials':'password']="PASSWORD" +YOCTAPI['route':'MATCHER':'read':'credentials':'database']="DATABASE" +YOCTAPI['route':'MATCHER':'read':'connector']="mysql" + +# Write Credentials +YOCTAPI['route':'MATCHER':'write':'credentials':'host']="localhost" +YOCTAPI['route':'MATCHER':'write':'credentials':'user']="USER" +YOCTAPI['route':'MATCHER':'write':'credentials':'password']="PASSWORD" +YOCTAPI['route':'MATCHER':'write':'credentials':'database']="DATABASE" +YOCTAPI['route':'MATCHER':'write':'connector']="mysql" + +# GET FILTERS +YOCTAPI['route':'MATCHER':'request':'get':'filter']="id,text" +YOCTAPI['route':'MATCHER':'request':'get':'display']="id" +YOCTAPI['route':'MATCHER':'request':'get':'search']="id" +YOCTAPI['route':'MATCHER':'request':'get':'table']="TABLE" + +# POST FILTERS +YOCTAPI['route':'MATCHER':'request':'post':'table']="TABLE" + +# PUT FILTERS +YOCTAPI['route':'MATCHER':'request':'put':'search']="id" +YOCTAPI['route':'MATCHER':'request':'put':'table']="TABLE" + +# DELETE FILTERS +YOCTAPI['route':'MATCHER':'request':'delete':'search']="id" +YOCTAPI['route':'MATCHER':'request':'delete':'table']="TABLE" diff --git a/func/dbconnector.sh b/func/dbconnector.sh deleted file mode 100644 index a4c621d..0000000 --- a/func/dbconnector.sh +++ /dev/null @@ -1,128 +0,0 @@ -db-get(){ - local matcher="$1" action="$2" _select _where _limit mysql_command="mysql-connect-slave" table display - - table="${DB_GET[$matcher:'table']:-$matcher}" - - _select="${DB_GET[$matcher:'column']:-*}" - - display="${DB_GET[$matcher:'display']:-$matcher}" - - if [[ ! -z "${DB_GET[$matcher:'selectfields']}" ]] - then - _select="${DB_GET[$matcher:'selectfields']%%;*}" - fi - - if [[ -z "${DB_GET[$matcher:'where']}" ]] - then - _where="" - else - _where="where ${DB_GET[$matcher:'where']} like '$action'" - fi - - ! [[ -z "${GET['limit']}" ]] && _limit="limit ${GET['limit']%%;*}" - - mysql-to-json "$display" "select $_select from $table $_where $_limit" -} - -db-put(){ - local matcher="$1" action="$2" _query _json _update value result table - - [[ -z "$action" ]] && return - - [[ -z "${POST[*]}" ]] && return - - [[ -z "${DB_PUT[$matcher:'where']}" ]] && return - - table="${DB_PUT[$matcher:'table']:-$matcher}" - - _query="update $table set" - - for value in "${!POST[@]}" - do - result="${POST[$value]}" - - # escape ; - value="${value/data:}" - value="${value%%;*}" - result="${result%%;*}" - - printf -v value "%q" "$value" - printf -v result "%q" "$result" - - _update+="\`${value}\`='$result'," - done - - _query+=" ${_update%,} where ${DB_PUT[$matcher:'where']}='$action'" - - mysql-connector-master "$_query" &>/dev/null && echo "{ \"msg\": \"Succesfully updated!\", \"${DB_PUT[$matcher:'where']}\": \"$action\", \"status\":\"$?\" }" - -} - -db-delete(){ - local matcher="$1" action="$2" table - - [[ -z "$action" ]] && return - - [[ -z "${DB_DELETE[$matcher:'where']}" ]] && return - - table="${DB_DELETE[$matcher:'table']:-$matcher}" - - mysql-connector-master "delete from $table where ${DB_DELETE[$matcher:'where']}='$action'" &>/dev/null && echo '{ "msg": "Sccesfully removed!" }' -} - -db-post(){ - local matcher="$1" _query _json _insert value result table - - [[ -z "$matcher" ]] && return - - [[ -z "${POST[*]}" ]] && return - - table="${DB_POST[$matcher:'table']:-$matcher}" - - _query="insert into $table set" - - for value in "${!POST[@]}" - do - result="${POST[$value]}" - - # escape ; - value="${value/data:}" - value="${value%%;*}" - result="${result%%;*}" - - printf -v value "%q" "$value" - printf -v result "%q" "$result" - - _insert+="\`${value}\`='$result'," - done - - _query+=" ${_insert%,}" - - mysql-connector-master "$_query" &>/dev/null && echo '{ "msg": "Succesfully added!" }' -} - -dbconnector(){ - local matcher action method - - matcher="${uri[1]%%;*}" - action="$(urlencode -d "${uri[2]%%;*}")" - method="$REQUEST_METHOD" - - printf -v matcher "%q" "$matcher" - printf -v action "%q" "$action" - - case "$method" in - GET) db-get "$matcher" "$action" ;; - PUT) db-put "$matcher" "$action" ;; - DELETE) db-delete "$matcher" "$action" ;; - POST) db-post "$matcher" ;; - *) fail "Method not allowed!" ;; - esac - -} - -fail(){ - http::send::status 500 - echo '{ "msg": "Method not allowed!"' -} - diff --git a/func/mysql.sh b/func/mysql.sh deleted file mode 100644 index 83e4768..0000000 --- a/func/mysql.sh +++ /dev/null @@ -1,43 +0,0 @@ -function mysql-connect-slave () -{ - mysql --default-character-set=utf8 $_mysql_opts -h ${DB_CONNECT[$table:'slave_mysql_host']:-$slave_mysql_host} -P ${DB_CONNECT[$table:'slave_mysql_port']:-$slave_mysql_port} -u ${DB_CONNECT[$table:'slave_mysql_user']:-$slave_mysql_user} --database=${DB_CONNECT[$table:'slave_mysql_dbname']:-$slave_mysql_dbname} -p${DB_CONNECT[$table:'slave_mysql_password']:-$slave_mysql_password} -e "$*\G" 2>/dev/null - echo END -} - -function mysql-connector-master () -{ - mysql --default-character-set=utf8 -h ${DB_CONNECT[$table:'master_mysql_host']:-$master_mysql_host} -P ${DB_CONNECT[$table:'master_mysql_port']:-$master_mysql_port} -u ${DB_CONNECT[$table:'master_mysql_user']:-$master_mysql_user} --database=${DB_CONNECT[$table:'master_mysql_dbname']:-$master_mysql_dbname} -p${DB_CONNECT[$table:'master_mysql_password']:-$master_mysql_password} -e "$*\G" 2>/dev/null - echo END -} - -function mysql-to-json () -{ - local display="$1" result - typeset -A arr - while read -r line - do - if [[ "${line}" =~ .*row.* || "$line" == "END" ]] - then - if [[ "$display" == "$matcher" ]] - then - [[ ! -z "${arr[@]}" ]] && results+="$(array-to-json arr)," - else - [[ ! -z "${arr[@]}" ]] && results+="\"${arr[$display]}\":$(array-to-json arr)," - fi - elif ! [[ "${line}" =~ .*row.* || "$line" == "END" ]] - then - result="${line#*:}" - trim result - arr[${line%%:*}]="$result" - fi - done < <($mysql_command "${*:2}" | grep -v -e '^$' | tr -d '\r') - - - if [[ "$display" == "$matcher" ]] - then - echo "{ \"$matcher\": [ ${results%,} ] }" - else - echo "{ \"$matcher\": { ${results%,} } }" - fi -} - diff --git a/lib/yoctapi.sh b/lib/yoctapi.sh new file mode 100644 index 0000000..2f1fd5c --- /dev/null +++ b/lib/yoctapi.sh @@ -0,0 +1,144 @@ +[public:assoc] YOCTAPI +YOCTAPI['config':'get':'action']="read" +YOCTAPI['config':'post':'action']="write" +YOCTAPI['config':'put':'action']="write" +YOCTAPI['config':'delete':'action']="write" + +Yoctapi::api::main(){ + [private:assoc] result + [private] table="${uri[1]}" + + [[ -z "${YOCTAPI['route':$table:${YOCTAPI['config':${REQUEST_METHOD,,}:'action']}:'connector']}" ]] && return 1 + + # Build creadentials + Yoctapi::build::credentials + + DATA['matcher':$table]="${YOCTAPI['route':$table:${YOCTAPI['config':${REQUEST_METHOD,,}:'action']}:'connector']}" + + # Run Request + Yoctapi::${REQUEST_METHOD,,} "$table" +} + +Yoctapi::build::credentials(){ + [public:map] array="${YOCTAPI['route':$table:${YOCTAPI['config':${REQUEST_METHOD,,}:'action']}:'connector']^^}" + + while read line; do + array[connection:$line]="${YOCTAPI[route:$table:${YOCTAPI['config':${REQUEST_METHOD,,}:'action']}:credentials:$line]}" + done < <(Type::array::get::key route:$table:${YOCTAPI['config':${REQUEST_METHOD,,}:'action']}:credentials YOCTAPI) + + return +} + +Yoctapi::get(){ + [private] table="$1" + [private] search="${uri[2]//;/}" + [private:assoc] result + [private:assoc] output + [private:assoc] query + + query['table']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'table']}" + + if ! [[ -z "$search" ]]; then + if [[ -z "${GET['data':'search']}" ]]; then + query['search']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}='$search'" + else + query['search']="${GET['data':'search']}='$search'" + fi + fi + + if [[ -z "${GET['data':'filter']}" ]]; then + query['filter']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'filter']}" + else + query['filter']="${GET['data':'filter']}" + fi + + Data::get "result" "$(Data::build::query::get query $table)" "$table" + + while read line; do + while read keys; do + output[$table:${result['result':$line:${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'display']}]}:$keys]="${result[result:$line:$keys]}" + done < <(Type::array::get::key result:$line result) + done < <(Type::array::get::key result result) + + Api::send::get output +} + +Yoctapi::post(){ + [private] table="$1" + [private:assoc] query + [private:assoc] result + [private:assoc] output + + query['table']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'table']}" + + [[ -z "${POST[*]}" ]] && Api::send::fail + + Type::array::fusion POST query + + Data::post 'result' "$(Data::build::query::post query $table)" "$table" + + while read line; do + output[$table:$line]="${result['result':'1':$line]}" + done < <(Type::array::get::key result:1 result) + + Api::send::post output +} + +Yoctapi::put(){ + [private] table="$1" + [private] search="${uri[2]}" + [private:assoc] query + [private:assoc] result + [private:assoc] output + + query['table']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'table']}" + + [[ -z "${POST[*]}" ]] && Api::send::fail + [[ -z "${search}" ]] && Api::send::not_found + + Type::array::fusion POST query + + if ! [[ -z "$search" ]]; then + if [[ -z "${GET['data':'search']}" ]]; then + query['search']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}='$search'" + else + query['search']="${GET['data':'search']}='$search'" + fi + fi + + Data::put 'result' "$(Data::build::query::put query $table)" "$table" + + while read line; do + output[$table:$line]="${result['result':'1':$line]}" + done < <(Type::array::get::key result:1 result) + + Api::send::put output +} + +Yoctapi::delete(){ + [private] table="$1" + [private] search="${uri[2]}" + [private:assoc] query + [private:assoc] result + [private:assoc] output + + query['table']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'table']}" + + [[ -z "${search}" ]] && Api::send::not_found + + if ! [[ -z "$search" ]]; then + if [[ -z "${GET['data':'search']}" ]]; then + query['search']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}='$search'" + else + query['search']="${GET['data':'search']}='$search'" + fi + fi + + Data::delete 'result' "$(Data::build::query::delete query $table)" "$table" + + while read line; do + output[$table:$line]="${result['result':'1':$line]}" + done < <(Type::array::get::key result:1 result) + + Api::send::delete output +} diff --git a/lighttpd.conf.esm.dist b/lighttpd.conf.esm.dist deleted file mode 100644 index 281e097..0000000 --- a/lighttpd.conf.esm.dist +++ /dev/null @@ -1,35 +0,0 @@ -server.modules = ( - "mod_access", - "mod_alias", - "mod_compress", - "mod_redirect", - "mod_cgi" -) - -server.document-root = "/var/www/esm/public" -server.upload-dirs = ( "/tmp" ) -server.errorlog = "/var/log/lighttpd/error.log" -server.pid-file = "/var/run/lighttpd.pid" -server.username = "esm" -server.groupname = "www-data" -server.bind = "0.0.0.0" -server.port = 8000 -server.error-handler-404 = "/main.sh" - -index-file.names = ( "index.php", "index.html", "index.lighttpd.html" ) -url.access-deny = ( "~", ".inc" ) -static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" ) - -compress.cache-dir = "/var/cache/lighttpd/compress/" -compress.filetype = ( "application/javascript", "text/css", "text/html", "text/plain" ) - -cgi.assign = ( ".sh" => "/bin/bash" ) - -ssl.engine = "enable" -ssl.pemfile = "/etc/lighttpd/cert.pem" - -# default listening port for IPv6 falls back to the IPv4 port -include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port -include_shell "/usr/share/lighttpd/create-mime.assign.pl" -include_shell "/usr/share/lighttpd/include-conf-enabled.pl" - diff --git a/logging/audit.sh b/logging/audit.sh deleted file mode 100644 index 0f451ea..0000000 --- a/logging/audit.sh +++ /dev/null @@ -1,21 +0,0 @@ -# audit - -function audit::log () -{ - local context - - case "$_level" in - crit) auditlevel="panic" ;; - esac - - # Parse Message - - IFS='|' read -a _data <<< $_message - - for data in "${_data[@]}" - do - context+=" -C $data" - done - - audit-client.sh -U $auditurl -l ${auditlevel:-$_level} -o http -b "$uri" -m "$application_name : $_level : ${REQUEST_URI%\?*}" $context -2 &>/dev/null -} diff --git a/logging/logger.sh b/logging/logger.sh deleted file mode 100644 index 8e3599c..0000000 --- a/logging/logger.sh +++ /dev/null @@ -1,10 +0,0 @@ -# logger - -function logger::log () -{ - case "$_level" in - crit) loggerlevel="panic" ;; - esac - - logger-client.sh -U $loggerurl -l ${loggerlevel:-$_level} -o http -b "$REQUEST_URI" -m "ESM : $_level : ${REQUEST_URI%\?*}" -C "msg=$_message" &>/dev/null -} diff --git a/public/main.sh b/public/main.sh index ed06cbd..aaf2bd4 100755 --- a/public/main.sh +++ b/public/main.sh @@ -1,32 +1,10 @@ #!/bin/bash -# main file - -# use autoloader -source /usr/share/yosh/autoloader.sh - -# get GET and POST and COOKIE variable -http::read::get -http::read::post -http::read::cookie - -# redirect stdout and stderr of function to file, to print after -tmpStdout="$(mktemp -p $TMPDIR)" -tmpStderr="$(mktemp -p $TMPDIR)" - -# Clean TMP file on exit -trap "rm $tmpStdout; rm $tmpStderr" EXIT - -# Save stdout and stderr to a file, to print out the both -# route::check 1>$tmpStdout 2>$tmpStderr -$router 1>$tmpStdout 2>$tmpStderr - -# Send header -http::send::out - -# send data from route -[[ -s "$tmpStdout" ]] && cat $tmpStdout -[[ -s "$tmpStderr" ]] && log -m $default_logging_method -l crit "STDERR: $(cat $tmpStderr)" - -# exit like a pro -exit +# We use the defaul main.sh from yosh + +if [[ -f "/usr/share/yosh/main.sh" ]] +then + source /usr/share/yosh/main.sh +else + echo "Yosh is missing!" +fi From 61051e242be2e091ff733d61346952e2af0041c1 Mon Sep 17 00:00:00 2001 From: dzove855 Date: Mon, 28 Jan 2019 18:44:19 +0100 Subject: [PATCH 02/11] add display --- lib/yoctapi.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/yoctapi.sh b/lib/yoctapi.sh index 2f1fd5c..e5382a9 100644 --- a/lib/yoctapi.sh +++ b/lib/yoctapi.sh @@ -32,6 +32,7 @@ Yoctapi::build::credentials(){ Yoctapi::get(){ [private] table="$1" [private] search="${uri[2]//;/}" + [private] display [private:assoc] result [private:assoc] output [private:assoc] query @@ -52,11 +53,17 @@ Yoctapi::get(){ query['filter']="${GET['data':'filter']}" fi + if [[ -z "${GET['data':'display']}" ]]; then + display="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'display']}" + else + display="${GET['data':'display']}" + fi + Data::get "result" "$(Data::build::query::get query $table)" "$table" while read line; do while read keys; do - output[$table:${result['result':$line:${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'display']}]}:$keys]="${result[result:$line:$keys]}" + output[$table:${result['result':$line:$display]}:$keys]="${result[result:$line:$keys]}" done < <(Type::array::get::key result:$line result) done < <(Type::array::get::key result result) @@ -142,3 +149,4 @@ Yoctapi::delete(){ Api::send::delete output } + From 8ca9aa809fe2358b0c2a41de5035a75ce390273e Mon Sep 17 00:00:00 2001 From: dzove855 Date: Tue, 29 Jan 2019 18:33:17 +0100 Subject: [PATCH 03/11] Modify config --- config/yoctapi.sh | 12 +++++++----- lib/yoctapi.sh | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/config/yoctapi.sh b/config/yoctapi.sh index 4bb1d3e..9faf24c 100644 --- a/config/yoctapi.sh +++ b/config/yoctapi.sh @@ -15,18 +15,20 @@ YOCTAPI['route':'MATCHER':'write':'credentials':'database']="DATABASE" YOCTAPI['route':'MATCHER':'write':'connector']="mysql" # GET FILTERS -YOCTAPI['route':'MATCHER':'request':'get':'filter']="id,text" -YOCTAPI['route':'MATCHER':'request':'get':'display']="id" -YOCTAPI['route':'MATCHER':'request':'get':'search']="id" +YOCTAPI['route':'MATCHER':'request':'get':'filter']="FILER1,FITLER2" +YOCTAPI['route':'MATCHER':'request':'get':'objet']="OBJET" +YOCTAPI['route':'MATCHER':'request':'get':'search']="SEARCH" YOCTAPI['route':'MATCHER':'request':'get':'table']="TABLE" # POST FILTERS YOCTAPI['route':'MATCHER':'request':'post':'table']="TABLE" # PUT FILTERS -YOCTAPI['route':'MATCHER':'request':'put':'search']="id" +YOCTAPI['route':'MATCHER':'request':'put':'search']="SEARCH" YOCTAPI['route':'MATCHER':'request':'put':'table']="TABLE" # DELETE FILTERS -YOCTAPI['route':'MATCHER':'request':'delete':'search']="id" +YOCTAPI['route':'MATCHER':'request':'delete':'search']="SEARCH" YOCTAPI['route':'MATCHER':'request':'delete':'table']="TABLE" + +YOCTAPI['route':'bertrand':'request':'delete':'search']="SEARCH" diff --git a/lib/yoctapi.sh b/lib/yoctapi.sh index e5382a9..f0cd88b 100644 --- a/lib/yoctapi.sh +++ b/lib/yoctapi.sh @@ -53,10 +53,10 @@ Yoctapi::get(){ query['filter']="${GET['data':'filter']}" fi - if [[ -z "${GET['data':'display']}" ]]; then - display="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'display']}" + if [[ -z "${GET['data':'objet']}" ]]; then + display="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'objet']}" else - display="${GET['data':'display']}" + display="${GET['data':'objet']}" fi Data::get "result" "$(Data::build::query::get query $table)" "$table" From 03198e9e48d58acdd6b6c9cb8410c1bc208c28c8 Mon Sep 17 00:00:00 2001 From: dzove855 Date: Sun, 3 Feb 2019 21:50:42 +0100 Subject: [PATCH 04/11] rename object --- config/yoctapi.sh | 2 +- lib/yoctapi.sh | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/yoctapi.sh b/config/yoctapi.sh index 9faf24c..8415149 100644 --- a/config/yoctapi.sh +++ b/config/yoctapi.sh @@ -16,7 +16,7 @@ YOCTAPI['route':'MATCHER':'write':'connector']="mysql" # GET FILTERS YOCTAPI['route':'MATCHER':'request':'get':'filter']="FILER1,FITLER2" -YOCTAPI['route':'MATCHER':'request':'get':'objet']="OBJET" +YOCTAPI['route':'MATCHER':'request':'get':'object']="OBJECT" YOCTAPI['route':'MATCHER':'request':'get':'search']="SEARCH" YOCTAPI['route':'MATCHER':'request':'get':'table']="TABLE" diff --git a/lib/yoctapi.sh b/lib/yoctapi.sh index f0cd88b..dab77d7 100644 --- a/lib/yoctapi.sh +++ b/lib/yoctapi.sh @@ -53,10 +53,10 @@ Yoctapi::get(){ query['filter']="${GET['data':'filter']}" fi - if [[ -z "${GET['data':'objet']}" ]]; then - display="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'objet']}" + if [[ -z "${GET['data':'object']}" ]]; then + display="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'object']}" else - display="${GET['data':'objet']}" + display="${GET['data':'object']}" fi Data::get "result" "$(Data::build::query::get query $table)" "$table" From 3d545fdeaaff77335b533ae60e0371a17c971683 Mon Sep 17 00:00:00 2001 From: dzove855 Date: Sun, 3 Feb 2019 22:40:47 +0100 Subject: [PATCH 05/11] Add api messages --- lib/yoctapi.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/yoctapi.sh b/lib/yoctapi.sh index dab77d7..e53d4f4 100644 --- a/lib/yoctapi.sh +++ b/lib/yoctapi.sh @@ -8,7 +8,7 @@ Yoctapi::api::main(){ [private:assoc] result [private] table="${uri[1]}" - [[ -z "${YOCTAPI['route':$table:${YOCTAPI['config':${REQUEST_METHOD,,}:'action']}:'connector']}" ]] && return 1 + [[ -z "${YOCTAPI['route':$table:${YOCTAPI['config':${REQUEST_METHOD,,}:'action']}:'connector']}" ]] && { Api::send::not_found; } # Build creadentials Yoctapi::build::credentials @@ -67,6 +67,8 @@ Yoctapi::get(){ done < <(Type::array::get::key result:$line result) done < <(Type::array::get::key result result) + [[ -z "${output[*]}" ]] && Api::send::not_found + Api::send::get output } @@ -88,6 +90,8 @@ Yoctapi::post(){ output[$table:$line]="${result['result':'1':$line]}" done < <(Type::array::get::key result:1 result) + [[ -z "${output[*]}" ]] && Api::send::not_found + Api::send::post output } @@ -119,6 +123,8 @@ Yoctapi::put(){ output[$table:$line]="${result['result':'1':$line]}" done < <(Type::array::get::key result:1 result) + [[ -z "${output[*]}" ]] && Api::send::not_found + Api::send::put output } @@ -147,6 +153,8 @@ Yoctapi::delete(){ output[$table:$line]="${result['result':'1':$line]}" done < <(Type::array::get::key result:1 result) + [[ -z "${output[*]}" ]] && Api::send::not_found + Api::send::delete output } From 6dd7c9fec1b384f35054d45e8b225b607b6b7f1b Mon Sep 17 00:00:00 2001 From: dzove855 Date: Mon, 4 Feb 2019 18:34:11 +0100 Subject: [PATCH 06/11] Add limit and search --- lib/yoctapi.sh | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/yoctapi.sh b/lib/yoctapi.sh index e53d4f4..fa5c2f2 100644 --- a/lib/yoctapi.sh +++ b/lib/yoctapi.sh @@ -41,9 +41,11 @@ Yoctapi::get(){ if ! [[ -z "$search" ]]; then if [[ -z "${GET['data':'search']}" ]]; then - query['search']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}='$search'" + query['search':'column']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}" + query['search':'key']="$search" else - query['search']="${GET['data':'search']}='$search'" + query['search':'column']="${GET['data':'search']}" + query['search':'key']="$search" fi fi @@ -59,6 +61,8 @@ Yoctapi::get(){ display="${GET['data':'object']}" fi + [[ -z "${GET['data':'limit']}" ]] || query['limit']="${GET['data':'limit']}" + Data::get "result" "$(Data::build::query::get query $table)" "$table" while read line; do @@ -111,9 +115,11 @@ Yoctapi::put(){ if ! [[ -z "$search" ]]; then if [[ -z "${GET['data':'search']}" ]]; then - query['search']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}='$search'" + query['search':'column']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}" + query['search':'key']="$search" else - query['search']="${GET['data':'search']}='$search'" + query['search':'column']="${GET['data':'search']}" + query['search':'key']="$search" fi fi @@ -141,9 +147,11 @@ Yoctapi::delete(){ if ! [[ -z "$search" ]]; then if [[ -z "${GET['data':'search']}" ]]; then - query['search']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}='$search'" + query['search':'column']="${YOCTAPI['route':$table:'request':${REQUEST_METHOD,,}:'search']}" + query['search':'key']="$search" else - query['search']="${GET['data':'search']}='$search'" + query['search':'column']="${GET['data':'search']}" + query['search':'key']="$search" fi fi From e1fd1753a6e9fa1250a1b6fbbd130ee801b46b1f Mon Sep 17 00:00:00 2001 From: dzove855 Date: Tue, 5 Feb 2019 03:22:31 +0100 Subject: [PATCH 07/11] Add debian build package --- build.sh | 105 ++++++++++++++++++++++++++++++++++++++ debian/.gitignore | 0 debian/compat | 1 + debian/control | 12 +++++ debian/rules | 6 +++ debian/yoctapi.install | 14 +++++ dist/apache2/yoctapi.conf | 24 +++++++++ 7 files changed, 162 insertions(+) create mode 100755 build.sh create mode 100644 debian/.gitignore create mode 100644 debian/compat create mode 100644 debian/control create mode 100755 debian/rules create mode 100644 debian/yoctapi.install create mode 100644 dist/apache2/yoctapi.conf diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..8d8dafc --- /dev/null +++ b/build.sh @@ -0,0 +1,105 @@ +#!/bin/bash + +SELF="${BASH_SOURCE[0]##*/}" +NAME="${SELF%.sh}" + +OPTS="svxEh" +USAGE="Usage: $SELF [$OPTS]" + +HELP=" +$USAGE + + Options: + -s simulate + -v set -v + -x set -x + -e set -ve + -h Help + +" + +function _quit () +{ + local retCode="$1" msg="${@:2}" + + echo -e "$msg" + exit $retCode +} + +function _notify() +{ + echo -e "\n\n\n\n\n################################## $* #####################################\n\n\n\n\n" >&2 +} + +while getopts "${OPTS}" arg; do + case "${arg}" in + s) _run="echo" ;; + v) set -v ;; + x) set -x ;; + e) set -ve ;; + h) _quit 0 "$HELP" ;; + ?) _quit 1 "Invalid Argument: $USAGE" ;; + *) _quit 1 "$USAGE" ;; + esac +done +shift $((OPTIND - 1)) + +fileuuid="bck8:49f0d248-f21d-4a14-871d-31b007189081" +project="yoctapi" +ppa_repo="public" + +trap '_quit 2 "An Error occured while running script"' ERR + +_notify "Install dependencies" +sudo apt-get update >&/dev/null ; sudo apt-get install -y apt-transport-https devscripts debianutils jq gridsite-clients &>/dev/null +wget -qO - https://ppa.yoctu.com/archive.key | sudo apt-key add - + +curl -s -o /tmp/jq -O -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 +chmod +x /tmp/jq +sudo mv /tmp/jq /usr/bin/jq + +echo "deb https://ppa.yoctu.com/ all unstable" | sudo tee /etc/apt/sources.list +sudo apt-get update &>/dev/null +cd /tmp + +sudo apt-get install yoctu-client-scripts &>/dev/null + +_notify "Finished installing dependencies" + +_notify "Fetch changelog" +cd - + +filer-client.sh -U https://filer.test.flash.global -X get -u $fileuuid + +mv /tmp/${project}-changelog debian/changelog + +_notify "Fetched changelog" + +_notify "Setup Github" +sudo -s curl -o /bin/git-to-deb.sh -O -L https://ppa.yoctu.com/git-to-deb.sh +sudo chmod +x /bin/git-to-deb.sh + +git config --global user.email "git@yoctu.com" +git config --global user.name "git" +_notify "Setup done" + +_notify "Build package" + +git-to-deb.sh -i >/dev/null + +filer-client.sh -U https://filer.test.flash.global -c MISCELLANEOUS -n "${project}-changelog" -f debian/changelog -C "need=Changelog file for $project" -m "text/plain" -X update -u $fileuuid +_notify "Build done" + +_notify "Rsync to ppa" +mv ../$project*.deb ../$project.deb +export LC_FLASH_PROJECT_ID="$project" + +IFS='/' read nonused heads branch <<<$CPHP_GIT_REF +[[ "$heads" == "tags" ]] && branch="$heads" +export LC_FLASH_ARGS="project=$project ppa_repo=$ppa_repo git_branch=$branch flashGitRef=$CPHP_GIT_REF" +export LC_FLASH_YAML="000-default-flash-debian-script.yaml" + +export LC_FLASH_BRANCH=$CPHP_GIT_REF && scp -P2222 -o StrictHostKeyChecking=no -i ~/.ssh/automate.key ../$project.deb automate@term.test.flash-global.net:/tmp/${LC_FLASH_PROJECT_ID}.deb +_notify "Rsync done" + +rm -rf debian diff --git a/debian/.gitignore b/debian/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..7f8f011 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +7 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..4394c2c --- /dev/null +++ b/debian/control @@ -0,0 +1,12 @@ +Source: yoctapi +Section: utils +Maintainer: Dzove855 +Build-Depends: debhelper +Standards-Version: 0.8.20 + +Package: yoctapi +Architecture: all +Maintainer: Dzogovic Vehbo +Priority: optional +Depends: jq, yosh, mysql-client +Description: yoctapi (yoctu shell) - Micro Web Framework lib diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..483d681 --- /dev/null +++ b/debian/rules @@ -0,0 +1,6 @@ +#!/usr/bin/make -f +%: + dh $@ + +override_dh_builddeb: + dh_builddeb -- -Zgzip diff --git a/debian/yoctapi.install b/debian/yoctapi.install new file mode 100644 index 0000000..58c26ca --- /dev/null +++ b/debian/yoctapi.install @@ -0,0 +1,14 @@ +app var/www/yoctapi/ +auth var/www/yoctapi/ +bin var/www/yoctapi/ +config var/www/yoctapi/ +css var/www/yoctapi/ +fonts var/www/yoctapi/ +func var/www/yoctapi/ +html var/www/yoctapi/ +img var/www/yoctapi/ +js var/www/yoctapi/ +lib var/www/yoctapi/ +public var/www/yoctapi/ +README.md usr/share/yoctapi/doc/ +dist/apache2/yoctapi.conf etc/apache2/sites-available/ diff --git a/dist/apache2/yoctapi.conf b/dist/apache2/yoctapi.conf new file mode 100644 index 0000000..5dfa272 --- /dev/null +++ b/dist/apache2/yoctapi.conf @@ -0,0 +1,24 @@ + + + define documentroot /var/www/yoctapi/public + + DocumentRoot ${documentroot} + + + Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch + AddHandler cgi-script .sh + + RewriteEngine On + RewriteCond %{REQUEST_FILENAME} -s [OR] + RewriteCond %{REQUEST_FILENAME} -l [OR] + RewriteCond %{REQUEST_FILENAME} -d + RewriteRule ^.*$ - [NC,L] + RewriteRule ^.*$ /main.sh [NC,L] + + DirectoryIndex main.sh + + + + undefine documentroot + + From a36bf703e7d40822c428edbc07e2ba7d1b305b67 Mon Sep 17 00:00:00 2001 From: dzove855 Date: Tue, 5 Feb 2019 03:25:57 +0100 Subject: [PATCH 08/11] correct .install --- debian/yoctapi.install | 5 ----- fonts/.gitignore | 0 img/.gitignore | 0 3 files changed, 5 deletions(-) delete mode 100644 fonts/.gitignore delete mode 100644 img/.gitignore diff --git a/debian/yoctapi.install b/debian/yoctapi.install index 58c26ca..3343558 100644 --- a/debian/yoctapi.install +++ b/debian/yoctapi.install @@ -2,12 +2,7 @@ app var/www/yoctapi/ auth var/www/yoctapi/ bin var/www/yoctapi/ config var/www/yoctapi/ -css var/www/yoctapi/ -fonts var/www/yoctapi/ func var/www/yoctapi/ -html var/www/yoctapi/ -img var/www/yoctapi/ -js var/www/yoctapi/ lib var/www/yoctapi/ public var/www/yoctapi/ README.md usr/share/yoctapi/doc/ diff --git a/fonts/.gitignore b/fonts/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/img/.gitignore b/img/.gitignore deleted file mode 100644 index e69de29..0000000 From 379962673bc109ed015afc8e8290a80d6e7178c0 Mon Sep 17 00:00:00 2001 From: dzove855 Date: Tue, 5 Feb 2019 03:30:37 +0100 Subject: [PATCH 09/11] add right .deb --- debian/control | 2 +- debian/yoctapi.install | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/debian/control b/debian/control index 4394c2c..206bc48 100644 --- a/debian/control +++ b/debian/control @@ -8,5 +8,5 @@ Package: yoctapi Architecture: all Maintainer: Dzogovic Vehbo Priority: optional -Depends: jq, yosh, mysql-client +Depends: jq, yosh, libyosh-gateway Description: yoctapi (yoctu shell) - Micro Web Framework lib diff --git a/debian/yoctapi.install b/debian/yoctapi.install index 3343558..0f533f7 100644 --- a/debian/yoctapi.install +++ b/debian/yoctapi.install @@ -1,4 +1,3 @@ -app var/www/yoctapi/ auth var/www/yoctapi/ bin var/www/yoctapi/ config var/www/yoctapi/ From 710eedd8fc5f9a1b4f2644f99a89dcf5d1d2f81b Mon Sep 17 00:00:00 2001 From: dzove855 Date: Tue, 5 Feb 2019 03:36:15 +0100 Subject: [PATCH 10/11] add debug --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 8d8dafc..19c9797 100755 --- a/build.sh +++ b/build.sh @@ -85,7 +85,7 @@ _notify "Setup done" _notify "Build package" -git-to-deb.sh -i >/dev/null +git-to-deb.sh -i #>/dev/null filer-client.sh -U https://filer.test.flash.global -c MISCELLANEOUS -n "${project}-changelog" -f debian/changelog -C "need=Changelog file for $project" -m "text/plain" -X update -u $fileuuid _notify "Build done" From a183e9ebbbb635cf3698d2d5d2fc855958f1b29b Mon Sep 17 00:00:00 2001 From: dzove855 Date: Tue, 5 Feb 2019 03:39:48 +0100 Subject: [PATCH 11/11] rm func --- build.sh | 2 +- debian/yoctapi.install | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/build.sh b/build.sh index 19c9797..8d8dafc 100755 --- a/build.sh +++ b/build.sh @@ -85,7 +85,7 @@ _notify "Setup done" _notify "Build package" -git-to-deb.sh -i #>/dev/null +git-to-deb.sh -i >/dev/null filer-client.sh -U https://filer.test.flash.global -c MISCELLANEOUS -n "${project}-changelog" -f debian/changelog -C "need=Changelog file for $project" -m "text/plain" -X update -u $fileuuid _notify "Build done" diff --git a/debian/yoctapi.install b/debian/yoctapi.install index 0f533f7..24f5e9c 100644 --- a/debian/yoctapi.install +++ b/debian/yoctapi.install @@ -1,7 +1,6 @@ auth var/www/yoctapi/ bin var/www/yoctapi/ config var/www/yoctapi/ -func var/www/yoctapi/ lib var/www/yoctapi/ public var/www/yoctapi/ README.md usr/share/yoctapi/doc/