Compare commits

...

4 commits

Author SHA1 Message Date
rottenwheel
c14de55cd1
Merge pull request #17 from cowingtonpost1/readme
readme updates
2025-03-17 04:01:50 +00:00
root
8c2cef87c2 readme updates 2025-03-16 20:58:44 -07:00
rottenwheel
eadc8d3026
Merge pull request #16 from cowingtonpost1/cow
restructure project and fix issue #12
2025-03-16 21:44:42 +00:00
cow
b5dbe19617 restructure project and fix issue #12 2025-03-16 12:29:14 -07:00
27 changed files with 537 additions and 738 deletions

11
.gitignore vendored
View file

@ -6,13 +6,14 @@
/node_modules/
# Coingecko export data
/coingecko.json
/coingecko-original.json
/coingecko-currencies.json
/storage/coingecko.json
/storage/coingecko-original.json
/storage/coingecko-currencies.json
/storage/cache/
# Secret files
/secrets.php
# Compiled files
/js/
/css/
/public/js/
/public/css/

View file

@ -1,4 +1,4 @@
![Favicon - moner.ooo](/img/favicon-152x152.png)
![Favicon - moner.ooo](/public/img/favicon-152x152.png)
# Revuo Monero Calc
@ -98,7 +98,7 @@ For an example, see [kuno.anne.media](https://kuno.anne.media/donate/onml/).
npm run build
```
4. Point your web server to the repository directory.
4. Point your web server to the public directory inside this repository.
## Configuration
@ -117,18 +117,17 @@ return [
];
```
Create a `secrets.php` file in the root directory to store CoinGecko API keys. Example:
Create a `secrets.php` file in the root directory to store CoinGecko API keys. `use_api_key` can be set to false for testing locally. Example:
```php
<?php
return [
'coingecko_api_key' => 'CG-xxxx',
'coingecko_key_is_demo' => true,
'use_api_key' => true,
];
```
**Note:** The `secrets.dist.php` file should not be accessible from the web server.
### Fetching Exchange Rates
Exchange rates are fetched from the CoinGecko API. The `coingecko.php` file handles the API requests and attempts to update exchange rates every 5 seconds. Due to the rate limits of the CoinGecko API, actual update intervals may vary and are closer to 60 seconds.

View file

@ -1,11 +1,13 @@
<?php
declare(strict_types=1);
class FileCache {
class FileCache
{
private $cacheDir;
private $defaultExpiration;
public function __construct($cacheDir = 'cache', $defaultExpiration = 60) {
public function __construct($cacheDir = '../storage/cache', $defaultExpiration = 60)
{
$this->cacheDir = $cacheDir;
$this->defaultExpiration = $defaultExpiration;
// Create the cache directory if it doesn't exist

1160
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,10 +6,11 @@ date_default_timezone_set('Europe/Berlin');
// Define currencies that should *not* be included in the list
$excludedCurrencies = ['bits', 'sats'];
require_once __DIR__ . '/cache.php';
require_once '../lib/cache.php';
// Fetch JSON data from a file and decode it
function fetchJson($filename) {
function fetchJson($filename)
{
return json_decode(file_get_contents($filename), true);
}
@ -21,7 +22,8 @@ function fetchCache(string $key, string $url, FileCache $cache)
}
// Make an API request and return the JSON response
function makeApiRequest($url) {
function makeApiRequest($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
@ -36,15 +38,19 @@ function makeApiRequest($url) {
}
// Get CoinGecko key URL parameter
function getCoinGeckoApiUrl($path, $params = []) {
$secrets = require_once 'secrets.php';
$key = $secrets['coingecko_api_key'];
function getCoinGeckoApiUrl($path, $params = [])
{
$secrets = require '../secrets.php';
$demo = $secrets['coingecko_key_is_demo'];
if ($secrets['use_api_key'] === true) {
$key = $secrets['coingecko_api_key'];
$paramName = $demo ? 'x_cg_demo_api_key' : 'x_cg_pro_api_key';
$params[$paramName] = $key;
}
$baseUrl = $demo ? "https://api.coingecko.com/api/v3/" : "https://pro-api.coingecko.com/api/v3/";
$params[$paramName] = $key;
$url = $baseUrl . $path;
if (!empty($params)) {
@ -58,8 +64,9 @@ $currentTime = time();
// Fetch list of available currencies from CoinGecko API
// Available currencies are cached for 24 hours
function fetchAvailableCurrencies() {
$cacheFile = 'coingecko-currencies.json';
function fetchAvailableCurrencies()
{
$cacheFile = dirname(__DIR__) . '/storage/coingecko-currencies.json';
$cacheTime = 86400;
// Return cached data if it exists and is less than 24 hours old
@ -75,21 +82,23 @@ function fetchAvailableCurrencies() {
return $data;
}
return null;
return $data;
}
// Fetch currency data from CoinGecko API
function fetchCurrencyData($currencies) {
$cache = new FileCache(__DIR__ . '/cache');
function fetchCurrencyData($currencies)
{
$cache = new FileCache(dirname(__DIR__) . '/storage/cache');
$apiUrl = getCoinGeckoApiUrl('simple/price', ['ids' => 'monero', 'vs_currencies' => implode(',', array_map('strtolower', $currencies))]);
return fetchCache('currency_data', $apiUrl, $cache);
}
$currencyFile = 'coingecko.json';
$originalFile = 'coingecko-original.json';
$currencyFile = dirname(__DIR__) . '/storage/coingecko.json';
$originalFile = dirname(__DIR__) . '/storage/coingecko-original.json';
// Function to process currency data
function processCurrencyData($availableCurrencies, $previousData, $currentTime, $excludedCurrencies) {
function processCurrencyData($availableCurrencies, $previousData, $currentTime, $excludedCurrencies)
{
// Remove excluded currencies
$availableCurrencies = array_diff($availableCurrencies, $excludedCurrencies);
$currencies = array_map('strtoupper', $availableCurrencies);
@ -116,7 +125,7 @@ function processCurrencyData($availableCurrencies, $previousData, $currentTime,
return null;
}
$previousData = fetchJson($currencyFile);
$previousData = file_exists($currencyFile) ? fetchJson($currencyFile) : ["time" => 0];
$output = $previousData;
// Check if five seconds have passed since the last update

View file

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

View file

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

View file

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

View file

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View file

Before

Width:  |  Height:  |  Size: 5.1 KiB

After

Width:  |  Height:  |  Size: 5.1 KiB

View file

Before

Width:  |  Height:  |  Size: 952 B

After

Width:  |  Height:  |  Size: 952 B

View file

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

Before

Width:  |  Height:  |  Size: 589 B

After

Width:  |  Height:  |  Size: 589 B

View file

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View file

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View file

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

View file

Before

Width:  |  Height:  |  Size: 671 B

After

Width:  |  Height:  |  Size: 671 B

View file

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View file

Before

Width:  |  Height:  |  Size: 3 KiB

After

Width:  |  Height:  |  Size: 3 KiB

View file

@ -4,23 +4,24 @@ header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$COINGECKO_JSON_PATH = dirname(__DIR__) . '/storage/coingecko.json';
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$currentUrl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parentUrl = dirname($currentUrl);
// Get currency data from JSON
if (!file_exists('coingecko.json')) {
// Special case: First run.
exec('php coingecko.php');
if (!file_exists($COINGECKO_JSON_PATH)) {
`php coingecko.php`;
sleep(1);
}
$api_cg = json_decode(file_get_contents('coingecko.json'), true);
$api_cg = json_decode(file_get_contents($COINGECKO_JSON_PATH), true);
// Configuration file
$config = [];
if (file_exists('config.php')) {
$config = require_once 'config.php';
if (file_exists(dirname(__DIR__) . '/config.php')) {
$config = require_once '../config.php';
}
$display_servers_guru = isset($config['servers_guru']) && $config['servers_guru'] === true;
@ -71,8 +72,8 @@ $language_code = explode('-', $lang)[0];
$language_files = ["en", $language_code, $lang];
foreach ($language_files as $language_file) {
if (file_exists('lang/' . $language_file . '.php')) {
require_once 'lang/' . $language_file . '.php';
if (file_exists('../lang/' . $language_file . '.php')) {
require_once '../lang/' . $language_file . '.php';
}
}
@ -106,5 +107,4 @@ foreach (array_reverse($preferred_currencies) as $currency) {
}
// Output the HTML
require_once 'templates/index.php';
?>
require_once '../templates/index.php';

View file

@ -1,6 +1,6 @@
<?php
return [
'coingecko_api_key' => 'CG-xxxx',
'coingecko_key_is_demo' => true,
'use_api_key' => false,
];

0
storage/.gitkeep Normal file
View file

View file

@ -1,7 +1,5 @@
<!DOCTYPE html>
<html lang="<?php echo $lang_meta; ?>" <?php if ($rtl) {
echo 'dir="rtl"';
} ?>>
<html lang="<?php echo $lang_meta ?>" <?= $rtl ? 'dir="rtl"' : '' ?>>
<head>
<meta charset="utf-8" />

View file

@ -10,7 +10,7 @@ module.exports = {
entry: './src/js/main.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'js'),
path: path.resolve(__dirname, 'public/js'),
},
module: {
rules: [
@ -29,7 +29,7 @@ module.exports = {
}),
new PurgeCSSPlugin({
paths: glob.sync([
path.join(__dirname, 'index.php'),
path.join(__dirname, 'public/index.php'),
path.join(__dirname, 'src/js/*.js'),
path.join(__dirname, 'templates/*.php'),
]),