Skip to content

Commit

Permalink
add webserver for future api user validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Prado committed Apr 2, 2021
1 parent 6c98055 commit cf2229e
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 24 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# PlantMonitor "Real Plant Book" :chart_with_downwards_trend: :seedling:
Version v0.1-alpha
Version v1.0.0

### Importante
- Este proyecto está financiado 100% por mi mísmo. (Por tal razón crece lentamente).
- Esta es la versión v0.1-alpha. Si bien es completamente funcional, algunas partes del sistema no tienen el mejor diseño ni el código más elegante.
- Esta es la versión v1.0.0. Si bien es completamente funcional, algunas partes del sistema no tienen el mejor diseño ni el código más elegante.
- Sientete libre de "forckear" y mejorar. O si crees que puedes ayudarme a mejorar el código, bienvenido seas!

- <b>Si crees que es interesante para tu jardín, huerta o invernadero por favor contactame, seguramente podremos hacer algo fantástico juntos!</b>
Expand All @@ -22,12 +22,13 @@ El sistema también cuenta con una simple API que puede ser consultada públicam

### En el futuro:
- Documentar con mayor detalle y traducir a ingles <b>[On going]</b>
- Generar un pequeño invernadero con control de ventilación. <b>[On going]</b>
- Implementar el uso de paneles solares (100% autosustentable). <b>[Pending...]</b>
- Generar un pequeño invernadero con control de ventilación. <b>[Pending]</b>
- Implementar el uso de paneles solares (100% autosustentable). <b>[Investigando...]</b>
- Generar el sistema de riego automático. <b>[Pending...]</b>
- Generar un Plant Book más completo. <b>[Pending...]</b>
- Implementar el uso en Hidroponia. <b>[Pending...]</b>
- Implementar desarrollo a una escala mayor. <b>[Pending...]</b>
- Implementar sistema de usuarios para api. <b>[On going...]</b>

<br>

Expand Down
5 changes: 2 additions & 3 deletions infra/API/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,20 @@ def hello():


@app.route('/api/sensor', methods=['GET'])
#@requires_auth
@requires_auth
def get_temp():
fields = str(request.args.get('fields'))
Qlimit = int(request.args.get('limit'))
if(Qlimit > 20):
return "Sorry: limit must be less than 20"
else:
dbClient = InfluxDBClient(host, port, user, password, dbname)
DBquery = dbClient.query('SELECT '+fields+' FROM "IoT" ORDER BY time DESC LIMIT '+str(Qlimit))
DBquery = dbClient.query('SELECT '+fields+' FROM '+fields+' ORDER BY time DESC LIMIT '+str(Qlimit))
Qoutput = DBquery.raw
print(Qoutput)
return Qoutput




if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True) # Open a webserver on port 8090
51 changes: 46 additions & 5 deletions infra/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ services:
image: eclipse-mosquitto:1.5.8
container_name: mosquitto
volumes:
- ./srv/mosquitto/:/etc/mosquitto/:ro
- ./srv/mosquitto/:/etc/mosquitto/:rw
ports:
- "1883:1883"
networks:
- fullinfra
#----------------- SAVE DATA -----------------------------
#----------------- INFLUXDB -----------------------------
influxdb:
image: influxdb:1.7.10
container_name: influxdb
ports:
- "8083"
- "8086"
- "8083:8083"
- "8086:8086"
- "8090"
volumes:
- ./srv/influxdb/data:/var/lib/influxdb:rw
Expand Down Expand Up @@ -52,7 +52,7 @@ services:
- ./grafana/provisioning/:/etc/grafana/provisioning/:ro
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_SECURITY_ADMIN_PASSWORD=Z4rleng4.Sup3rS3rv3r-1.!
- GF_GRAFANA_PLUGIN_LIST="pierosavi-imageit-panel"
networks:
- fullinfra
Expand Down Expand Up @@ -83,6 +83,47 @@ services:
restart: 'always'
networks:
- fullinfra
#---------------------- MYSQL ------------------------------
mysql:
image: mariadb:10.1.19
container_name: mysql
ports:
- "3306"
restart: 'always'
volumes:
- ./webserver/code/dump.sql:/docker-entrypoint-initdb.d/dump.sql
environment:
MYSQL_DATABASE: 'test'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'admin'
MYSQL_ROOT_PASSWORD: 'admin'
networks:
- fullinfra
#---------------------- PHP ------------------------------
php:
build:
context: php/
container_name: php
volumes:
- ./webserver/code/:/usr/share/nginx/html:ro
restart: 'always'
networks:
- fullinfra
#---------------------- HTTP ------------------------------
nginx:
image: nginx
container_name: nginx
ports:
- "80:80"
links:
- mysql
- php
volumes:
- ./webserver/code/:/usr/share/nginx/html:ro
- ./webserver/conf/site.conf:/etc/nginx/conf.d/default.conf:ro
restart: 'always'
networks:
- fullinfra
#-------------------- NETWORK ----------------------------
networks:
fullinfra:
Expand Down
2 changes: 2 additions & 0 deletions infra/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM php:7.2.7-fpm-alpine3.7
RUN apk update; apk upgrade; docker-php-ext-install mysqli; docker-php-ext-enable mysqli
32 changes: 32 additions & 0 deletions infra/webserver/code/add.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<html>
<head>
<title>Add Data</title>
</head>

<body>
<button><a href="index.php">Home</a></button>
<br/><br/>

<form action="add.php" method="post" name="form1">
<table width="25%" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>data1</td>
<td><input type="text" name="data1"></td>
</tr>
<tr>
<td>data2</td>
<td><input type="text" name="data2"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Add"></td>
</tr>
</table>
</form>
</body>
</html>

46 changes: 46 additions & 0 deletions infra/webserver/code/add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<html>
<head>
<title>Add Data</title>
</head>

<body>
<?php
//including the database connection file
include_once("config.php");

if(isset($_POST['Submit'])) {
$name = mysqli_real_escape_string($mysqli, $_POST['name']);
$data1 = mysqli_real_escape_string($mysqli, $_POST['data1']);
$data2 = mysqli_real_escape_string($mysqli, $_POST['data2']);

// checking empty fields
if(empty($name) || empty($data1) || empty($data2)) {

if(empty($name)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}

if(empty($data1)) {
echo "<font color='red'>Age field is empty.</font><br/>";
}

if(empty($data2)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}

//link to the previous pdata1
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// if all the fields are filled (not empty)

//insert data to database
$result = mysqli_query($mysqli, "INSERT INTO users(name,data1,data2) VALUES('$name','$data1','$data2')");

//display success messdata1
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
}
?>
</body>
</html>
11 changes: 11 additions & 0 deletions infra/webserver/code/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$databaseHost = 'mysql';
$databaseName = 'test';
$databaseUsername = 'admin';
$databasePassword = 'admin';
$databasePort = '3306';

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName, $databasePort);

?>
14 changes: 14 additions & 0 deletions infra/webserver/code/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
//including the database connection file
include("config.php");

//getting id of the data from url
$id = $_GET['id'];

//deleting the row from table
$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");

//redirecting to the display page (index.php in our case)
header("Location:index.php");
?>

18 changes: 18 additions & 0 deletions infra/webserver/code/dump.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use test;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`data1` varchar(20) DEFAULT NULL,
`data2` varchar(20) DEFAULT NULL,
`data3` varchar(20) DEFAULT NULL,
`data4` varchar(20) DEFAULT NULL,
`data5` varchar(20) DEFAULT NULL,
`data6` varchar(20) DEFAULT NULL,
`data7` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

LOCK TABLES `users` WRITE;
INSERT INTO `users` VALUES (1,'test01','test01','test01',NULL,NULL,NULL,NULL,NULL),(2,'test02','test02','test02',NULL,NULL,NULL,NULL,NULL);
UNLOCK TABLES;
81 changes: 81 additions & 0 deletions infra/webserver/code/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
// including the database connection file
include_once("config.php");

if(isset($_POST['update']))
{

$id = mysqli_real_escape_string($mysqli, $_POST['id']);

$name = mysqli_real_escape_string($mysqli, $_POST['name']);
$data1 = mysqli_real_escape_string($mysqli, $_POST['data1']);
$data2 = mysqli_real_escape_string($mysqli, $_POST['data2']);

// checking empty fields
if(empty($name) || empty($data1) || empty($data2)) {

if(empty($name)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}

if(empty($data1)) {
echo "<font color='red'>data1 field is empty.</font><br/>";
}

if(empty($data2)) {
echo "<font color='red'>data2 field is empty.</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($mysqli, "UPDATE users SET name='$name',data1='$data1',data2='$data2' WHERE id=$id");

//redirectig to the display pdata1. In our case, it is index.php
header("Location: index.php");
}
}
?>
<?php
//getting id from url
$id = $_GET['id'];

//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");

while($res = mysqli_fetch_array($result))
{
$name = $res['name'];
$data1 = $res['data1'];
$data2 = $res['data2'];
}
?>
<html>
<head>
<title>Edit Data</title>
</head>

<body>
<a href="index.php">Home</a>
<br/><br/>

<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $name;?>"></td>
</tr>
<tr>
<td>data1</td>
<td><input type="text" name="data1" value="<?php echo $data1;?>"></td>
</tr>
<tr>
<td>data2</td>
<td><input type="text" name="data2" value="<?php echo $data2;?>"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
1 change: 1 addition & 0 deletions infra/webserver/code/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
holaaa :)
36 changes: 36 additions & 0 deletions infra/webserver/code/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
//including the database connection file
include_once("config.php");

//fetching data in descending order (lastest entry first)
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); // using mysqli_query instead
?>

<html>
<head>
<title>Homepage</title>
</head>

<body>
<button><a href="add.html">Add New Data</a></button><button><a href="localhost:3000" target="_blank">Graph</a></button><br/><br/><br/><br/>

<table width='80%' border=1>

<tr bgcolor='#CCCCCC'>
<td>Name</td>
<td>data1</td>
<td>data2</td>
<td>Update</td>
</tr>
<?php
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['data1']."</td>";
echo "<td>".$res['data2']."</td>";
echo "<td><button><a href=\"edit.php?id=$res[id]\">Edit</a></button> <button><a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></button></td>";
}
?>
</table>
</body>
</html>
2 changes: 2 additions & 0 deletions infra/webserver/code/info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
echo phpinfo();
Loading

0 comments on commit cf2229e

Please sign in to comment.