diff --git a/Dockerfile b/Dockerfile index 46031c6..e68aabd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,7 @@ RUN ./node_modules/.bin/tailwindcss -i ./assets/css/app.css -o ./public/build/ap FROM php:8.3-apache RUN a2enmod rewrite +RUN docker-php-ext-install pdo_mysql COPY . /var/www/html COPY --from=builder /app/public/build /var/www/html/public/build RUN sed -i 's|/var/www/html|/var/www/html/public|g' \ diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..01e90f3 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,40 @@ +services: + app: + build: + context: . + dockerfile: Dockerfile + container_name: pawra-app + ports: + - "8080:80" + environment: + DB_HOST: db + DB_PORT: 3306 + DB_NAME: pawra + DB_USER: pawra + DB_PASS: pawra_pw + depends_on: + db: + condition: service_healthy + + db: + image: mysql:8.4 + container_name: pawra-db + restart: unless-stopped + environment: + MYSQL_ROOT_PASSWORD: root_pw + MYSQL_DATABASE: pawra + MYSQL_USER: pawra + MYSQL_PASSWORD: pawra_pw + ports: + - "3306:3306" + volumes: + - db_data:/var/lib/mysql + - ./migrations/schema.sql:/docker-entrypoint-initdb.d/01-schema.sql:ro + healthcheck: + test: ["CMD-SHELL", "mysqladmin ping -h localhost -uroot -proot_pw || exit 1"] + interval: 5s + timeout: 3s + retries: 20 + +volumes: + db_data: \ No newline at end of file diff --git a/migrations/schema.sql b/migrations/schema.sql new file mode 100644 index 0000000..151bd78 --- /dev/null +++ b/migrations/schema.sql @@ -0,0 +1,23 @@ +CREATE DATABASE IF NOT EXISTS auth_system + CHARACTER SET utf8mb4 + COLLATE utf8mb4_unicode_ci; + +USE auth_system; + +CREATE TABLE IF NOT EXISTS users ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(30) NOT NULL UNIQUE, + email VARCHAR(255) NOT NULL UNIQUE, + password_hash VARCHAR(255) NOT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_login DATETIME NULL, + is_active TINYINT(1) NOT NULL DEFAULT 1 +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE IF NOT EXISTS remember_tokens ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + user_id INT UNSIGNED NOT NULL, + token_hash VARCHAR(255) NOT NULL, + expires_at DATETIME NOT NULL, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; \ No newline at end of file diff --git a/public/index.php b/public/index.php index 52294be..48da667 100644 --- a/public/index.php +++ b/public/index.php @@ -8,8 +8,20 @@ $autoloader->register(); use src\Container; use src\Router; use src\Middleware\TestMiddleware; +use src\Database; $container = new Container(); + +$container->bind(Database::class, function() { + return new Database( + $_ENV['DB_HOST'] ?? '127.0.0.1', + $_ENV['DB_PORT'] ?? '3306', + $_ENV['DB_NAME'] ?? 'pawra', + $_ENV['DB_USER'] ?? 'root', + $_ENV['DB_PASS'] ?? '' + ); +}); + $router = new Router($container); $router->addGlobalMiddleware(TestMiddleware::class); diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 43dff04..bf98930 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -1,12 +1,23 @@ db->pdo()->query('SELECT NOW() as now'); + $row = $stmt->fetch(); + $this->render('home', [ - 'title' => 'meow :3' + 'title' => 'meow :3', + 'now' => $row['now'] ?? null, ]); } } \ No newline at end of file diff --git a/src/Database.php b/src/Database.php new file mode 100644 index 0000000..f54b90b --- /dev/null +++ b/src/Database.php @@ -0,0 +1,30 @@ +pdo = new PDO($dsn, $user, $password, [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]); + } + + public function pdo(): PDO + { + return $this->pdo; + } +} \ No newline at end of file diff --git a/templates/home.php b/templates/home.php index e9d46e7..50ef4ce 100644 --- a/templates/home.php +++ b/templates/home.php @@ -9,6 +9,8 @@ ob_start(); +
= htmlspecialchars($now) ?>
+