first structure working api - stil no database neither filesystem data

* separation of "notFound" module into one for api and one for Views
  if there is a wrong url will show `notFoundApi` module results
  otherwise will show the borrow of `notFound`
* provide new module for storage, under `api/v1/store` route,
  it receives 3 arguments, will check nature of variables and
  store if those are valid, the API will store a tree CTE data!
* provide enhanced listing under `api/v1/lists` route, that
  optionally receives one argument, if present will try to
  retrieve it data with filtering, otherwise will retreive all
* documents te layer oif the database used on the example, cos is a CTE
  recursive emulated layer table.
* documents into `DEVEL.md` the separate development from quick deploy
* tune up the `.env` file, only use minimal variables, remove non usefully
* use 0 spaces into the editors for the init config files on `.editorconfig`
* by defaults use sqlite3 databe in config file
* privide minimal test using php code
main
mckaygerhard 2024-04-17 22:44:37 -04:00
parent 3b2eac3cbc
commit bd5afb5078
17 changed files with 428 additions and 66 deletions

13
.env Normal file
View File

@ -0,0 +1,13 @@
APP_NAME=miniapi
APP_KEY=base64:W4HlzB4yC64SR5IMIGcdAYx0wOEM/fO3sEY427eF2aw=
APP_URL=localhost
APP_HOST=0.0.0.0
APP_PORT=8000
DB_CONNECTION=sqlite3
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db.sqlite3
DB_USERNAME=access
DB_PASSWORD=access

View File

@ -1,15 +1,10 @@
<?php
/*
____ _ _
/ ___|_ _ __ _ ___| |__ (_)
| | _| | | |/ _` |/ __| '_ \| |
| |_| | |_| | (_| | (__| | | | |
\____|\__,_|\__,_|\___|_| |_|_|
Copyright (c) 2014 Díaz Víctor aka (Máster Vitronic)
Copyright (c) 2018 Díaz Víctor aka (Máster Vitronic)
<vitronic2@gmail.com> <mastervitronic@vitronic.com.ve>
*/
/**!
* @package minenux-skindb-webdb
* @filename api/api.php controller
* @route >api
* @version 1.0
*/
class api_api_controller extends controller {
public function execute() {

View File

@ -1,26 +1,40 @@
<?php
/**!
* @package skindb-webdb
* @package minenux-skindb-webdb
* @filename lists.php controller
* @route >api>v1>lists
* @version 1.0
*/
class api_v1_lists_controller extends controller {
public function execute($post = null) {
$parameters = array(0=>"lists");
$arguments = array('id'=>-1);
$parameters = $this->router->parameters;
foreach($parameters as $key => $value) {
$$value = $value;
$$key = $value;
}
$arguments = array_merge($arguments, $_GET);
if ($_SERVER["REQUEST_METHOD"] == "GET") {
foreach($_GET as $keyp => $valuep) {
$$keyp = $valuep;
}
}
$arguments = array_merge($arguments, $_POST);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
foreach($_POST as $keyp => $valuep) {
$$keyp = $valuep;
}
}
$this->model->processdata($parameters, $arguments);
$this->model->show();
return;
}
}

View File

@ -0,0 +1,39 @@
<?php
/**!
* @package miniapi-webdb
* @filename store.php controller
* @route >api>v1>store
* @version 1.0
*/
class api_v1_store_controller extends controller {
public function execute($post = null) {
$parameters = array(0=>"lists");
$arguments = array('id'=>-1);
$parameters = $this->router->parameters;
foreach($parameters as $key => $value) {
$$key = $value;
}
$arguments = array_merge($arguments, $_GET);
if ($_SERVER["REQUEST_METHOD"] == "GET") {
foreach($_GET as $keyp => $valuep) {
$$keyp = $valuep;
}
}
$arguments = array_merge($arguments, $_POST);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
foreach($_POST as $keyp => $valuep) {
$$keyp = $valuep;
}
}
$this->model->processdata($parameters, $arguments);
$this->model->show();
return;
}
}

View File

