Switched Uiharu config to Syokuhou format.

This commit is contained in:
Pachira 2023-12-15 01:39:11 +00:00
parent 4089efbbdf
commit d74457a140
6 changed files with 17 additions and 45 deletions

1
.gitignore vendored
View file

@ -3,6 +3,7 @@
.DS_Store
/.debug
/config.ini
/uiharu.cfg
/public/robots.txt
/lib/index-dev
/vendor

View file

@ -10,7 +10,7 @@ $ctx->registerLookup(new \Uiharu\Lookup\EEPROMLookup('eeprom', 'eeprom.flashii.n
if(UIH_DEBUG)
$ctx->registerLookup(new \Uiharu\Lookup\EEPROMLookup('devrom', 'eeprom.edgii.net', ['i.edgii.net']));
$ctx->registerLookup(new \Uiharu\Lookup\YouTubeLookup);
$ctx->registerLookup(new \Uiharu\Lookup\YouTubeLookup($cfg->scopeTo('google')));
$ctx->registerLookup(new \Uiharu\Lookup\NicoNicoLookup);
// this should always come AFTER other lookups involved http(s)

View file

@ -1,25 +0,0 @@
<?php
namespace Uiharu;
final class Config {
private static array $config = [];
public static function load(string $path): void {
$config = parse_ini_file($path, true, INI_SCANNER_TYPED);
if(!empty($config))
self::$config = array_merge(self::$config, $config);
}
public static function get(string $section, string $key, $default = null) {
if(!self::has($section, $key))
return $default;
return self::$config[$section][$key];
}
public static function has(string $section, string $key) {
return array_key_exists($section, self::$config)
&& array_key_exists($key, self::$config[$section])
&& !empty(self::$config[$section][$key]);
}
}

View file

@ -2,6 +2,7 @@
namespace Uiharu\Lookup;
use RuntimeException;
use Syokuhou\IConfig;
use Uiharu\Config;
use Uiharu\Url;
@ -34,6 +35,8 @@ final class YouTubeLookup implements \Uiharu\ILookup {
return in_array($host, self::SHORT_DOMAINS);
}
public function __construct(private IConfig $config) {}
public function match(Url $url): bool {
if(!$url->isWeb())
return false;
@ -69,7 +72,7 @@ final class YouTubeLookup implements \Uiharu\ILookup {
}
private function lookupVideo(string $videoId): ?object {
$curl = curl_init("https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id={$videoId}&key=" . Config::get('Google', 'apiKey'));
$curl = curl_init("https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id={$videoId}&key=" . $this->config->getString('api_key'));
curl_setopt_array($curl, [
CURLOPT_AUTOREFERER => false,
CURLOPT_CERTINFO => false,

View file

@ -3,15 +3,18 @@ namespace Uiharu;
use Index\Data\IDbConnection;
use Index\Http\HttpFx;
use Syokuhou\IConfig;
final class UihContext {
private IDbConnection $database;
private IConfig $config;
private HttpFx $router;
private array $apis = [];
private array $lookups = [];
public function __construct(IDbConnection $database) {
public function __construct(IDbConnection $database, IConfig $config) {
$this->database = $database;
$this->config = $config;
}
public function getDatabase(): IDbConnection {
@ -28,7 +31,7 @@ final class UihContext {
if($origin === $_SERVER['HTTP_HOST'])
return true;
$allowed = Config::get('CORS', 'origins', []);
$allowed = $this->config->getArray('cors:origins');
if(empty($allowed))
return true;

View file

@ -2,8 +2,8 @@
namespace Uiharu;
use Index\Environment;
use Index\Data\ConnectionFailedException;
use Index\Data\DbTools;
use Syokuhou\SharpConfig;
define('UIH_STARTUP', microtime(true));
define('UIH_ROOT', __DIR__);
@ -11,7 +11,7 @@ define('UIH_DEBUG', is_file(UIH_ROOT . '/.debug'));
define('UIH_PUBLIC', UIH_ROOT . '/public');
define('UIH_SOURCE', UIH_ROOT . '/src');
define('UIH_LIBRARY', UIH_ROOT . '/lib');
define('UIH_VERSION', '20231021');
define('UIH_VERSION', '20231215');
require_once UIH_ROOT . '/vendor/autoload.php';
@ -20,19 +20,9 @@ Environment::setDebug(UIH_DEBUG);
mb_internal_encoding('utf-8');
date_default_timezone_set('utc');
$configPath = UIH_ROOT . '/config.ini';
if(!is_file($configPath))
die('Uiharu configuration is missing.');
$cfg = SharpConfig::fromFile(UIH_ROOT . '/uiharu.cfg');
Config::load($configPath);
if(!Config::has('Database', 'dsn'))
die('Uiharu database is not configured.');
$db = DbTools::create($cfg->getString('database:dsn', 'null:'));
$db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';');
try {
$db = DbTools::create(Config::get('Database', 'dsn'));
} catch(ConnectionFailedException $ex) {
echo '<h3>Unable to connect to database</h3>';
die($ex->getMessage());
}
$ctx = new UihContext($db);
$ctx = new UihContext($db, $cfg);