Commit f84e123d authored by CUNY Florian's avatar CUNY Florian
Browse files

WORKS, IT DOES. IN THE FORCE, BELIEVE, YOU MUST.

parent 0bc531ad
APP_NAME=foogood
APP_PORT=8080
APP_DB_ADMIN_PORT=8081
DB_PORT=33016
MYSQL_ROOT_PASS=superSecr3t
MYSQL_USER=app_user
MYSQL_PASS=t3rceS
MYSQL_DB=foogood
TOKEN_PASSPHRASE=iutsd
<?php
require_once __DIR__ . '/../db/DBConnection.php';
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
$app->get('/api/users', function( Request $request, Response $response){
$sql = "select * from users order by id";
try
{
try {
$dbconn = new DB\DBConnection();
$db = $dbconn->connect();
// query
$stmt = $db->query( $sql );
$users = $stmt->fetchAll( PDO::FETCH_OBJ );
$db = null; // clear db object
// print out the result as json format
//echo json_encode( $books );
$response->getBody()->write(json_encode( $users ));
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
} catch( PDOException $e ) {
// show error message as Json format
//echo '{"error": {"msg": ' . $e->getMessage() . '}';
$response->getBody()->write('{"error": {"msg": "' . $e->getMessage() . '"}}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
}
catch (Exception $e)
{
$response->getBody()->write('{"error": {"msg": "' . $e->getMessage() . '"}}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(401);
}
});
<?php namespace DB;
use \PDO;
/**
* Connect MySQL with PDO - Helper class
*
* (Use environment variables)
*/
class DBConnection {
private $dbhost ;
private $dbuser ;
private $dbpass ;
private $dbname ;
public function __construct()
{
$this->dbhost = 'db_server';
$this->dbuser = getenv("MYSQL_USER");
$this->dbpass = getenv("MYSQL_PASSWORD");
$this->dbname = getenv("MYSQL_DATABASE");
}
public function connect() {
$prepare_conn_str = "mysql:host=$this->dbhost;dbname=$this->dbname";
$dbConn = new \PDO( $prepare_conn_str, $this->dbuser, $this->dbpass );
// https://www.php.net/manual/en/pdo.setattribute.php
$dbConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// return the database connection back
return $dbConn;
}
}
<?php
// Include the Slim framework
require __DIR__ . '/../vendor/autoload.php';
// Use Namespaces for HTTP request
// # use Namespaces for HTTP request
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
// # include the Slim framework
require __DIR__ . '/../vendor/autoload.php';
// Create config params array
$config['displayErrorDetails'] = true;
$config['addContentLengthHeader'] = false;
// Create new Slim instance applying settings
// # create new Slim instance
$app = new \Slim\App(
[
'settings' => $config
'settings' => $config,
'upload_directory' => __DIR__ . '/uploads'
]
);
// The root route
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write("Welcome in FooGood !");
return $response;
});
// Run the API
// # include Users route
require __DIR__ . '/../apiroutes/users.php';
$app->run();
-- phpMyAdmin SQL Dump
-- version 5.1.1
-- https://www.phpmyadmin.net/
--
-- Hôte : db_server
-- Généré le : dim. 31 oct. 2021 à 12:00
-- Version du serveur : 10.5.9-MariaDB-1:10.5.9+maria~focal
-- Version de PHP : 7.4.20
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Base de données : `foogood`
--
-- --------------------------------------------------------
--
-- Structure de la table `users`
--
CREATE TABLE `users` (
`id` int(10) UNSIGNED NOT NULL,
`first_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`admin` tinyint(1) DEFAULT 0,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Déchargement des données de la table `users`
--
INSERT INTO `users` (`id`, `first_name`, `last_name`, `username`, `password`, `admin`, `created_at`, `updated_at`) VALUES
(2, 'Maitre', 'Yoda', 'm.yoda@force.you', 'Fear is the path to the dark side. Fear leads to anger. Anger leads to hate. Hate leads to suffering', 1, '2021-05-04 18:30:00', '2021-08-30 09:37:30'),
(6, 'Johnny', 'Doey', 'john.doe@unknown.fr', 'password', 0, '2021-08-30 09:36:17', '2021-08-30 09:40:28');
--
-- Index pour les tables déchargées
--
--
-- Index pour la table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `users_email_unique` (`username`);
--
-- AUTO_INCREMENT pour les tables déchargées
--
--
-- AUTO_INCREMENT pour la table `users`
--
ALTER TABLE `users`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
[mysqld]
collation-server = utf8mb4_unicode_ci
character-set-server = utf8mb4
version: '3'
networks:
foogood_net:
services:
server: # Nom du micro service (server = service web)
build: # Instruction pour build l'image
context: ./build
dockerfile: Dockerfile
container_name: '${APP_NAME}-server' # Nom du container
restart: always # Politique de redémarrage lorsque le serveur redémarre
ports:
- '${APP_PORT}:80'
working_dir: /var/www/html
environment:
TZ: "Europe/Paris"
MYSQL_USER: '${MYSQL_USER}'
MYSQL_PASSWORD: '${MYSQL_PASS}'
MYSQL_DATABASE: '${MYSQL_DB}'
volumes:
- ./app:/var/www/html
- ./web/000-default.conf:/etc/apache2/sites-available/000-default.conf
- ./web/php.ini:/usr/local/etc/php/conf.d/extra-php-config.ini
depends_on:
- db_server # Pour forcer l'ordre de démarrage des conteneurs
networks:
- foogood_net # Spécifie le même réseau virtuel que les autres conteneurs
db_server: # Nom du micro service de la BDD
image: mariadb:10.5.9 # Basé sur une image officielle mariadb (dockerhub)
container_name: '${APP_NAME}-db' # Nom du container
restart: always # Politique de redémarrage lorsque le serveur redémarre
ports:
- '${DB_PORT}:3306' # Port mysql
environment: # variables d'environnement visibles à l'intérieur du conteneur "db_server" (requis pour mariadb)
MYSQL_ROOT_PASSWORD: '${MYSQL_ROOT_PASS}'
MYSQL_USER: '${MYSQL_USER}'
MYSQL_PASSWORD: '${MYSQL_PASS}'
MYSQL_DATABASE: '${MYSQL_DB}'
volumes:
- ./db/data/:/var/lib/mysql # Le volume qui contiendra toutes les données persistentes de la BDD
- ./db/my.cnf:/etc/mysql/conf.d/my.cnf # fichier de conf mariadb
- ./db/init_schema.sql:/docker-entrypoint-initdb.d/init_schema.sql # script d'initialisation de la BDD (lancé au démarrage de mariadb)
networks:
- foogood_net # Spécifie le même réseau virtuel que les autres conteneurs
db_admin: # Nom du micro service PhpMyAdmin
image: phpmyadmin/phpmyadmin:5 # Basé sur une image officielle phpmyadmin (dockerhub)
container_name: '${APP_NAME}-db-admin' # Nom du container
restart: always # Politique de redémarrage lorsque le serveur redémarre
ports:
- '${APP_DB_ADMIN_PORT}:80' # Port d'accès à la page web d'admin de PhpMyAdmin
environment:
PMA_HOST: db_server # Hostname (nom du micro-service dans la stack) du conteneur de BDD
depends_on:
- db_server # Pour forcer l'ordre de démarrage des conteneurs
networks:
- foogood_net # Spécifie le même réseau virtuel que les autres conteneurs
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment