Skip to content

Commit

Permalink
index.php and config rewritten to the acdhOeaw\arche\lib\dissCache\Se…
Browse files Browse the repository at this point in the history
…rvice
  • Loading branch information
zozlak committed Oct 24, 2024
1 parent 312edb1 commit c947dae
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 117 deletions.
36 changes: 18 additions & 18 deletions build/config/arche.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
db: sqlite:/var/www/html/db.sqlite
log:
file: /var/www/html/log
level: debug
cache:
dissCacheService:
db: sqlite:/var/www/html/db.sqlite
log:
file: /var/www/html/log
level: debug
ttl:
resource: 3600 # 1 hour
response: 31536000 # 1 year
repoDb:
- archeProd.yaml
- archeCur.yaml
allowedNmsp:
- https://id.acdh.oeaw.ac.at/
- https://arche.acdh.oeaw.ac.at/api/
- https://arche-curation.acdh-dev.oeaw.ac.at/api/
- https://arche-dev.acdh-dev.oeaw.ac.at.at/api/
- https://hdl.handle.net/
repoDb:
- archeProd.yaml
- archeCur.yaml
allowedNmsp:
- https://id.acdh.oeaw.ac.at/
- https://arche.acdh.oeaw.ac.at/api/
- https://arche-curation.acdh-dev.oeaw.ac.at/api/
- https://arche-dev.acdh-dev.oeaw.ac.at.at/api/
- https://hdl.handle.net/
metadataMode: parents
parentProperty: ""
resourceProperties: []
relativesProperties: []
biblatex:
metadataMode: parents
parentProperty: ""
resourceProperties: []
relativesProperties: []
defaultLang: en
biblatexProperty: https://vocabs.acdh.oeaw.ac.at/schema#hasCustomCitation
etal: etal
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"guzzlehttp/guzzle": "^7",
"acdh-oeaw/arche-lib": "^7",
"renanbr/bibtex-parser": "^2.1",
"acdh-oeaw/arche-diss-cache": "^0.2"
"acdh-oeaw/arche-diss-cache": "^0.4"
},
"autoload": {
"psr-4": {
Expand Down
83 changes: 11 additions & 72 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,82 +24,21 @@
* THE SOFTWARE.
*/

use zozlak\logging\Log;
use acdhOeaw\arche\lib\RepoDb;
use acdhOeaw\arche\lib\SearchConfig;
use acdhOeaw\arche\lib\RepoResourceInterface;
use acdhOeaw\arche\lib\exception\NotFound;
use acdhOeaw\arche\lib\dissCache\CachePdo;
use acdhOeaw\arche\lib\dissCache\ResponseCache;
use acdhOeaw\arche\lib\dissCache\RepoWrapperGuzzle;
use acdhOeaw\arche\lib\dissCache\RepoWrapperRepoInterface;
use acdhOeaw\arche\lib\dissCache\Service;
use acdhOeaw\arche\biblatex\Resource;
use acdhOeaw\arche\biblatex\BiblatexException;

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With, Content-Type');

require_once 'vendor/autoload.php';

$config = json_decode(json_encode(yaml_parse_file(__DIR__ . '/config.yaml')));

$logId = sprintf("%08d", rand(0, 99999999));
$tmpl = "{TIMESTAMP}:$logId:{LEVEL}\t{MESSAGE}";
$log = new Log($config->log->file, $config->log->level, $tmpl);
try {
$t0 = microtime(true);

$id = $_GET['id'] ?? 'no identifer provided';
$log->info("Getting biblatex for $id");
$allowed = false;
foreach ($config->allowedNmsp as $i) {
if (str_starts_with($id, $i)) {
$allowed = true;
break;
}
}
if (!$allowed) {
throw new BiblatexException("Requested resource $id not in allowed namespace", 400);
}

$cache = new CachePdo($config->db);

$repos = [];
foreach ($config->repoDb ?? [] as $i) {
$repos[] = new RepoWrapperRepoInterface(RepoDb::factory($i), true);
}
$repos[] = new RepoWrapperGuzzle(false);

$searchConfig = new SearchConfig();
$searchConfig->metadataMode = $config->biblatex->metadataMode ?? RepoResourceInterface::META_RESOURCE;
$searchConfig->metadataParentProperty = $config->biblatex->parentProperty ?? '';
$searchConfig->resourceProperties = $config->biblatex->resourceProperties ?? [];
$searchConfig->relativesProperties = $config->biblatex->relativesProperties ?? [];

$clbck = fn($res, $param) => Resource::cacheHandler($res, $param, $config->biblatex, $log);
$ttl = $config->cache->ttl;
$cache = new ResponseCache($cache, $clbck, $ttl->resource, $ttl->response, $repos, $searchConfig, $log);

$param = [
$_GET['lang'] ?? $config->biblatex->defaultLang,
$_GET['override'] ?? null,
];
$response = $cache->getResponse($param, $id);
$response->send();
$log->info("Ended in " . round(microtime(true) - $t0, 3) . " s");
} catch (\Throwable $e) {
$code = $e->getCode();
$ordinaryException = $e instanceof BiblatexException || $e instanceof NotFound;
$logMsg = "$code: " . $e->getMessage() . ($ordinaryException ? '' : "\n" . $e->getFile() . ":" . $e->getLine() . "\n" . $e->getTraceAsString());
$log->error($logMsg);

if ($code < 400 || $code >= 500) {
$code = 500;
}
http_response_code($code);
if ($ordinaryException) {
echo $e->getMessage() . "\n";
} else {
echo "Internal Server Error\n";
}
}
$service = new Service(__DIR__ . '/config.yaml');
$config = $service->getConfig();
$clbck = fn($res, $param) => Resource::cacheHandler($res, $param, $config->biblatex, $service->getLog());
$service->setCallback($clbck);
$param = [
$_GET['lang'] ?? $config->biblatex->defaultLang,
$_GET['override'] ?? null,
];
$response = $service->serveRequest($_GET['id'] ?? '', $param);
$response->send();
27 changes: 23 additions & 4 deletions src/acdhOeaw/arche/biblatex/BiblatexException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
<?php

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
* The MIT License
*
* Copyright 2024 Austrian Centre for Digital Humanities.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace acdhOeaw\arche\biblatex;
Expand All @@ -12,6 +31,6 @@
*
* @author zozlak
*/
class BiblatexException extends \Exception {
//put your code here
class BiblatexException extends \acdhOeaw\arche\lib\dissCache\ServiceException {

}
12 changes: 6 additions & 6 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@ private function getCache(): ResponseCache {
foreach (glob('/tmp/cachePdo_*') as $i) {
unlink($i);
}
$cfg = self::$cfg->dissCacheService;
$db = new CachePdo('sqlite::memory:');
$clbck = fn($res, $param) => BibResource::cacheHandler($res, $param, self::$cfg->biblatex);
$ttl = self::$cfg->cache->ttl;
$repos = [new RepoWrapperGuzzle(false)];
$searchConfig = new SearchConfig();
$searchConfig->metadataMode = self::$cfg->biblatex->metadataMode;
$searchConfig->metadataParentProperty = self::$cfg->biblatex->parentProperty;
$searchConfig->resourceProperties = self::$cfg->biblatex->resourceProperties;
$searchConfig->relativesProperties = self::$cfg->biblatex->relativesProperties;
$searchConfig->metadataMode = $cfg->metadataMode;
$searchConfig->metadataParentProperty = $cfg->parentProperty;
$searchConfig->resourceProperties = $cfg->resourceProperties;
$searchConfig->relativesProperties = $cfg->relativesProperties;

$cache = new ResponseCache($db, $clbck, $ttl->resource, $ttl->response, $repos, $searchConfig);
$cache = new ResponseCache($db, $clbck, $cfg->ttl->resource, $cfg->ttl->response, $repos, $searchConfig);

return $cache;
}
Expand Down
32 changes: 16 additions & 16 deletions tests/config.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
db: sqlite:/tmp/__db__.sqlite
log:
file: /tmp/__log__
level: debug
cache:
dissCacheService:
db: sqlite:/tmp/__db__.sqlite
log:
file: /tmp/__log__
level: debug
ttl:
resource: 3600 # 1 hour
response: 31536000 # 1 year
repoDb: []
allowedNmsp:
- https://id.acdh.oeaw.ac.at/
- https://arche.acdh.oeaw.ac.at/api/
- https://arche-curation.acdh-dev.oeaw.ac.at/api/
- https://arche-dev.acdh-dev.oeaw.ac.at.at/api/
- https://hdl.handle.net/
repoDb: []
allowedNmsp:
- https://id.acdh.oeaw.ac.at/
- https://arche.acdh.oeaw.ac.at/api/
- https://arche-curation.acdh-dev.oeaw.ac.at/api/
- https://arche-dev.acdh-dev.oeaw.ac.at.at/api/
- https://hdl.handle.net/
metadataMode: parents
parentProperty: ""
resourceProperties: []
relativesProperties: []
biblatex:
metadataMode: parents
parentProperty: ""
resourceProperties: []
relativesProperties: []
defaultLang: en
biblatexProperty: https://vocabs.acdh.oeaw.ac.at/schema#hasCustomCitation
etal: etal
Expand Down

0 comments on commit c947dae

Please sign in to comment.