asdsad
This commit is contained in:
110
public/api/mining-checker/index.php
Normal file
110
public/api/mining-checker/index.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once dirname(__DIR__, 3) . '/src/App/bootstrap.php';
|
||||
|
||||
use App\ConfigLoader;
|
||||
use App\KeycloakAuth;
|
||||
use MiningChecker\MiningCheckerCalculator;
|
||||
use MiningChecker\MiningCheckerStore;
|
||||
use MiningChecker\MiningCheckerUserScope;
|
||||
|
||||
session_start();
|
||||
|
||||
$projectRoot = dirname(__DIR__, 3);
|
||||
$keycloakConfig = ConfigLoader::load($projectRoot, 'keycloak');
|
||||
$auth = new KeycloakAuth($keycloakConfig);
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
if (!$auth->shouldShowDesktop()) {
|
||||
http_response_code(401);
|
||||
echo json_encode(['error' => 'Nicht autorisiert.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
$store = new MiningCheckerStore($projectRoot, MiningCheckerUserScope::current());
|
||||
$calculator = new MiningCheckerCalculator();
|
||||
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
||||
|
||||
try {
|
||||
if ($method === 'GET') {
|
||||
$state = $store->load();
|
||||
$state['metrics'] = $calculator->calculate((array) ($state['settings'] ?? []));
|
||||
echo json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
$payloadRaw = file_get_contents('php://input');
|
||||
$payload = json_decode($payloadRaw !== false ? $payloadRaw : '', true);
|
||||
$payload = is_array($payload) ? $payload : [];
|
||||
|
||||
if ($method === 'PUT') {
|
||||
$state = $store->load();
|
||||
$state['settings'] = normalizeSettings(array_merge((array) ($state['settings'] ?? []), $payload));
|
||||
$store->save($state);
|
||||
$state['metrics'] = $calculator->calculate((array) $state['settings']);
|
||||
echo json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($method === 'POST') {
|
||||
$state = $store->load();
|
||||
$settings = normalizeSettings(array_merge((array) ($state['settings'] ?? []), is_array($payload['settings'] ?? null) ? $payload['settings'] : []));
|
||||
$metrics = $calculator->calculate($settings);
|
||||
$note = trim((string) ($payload['note'] ?? ''));
|
||||
$history = is_array($state['history'] ?? null) ? $state['history'] : [];
|
||||
|
||||
array_unshift($history, [
|
||||
'id' => bin2hex(random_bytes(8)),
|
||||
'created_at' => gmdate('c'),
|
||||
'project_name' => (string) ($settings['project_name'] ?? ''),
|
||||
'coin_symbol' => (string) ($settings['coin_symbol'] ?? ''),
|
||||
'currency' => (string) ($settings['currency'] ?? 'EUR'),
|
||||
'note' => $note,
|
||||
'settings' => $settings,
|
||||
'metrics' => $metrics,
|
||||
]);
|
||||
|
||||
$state['settings'] = $settings;
|
||||
$state['history'] = array_slice($history, 0, 25);
|
||||
$store->save($state);
|
||||
|
||||
$state['metrics'] = $metrics;
|
||||
echo json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Methode nicht erlaubt.'], JSON_UNESCAPED_UNICODE);
|
||||
} catch (Throwable $throwable) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $throwable->getMessage()], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $settings
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
function normalizeSettings(array $settings): array
|
||||
{
|
||||
return [
|
||||
'project_name' => trim((string) ($settings['project_name'] ?? 'DOGE Mining')),
|
||||
'coin_symbol' => strtoupper(trim((string) ($settings['coin_symbol'] ?? 'DOGE'))),
|
||||
'algorithm' => trim((string) ($settings['algorithm'] ?? 'Scrypt')),
|
||||
'currency' => strtoupper(trim((string) ($settings['currency'] ?? 'EUR'))),
|
||||
'hashrate' => (float) ($settings['hashrate'] ?? 0),
|
||||
'hashrate_unit' => trim((string) ($settings['hashrate_unit'] ?? 'MH/s')),
|
||||
'network_hashrate' => (float) ($settings['network_hashrate'] ?? 0),
|
||||
'network_hashrate_unit' => trim((string) ($settings['network_hashrate_unit'] ?? 'TH/s')),
|
||||
'block_reward' => (float) ($settings['block_reward'] ?? 0),
|
||||
'block_time_seconds' => (float) ($settings['block_time_seconds'] ?? 60),
|
||||
'coin_price' => (float) ($settings['coin_price'] ?? 0),
|
||||
'power_watts' => (float) ($settings['power_watts'] ?? 0),
|
||||
'electricity_price' => (float) ($settings['electricity_price'] ?? 0),
|
||||
'pool_fee_percent' => (float) ($settings['pool_fee_percent'] ?? 0),
|
||||
'hardware_cost' => (float) ($settings['hardware_cost'] ?? 0),
|
||||
'notes' => trim((string) ($settings['notes'] ?? '')),
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user