feat: /api/data.php with fixture source, robots.txt
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Weerscherm\Cache;
|
||||
use Weerscherm\Config;
|
||||
use Weerscherm\DashboardService;
|
||||
use Weerscherm\FixtureSource;
|
||||
use Weerscherm\LiveSource;
|
||||
|
||||
$root = dirname(__DIR__, 2);
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
header('X-Robots-Tag: noindex');
|
||||
|
||||
require $root . '/vendor/autoload.php';
|
||||
|
||||
$configFile = $root . '/config.php';
|
||||
if (!is_file($configFile)) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['errors' => ['config_missing'], 'message' => 'config.php ontbreekt: kopieer config.example.php naar config.php']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$config = Config::load($configFile);
|
||||
$storage = rtrim((string) $config->get('storage_path'), '/');
|
||||
$timezone = (string) $config->get('timezone');
|
||||
$lat = $config->get('forecast_lat');
|
||||
$lon = $config->get('forecast_lon');
|
||||
|
||||
$source = $config->get('data_source') === 'live'
|
||||
? LiveSource::fromConfig($config)
|
||||
: new FixtureSource(null, $timezone);
|
||||
|
||||
$service = new DashboardService(
|
||||
$source,
|
||||
new Cache($storage . '/cache'),
|
||||
$config->get('cache_ttl'),
|
||||
$timezone,
|
||||
is_numeric($lat) ? (float) $lat : null,
|
||||
is_numeric($lon) ? (float) $lon : null,
|
||||
is_array($config->get('indoor_modules')) ? $config->get('indoor_modules') : [],
|
||||
is_string($config->get('netatmo_device_id')) && $config->get('netatmo_device_id') !== '' ? $config->get('netatmo_device_id') : null,
|
||||
);
|
||||
|
||||
echo json_encode($service->build(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
} catch (Throwable $e) {
|
||||
error_log('weerscherm: ' . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['errors' => ['server_error']]);
|
||||
}
|
||||
Reference in New Issue
Block a user