Implemented: - Database schema (SQLite) with tables: users, profiles, posts, swipes, matches, chats, chat_messages, events, notifications, relationships, post_likes, post_comments, refresh_tokens, media - Core PHP structure following skeleton pattern (www/api/) - JWT Authentication (register, login, refresh, logout) - User profile CRUD (show, update, delete, search) - User sub-resources (posts, friends, followers, following) - Minimal Firebase JWT implementation for PHP without Composer
103 lines
3.4 KiB
PHP
103 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* Sozial API - Entry Point
|
|
*
|
|
* This is the main entry point for the Sozial REST API.
|
|
* All requests are routed through this file.
|
|
*/
|
|
|
|
// Error reporting for development
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 0);
|
|
ini_set('log_errors', 1);
|
|
|
|
// Autoloader
|
|
spl_autoload_register(function ($class) {
|
|
// Convert namespace to file path
|
|
$prefixes = [
|
|
'Core\\' => __DIR__ . '/../src/core/',
|
|
'Modules\\' => __DIR__ . '/../src/modules/',
|
|
'Services\\' => __DIR__ . '/../src/services/',
|
|
'Utils\\' => __DIR__ . '/../src/utils/',
|
|
'Middleware\\' => __DIR__ . '/../src/core/',
|
|
];
|
|
|
|
foreach ($prefixes as $prefix => $baseDir) {
|
|
$len = strlen($prefix);
|
|
if (strncmp($prefix, $class, $len) === 0) {
|
|
$relativeClass = substr($class, $len);
|
|
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
|
|
if (file_exists($file)) {
|
|
require $file;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
// Load configurations
|
|
$dbConfig = require __DIR__ . '/../src/config/database.php';
|
|
$jwtConfig = require __DIR__ . '/../src/config/jwt.php';
|
|
$appConfig = require __DIR__ . '/../src/config/app.php';
|
|
|
|
// Initialize database
|
|
use Core\Database;
|
|
Database::init($dbConfig);
|
|
|
|
// Initialize database schema if tables don't exist
|
|
$pdo = Database::getInstance();
|
|
$stmt = $pdo->query("SELECT name FROM sqlite_master WHERE type='table' AND name='users'");
|
|
if (!$stmt->fetch()) {
|
|
$schemaPath = __DIR__ . '/../database/schema.sql';
|
|
if (file_exists($schemaPath)) {
|
|
Database::initialize($schemaPath);
|
|
}
|
|
}
|
|
|
|
// Initialize JWT service
|
|
use Services\JwtService;
|
|
$jwtService = new JwtService($jwtConfig);
|
|
|
|
// Initialize auth middleware
|
|
use Middleware\Auth;
|
|
Auth::setJwtService($jwtService);
|
|
|
|
// Create request
|
|
use Core\Request;
|
|
$request = new Request();
|
|
|
|
// Create router
|
|
use Core\Router;
|
|
$router = new Router();
|
|
|
|
// Load controllers
|
|
use Modules\Auth\AuthController;
|
|
use Modules\User\UserController;
|
|
|
|
$authController = new AuthController(Database::getInstance(), $jwtService);
|
|
$userController = new UserController(Database::getInstance());
|
|
|
|
// Auth routes
|
|
$router->post('/auth/register', fn($req) => $authController->register($req));
|
|
$router->post('/auth/login', fn($req) => $authController->login($req));
|
|
$router->post('/auth/refresh', fn($req) => $authController->refresh($req));
|
|
$router->post('/auth/logout', fn($req) => $authController->logout($req), ['Auth']);
|
|
|
|
// User routes
|
|
$router->get('/users/search', fn($req) => $userController->search($req));
|
|
$router->get('/users/{id}', fn($req, $params) => $userController->show($req, $params));
|
|
$router->patch('/users/{id}', fn($req, $params) => $userController->update($req, $params), ['Auth']);
|
|
$router->delete('/users/{id}', fn($req, $params) => $userController->destroy($req, $params), ['Auth']);
|
|
$router->get('/users/{id}/posts', fn($req, $params) => $userController->posts($req, $params));
|
|
$router->get('/users/{id}/friends', fn($req, $params) => $userController->friends($req, $params));
|
|
$router->get('/users/{id}/followers', fn($req, $params) => $userController->followers($req, $params));
|
|
$router->get('/users/{id}/following', fn($req, $params) => $userController->following($req, $params));
|
|
|
|
// Health check
|
|
$router->get('/health', function($req) {
|
|
\Core\Response::success(['status' => 'ok', 'service' => 'sozial-api']);
|
|
});
|
|
|
|
// Dispatch request
|
|
$router->dispatch($request);
|