@ -1,15 +1,10 @@
<?php
/*
____ _ _
/ ___|_ _ __ _ ___| |__ (_)
| | _| | | |/ _` |/ __| '_ \| |
| |_| | |_| | (_| | (__| | | | |
\____|\__,_|\__,_|\___|_| |_|_|
Copyright (c) 2014 Díaz Víctor aka (Máster Vitronic)
Copyright (c) 2018 Díaz Víctor aka (Máster Vitronic)
<vitronic2@gmail.com> <mastervitronic@vitronic.com.ve>
*/
/**!
* @package minenux-skindb-webdb
* @filename index.php controller
* @route >index
* @version 1.0
*/
class index_controller extends controller {
public function execute() {

View File

@ -1,11 +1,10 @@
<?php
/**!
* @package skindb-webdb
* @package minenux-skindb-webdb
* @filename lists.php controller
* @route >lists
* @version 1.0
*/
class lists_controller extends controller {
public function execute($post = null) {

View File

@ -1,6 +1,6 @@
## Skindb development
Quick setup: [README.md](README.md)
Quick setup to git clone this repo: [README.md](README.md)
### WORKFLOW
@ -35,6 +35,17 @@ Our eyecandy framework is based on Bulma https://github.com/jgthms/bulma?tab=rea
### routing structure and controllers
Any route must have a controller and also a module, both files must be named
in same way and routing will have this name.
There is no view cos its a concept, you can handle view using include routine
inside model or controller.
The model is also a concept, but here is represented as class under controller,
each controller has (and must has) a model, inside model all the bussiness logic
will be code, but controller can handle also those pieces of code.
* INDEX for routing: `modules.ini`
* API calls `controllers/api/v1/<module>.php`
* `models/api/v1/lists.php`
@ -50,6 +61,19 @@ Our eyecandy framework is based on Bulma https://github.com/jgthms/bulma?tab=rea
Please referes to [API.md](API.md) for all the skin api documentation, before
start to develop.
#### Authentication
The is no authtentication by default in the example database, any authenticated
route must be liste in private section of `modules.ini` filename of the root
proyect sources outside of public directory.
You can just emulated the authenticated routes using a conditional in the controllers,
that handle an API key that if are not listed will not accept the request or
shows only minimal data.
In any case you must use the routing mechanish to manage authenticated routes,
if you need more info please fils an issue at https://codeberg.org/codeigniter/miniapi/issues/new
### DATABASE
Se intentara omitir la carga hacia DB, usando los archivos de metadatos de los skins

View File

@ -9,6 +9,10 @@ A huge Minetest/Multicraft skin database interface that makes it possible to sea
This is the web front of huge skin database files at https://codeberg.org/minetest-stuffs/minetest-skindb-skindata
or the prefered site of https://git.minetest.io/minetest-stuffs/minetest-skindb-skindata repository files!
You can change those settings and files on your needs, the default API example
only handles a tree case, and retrieve the tree by nodes, there is no authentication,
but for auth support please read [DEVEL.md](DEVEL.md) document file.
## How to use this repo
``` bash
@ -31,7 +35,7 @@ cd $HOME/Devel/minenux-skindb-webdb
* `controllers/newlinktoshow.php`
* `models/newlinktoshow.php`
* http://localhost:<port>/newlinktoshow
* `http://localhost:<port>/newlinktoshow`
## How to Deploy and develop

78
docs/test.php Normal file
View File

@ -0,0 +1,78 @@
<?php
$env = parse_ini_file('../.env');
/* *** send as json SAVE DATA ************************************************* */
$url = $env["APP_HOST"].':'.$env["APP_PORT"]."/api/v1/store";
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'PAL' => 'PUV',
'cedh' => '1234565',
'cedp' => '1234567'
);
$payload = json_encode(array("user" => $data));
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
// Close cURL resource
curl_close($ch);
var_dump($result);
/* *** end send as json ************************************************* */
/* *** send as post SAVE DATA ************************************************* */
$url = $env["APP_HOST"].':'.$env["APP_PORT"]."/api/v1/store";
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$fields = array(
'PAL' => 'PUV',
'cedh' => '1234565',
'cedp' => '1234567'
);
$payload = http_build_query($data);
// Attach encoded fields string to the POST url
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to http post
curl_setopt($ch, CURLOPT_POST, true);
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
// Close cURL resource
curl_close($ch);
var_dump($result);
/* *** end send as post ************************************************* */

View File

@ -1,6 +1,6 @@
<?php
/**!
* @package skindb-webdb
* @package minenux-skindb-webdb
* @filename lists.php controller
* @route >api
* @version 1.0

View File

@ -1,6 +1,6 @@
<?php
/**!
* @package Skindb-webdb
* @package minenux-skindb-webdb
* @filename lists.php model
* @route >api>v1>lists
* @version 1.0
@ -8,15 +8,106 @@
class api_v1_lists_model extends model {
public function notFound() {
$this->borrow('notFoundApi')->show();
}
private $sql = null;
public function show() {
$errormessage = 'function not found or parameters are incorrect';
$httpcode = 404;
$variables = array('message'=>$errormessage,'httpcode'=>$httpcode);
$this->renderOutput($variables,true);
}
private $errormessage = 'error, cannot store values of data, error on parameters or arguments or error in data storage';
private $urlarguments = null;
private $savedatafull = array('nodes_child'=>'-1');
private $codehttpisok = 412;
public function notFound() {
$this->borrow('notFoundApi')->show();
}
public function processdata($parameter, $pagenm) {
//api/vi/lists/<ced>
$this->urlarguments = array('nodeid'=>$parameter, 'page'=>$pagenm);
$validator = new validator($this->urlarguments);
$validations = array(
'cedp' => array(
'type' => 'integer',
"required" => true
),
);
$check = $validator->execute($validations);
$this->sucess = $check[0];
if(! $this->sucess) {
$this->errormessage = $check[1];
return false;
}
$this->sucess = $this->_dbdatainit();
if(! $this->sucess) {
$this->errormessage = $this->errormessage.' please check DB layer structure!';
return false;
}
$this->sucess = $this->_dbdatavars();
$this->sucess = $this->_dbdatagets();
if(! $this->sucess) {
$this->errormessage = $this->errormessage.' Data error or no data/database';
return false;
}
$this->errormessage = 'Data present, check "data" variable';
$this->codehttpisok = 200;
$this->sucess = true;
return true;
}
private function _dbdatagets() {
$results = false;
$this->savedatafull = $this->db->execute($this->sql,$this->urlarguments['cedp']);
if(is_array($this->savedatafull) and is_array($this->savedatafull[0]))
$results = true;
return $results;
}
private function _dbdatainit() {
$results = -1;
$this->errormessage = ' Database structure seems not ready';
$this->sql = "SELECT count(nodes_childs) as nodes_childs FROM datatree LIMIT 1";
$results = $this->db->execute($this->sql);
if(is_array($results) and is_array($results[0])) {
$results = $results[0]['nodes_childs'];
return true;
}
return false;
}
private function _dbdatavars() {
$this->errormessage = ' Seems there is no data in database or maybe database layer is not ready..';
if( array_key_exists('cedp', $this->urlarguments)) {
if( trim($this->urlarguments['cedp']) == '') {
$this->urlarguments['cedp'] = '-1';
}
}
else
$this->urlarguments['cedp'] = '-1';
$sql = "
SELECT * FROM (
SELECT e1.nodes_childs, e1.nodes_parent, e1.nodes_notes
FROM datatree e1
WHERE e1.nodes_parent = %s
UNION ALL
SELECT e2.nodes_childs, e2.nodes_parent, e2.nodes_notes
FROM datatree e2
JOIN datatree e3 ON e2.nodes_parent = e3.nodes_childs
) nodes WHERE nodes_parent <> 0
";
$this->sql = $sql;
}
public function show($parameters = null) {
$errormessage = $this->errormessage;
$httpcode = $this->codehttpisok;
$sucess = $this->sucess;
if($this->sucess) $parameters = $this->savedatafull; else $sucess = 'false';
$variables = array('sucess'=>$sucess,'message'=>$errormessage,'httpcode'=>$httpcode,'data'=>$parameters);
$this->renderOutput($variables,true);
}
}

120
models/api/v1/store.php Normal file
View File

@ -0,0 +1,120 @@
<?php
/**!
* @package minenux-skindb-webdb
* @filename api list.php model
* @route >api>v1>store
* @version 1.0
*/
class api_v1_store_model extends model {
private $sql = null;
private $errormessage = 'error, cannot store values of data, error on parameters or arguments or error in data storage';
private $urlarguments = null;
private $savedatatodb = false;
private $savedatafull = false;
private $codehttpisok = 412;
public function notFound() {
$this->borrow('notFoundApi')->show();
}
public function processdata($parameter, $urlarguments) {
//api/vi/store/<asd>?PAL=UPV&cedh=nnnn&cedp=nnn
$this->urlarguments = $urlarguments;
$validator = new validator($urlarguments);
$validations = array(
'PAL' => array(
'type' => 'string',
"required" => true
),
'cedh' => array(
'type' => 'integer',
"required" => false
),
'cedp' => array(
'type' => 'integer',
"required" => false
),
);
$check = $validator->execute($validations);
$this->sucess = $check[0];
if(! $this->sucess) {
$this->errormessage = $check[1];
return false;
}
$this->sucess = $this->_dbdatainit();
if(! $this->sucess) {
$this->errormessage = $this->errormessage.' please check DB layer structure!';
return false;
}
$this->sucess = $this->_dbdatavars();
$this->sucess = $this->_dbdatasave();
if(! $this->sucess) {
$this->errormessage = $this->errormessage.' Data not saved into database';
return false;
}
$this->errormessage = 'Data saved into database';
$this->codehttpisok = 200;
$this->sucess = true;
return true;
}
private function _dbdatasave() {
$results = false;
$this->errormessage = ' Data save on db storage result in error when inserting, duplicate data or format error..';
if($this->savedatafull)
$results = $this->db->execute($this->sql,$this->urlarguments['cedh'],$this->urlarguments['cedp']);
else
$results = $this->db->execute($this->sql,$this->urlarguments['cedh']);
return $results;
}
private function _dbdatainit() {
$results = false;
$this->errormessage = ' Database structure seems not ready, trying to initialize error';
$this->sql = "ALTER TABLE datatree ADD COLUMN nodes_childs VARCHAR(40) NULL;";
$results = $results + $this->db->execute($this->sql);
$this->sql = "ALTER TABLE datatree ADD COLUMN nodes_parent VARCHAR(40) NULL;";
$results = $results + $this->db->execute($this->sql);
$this->sql = "ALTER TABLE datatree ADD COLUMN nodes_notes VARCHAR(40) NULL;";
$results = $results + $this->db->execute($this->sql);
$this->sql = "CREATE TABLE IF NOT EXISTS datatree(nodes_childs VARCHAR(40),nodes_parent VARCHAR(40) Default -1,nodes_notes VARCHAR(40),PRIMARY KEY(nodes_childs),FOREIGN KEY (nodes_parent) REFERENCES datatree(nodes_childs) ON UPDATE CASCADE);";
$results = $results + $this->db->execute($this->sql);
if( $results == 0)
return true;
return $results;
}
private function _dbdatavars() {
if( array_key_exists('cedp', $this->urlarguments)) {
$this->savedatafull = true;
if( trim($this->urlarguments['cedp']) == '') {
$this->urlarguments['cedp'] = NULL;
$this->savedatafull = false;
}
$this->savedatatodb = true;
}
if($this->savedatafull)
$sql = "INSERT INTO datatree (nodes_childs, nodes_parent) VALUES (%s, %s);";
else
$sql = "INSERT INTO datatree (nodes_childs, nodes_parent) VALUES (%s);";
$this->sql = $sql;
}
public function show($parameters = null) {
$errormessage = $this->errormessage;
$httpcode = $this->codehttpisok;
$sucess = $this->sucess;
$variables = array('sucess'=>$sucess,'message'=>$errormessage,'httpcode'=>$httpcode,'data'=>$parameters);
$this->renderOutput($variables,true);
}
}

View File

@ -1,15 +1,10 @@
<?php
/*
____ _ _
/ ___|_ _ __ _ ___| |__ (_)
| | _| | | |/ _` |/ __| '_ \| |
| |_| | |_| | (_| | (__| | | | |
\____|\__,_|\__,_|\___|_| |_|_|
Copyright (c) 2014 Díaz Víctor aka (Máster Vitronic)
Copyright (c) 2018 Díaz Víctor aka (Máster Vitronic)
<vitronic2@gmail.com> <mastervitronic@vitronic.com.ve>
*/
/**!
* @package minenux-skindb-webdb
* @filename index.php model
* @route >index
* @version 1.0
*/
class index_model extends model {
public function notFound() {

View File

@ -1,11 +1,10 @@
<?php
/**!
* @package Skindb-webdb
* @package minenux-skindb-webdb
* @filename lists.php model
* @route >lists
* @version 1.0
*/
class lists_model extends model {
public function notFound() {

View File

@ -1,15 +1,10 @@
<?php
/*
____ _ _
/ ___|_ _ __ _ ___| |__ (_)
| | _| | | |/ _` |/ __| '_ \| |
| |_| | |_| | (_| | (__| | | | |
\____|\__,_|\__,_|\___|_| |_|_|
Copyright (c) 2014 Díaz Víctor aka (Máster Vitronic)
Copyright (c) 2018 Díaz Víctor aka (Máster Vitronic)
<vitronic2@gmail.com> <mastervitronic@vitronic.com.ve>
*/
/**!
* @package minenux-skindb-webdb
* @filename notFound_model.php model
* @route >notfound
* @version 1.0
*/
class notFound_model extends model {
public function show($errormessage = 'The page could not be found or was moved, or your request is not valid, please consult upstream') {

View File

@ -1,6 +1,6 @@
<?php
/**!
* @package skindb-webdb
* @package minenux-skindb-webdb
* @filename lists.php controller
* @route >api>v1>lists
* @version 1.0

View File

@ -3,4 +3,5 @@ page[] = "index"
page[] = "lists"
page[] = "api/api"
page[] = "api/v1/lists"
page[] = "api/v1/store"