-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
98 lines (80 loc) · 3.37 KB
/
index.php
File metadata and controls
98 lines (80 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
date_default_timezone_set('America/Santiago');
// requires manual (sin Composer)
require __DIR__ . '/src/Util.php';
require __DIR__ . '/src/Cache.php';
require __DIR__ . '/src/SteamClient.php';
require __DIR__ . '/src/Render.php';
//require __DIR__ . '/src/Locale.php';
$config = require __DIR__ . '/config/config.php';
function json_out($payload, int $code = 200) {
Render::json($payload, $code); exit;
}
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
function read_common_params(array $config): array {
$cc = strtolower(Util::get('cc', $config['default_cc']));
$lang = strtolower(Util::get('l', $config['default_lang']));
$limit = max(1, min(200, (int) Util::get('limit', 50)));
$offset = max(0, (int) Util::get('offset', 0));
$minDiscount = max(0, (int) Util::get('min_discount', $config['min_discount']));
return compact('cc','lang','limit','offset','minDiscount');
}
$cache = new Cache(__DIR__ . '/storage/cache', (int)$config['cache_ttl']);
$client = new SteamClient($config);
function load_featured(Cache $cache, SteamClient $client, string $cc, string $lang): ?array {
$key = "featuredcategories:cc={$cc};l={$lang}";
$data = $cache->get($key);
if (!$data) {
$data = $client->getFeaturedCategories($cc, $lang);
if ($data) $cache->set($key, $data);
}
return $data;
}
// Ruta real ignorando la subcarpeta (p.ej. /steamApi)
$reqPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '/';
$basePath = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\'); // "/steamApi"
$route = ($basePath && strpos($reqPath, $basePath) === 0)
? substr($reqPath, strlen($basePath))
: $reqPath;
$path = rtrim($route, '/');
if ($path === '') { $path = '/'; }
$params = read_common_params($config);
// Health
if ($path === '' || $path === '/health') {
header('Content-Type: text/plain; charset=utf-8');
echo 'OK '.date('c');
exit;
}
// /api/v1/*
if (strpos($path, '/api/v1') === 0) {
$data = load_featured($cache, $client, $params['cc'], $params['lang']);
if (!$data) json_out(['ok'=>false,'error'=>'Steam no respondió (featuredcategories).'], 502);
$meta = [
'version' => 'v1',
'cc' => $params['cc'],
'language' => $params['lang'],
'limit' => $params['limit'],
'offset' => $params['offset'],
'cache_ttl' => $config['cache_ttl'],
'source' => 'featuredcategories (no oficial)',
'generated_at' => date('c'),
];
if ($path === '/api/v1/deals') {
$all = SteamClient::extractSpecials($data, $params['minDiscount']);
$total = count($all);
$slice = array_slice($all, $params['offset'], $params['limit']);
json_out(['ok'=>true, 'meta'=>$meta + ['min_discount'=>$params['minDiscount'], 'total'=>$total], 'data'=>$slice]);
}
if ($path === '/api/v1/free') {
$all = SteamClient::extractTopFree($data);
$total = count($all);
$slice = array_slice($all, $params['offset'], $params['limit']);
json_out(['ok'=>true, 'meta'=>$meta + ['total'=>$total], 'data'=>$slice]);
}
json_out(['ok'=>false,'error'=>'Endpoint no encontrado.'], 404);
}
// Si llegaste acá, no hay HTML de demo; responde 404
json_out(['ok'=>false,'error'=>'Not found'], 404);