Skip to content

Commit

Permalink
(#2), (#3): move actual schemas to /_/meta folder (not indented for d…
Browse files Browse the repository at this point in the history
…irect access)
  • Loading branch information
fititnt committed Sep 29, 2022
1 parent b49c884 commit 8828f4f
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 37 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ Check [resolvers](resolvers/) folder.
- https://json-ld.org/playground/
- https://json-ld.org/spec/latest/json-ld/#nested-properties
- https://www.jsonschemavalidator.net/
### Logos
- https://github.com/json-api/json-api/issues/1655
- https://json-ld.org/images/
- https://www.w3.org/RDF/icons/
- https://www.hydra-cg.com/spec/latest/core/ (maybe)
-->

## License
Expand Down
8 changes: 7 additions & 1 deletion app/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ Header add Access-Control-Allow-Methods: "GET,POST,HEAD,OPTIONS,DELETE,PUT"
Options +FollowSymlinks
Options -Indexes

# Alias /urn:resolver:meta:context:api public/_/meta/urnresolver-api.context.jsonld
# Alias /urn:resolver:meta:context:urnr public/_/meta/urnresolver-urnr.schema.json

RewriteEngine On

# RewriteBase /

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# RewriteRule ^urn:resolver:meta:context:urnr$ public/_/meta/urnresolver-urnr.schema.json [L,NC]
# RewriteRule ^urnresolver-urnr$ public/_/meta/urnresolver-urnr.schema.json [NC]

# RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
Expand Down
101 changes: 73 additions & 28 deletions app/lib/urnresolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
define("ROOT_PATH", dirname(dirname(__FILE__)));
define("RESOLVER_RULE_PATH", ROOT_PATH . '/public/.well-known/urn');

$global_conf = new Config();

define("URNRESOLVER_BASE", $global_conf->base_iri);

// https://www.php-fig.org/psr/psr-12/

function debug()
Expand All @@ -28,6 +32,25 @@ function debug()
return $info;
}

/**
* Pretty print JSON (2 spaces and newline)
*
* @param object $data
* @return string
*/
function to_json($data)
{
$json_string_4spaces = json_encode(
$data,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
// https://stackoverflow.com/a/31689850/894546
$json_string = preg_replace_callback('/^ +/m', function ($m) {
return str_repeat(' ', strlen($m[0]) / 2);
}, $json_string_4spaces);
return $json_string . "\n";
}

class Config
{
public $global_conf;
Expand Down Expand Up @@ -130,9 +153,13 @@ public function execute_output_2xx(
// header("Access-Control-Allow-Origin: *");

$result = [
'$schema' => 'https://jsonapi.org/schema',
'$id' => $base,
'@context' => 'https://urn.etica.ai/urnresolver-context.jsonld',
// '$schema' => 'https://jsonapi.org/schema',
// @TODO make this also an URN (with htaccess rewirte for performance reason)
'$schema' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.schema.json',
// '$id' => $base,
// '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld',
'@context' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.context.jsonld',
'@id' => $base,
'data' => $data,
'meta' => [
'@type' => 'schema:Message',
Expand All @@ -146,7 +173,8 @@ public function execute_output_2xx(
]
];

echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo to_json($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
die();
}

Expand All @@ -165,9 +193,13 @@ public function execute_output_4xx(
// header("Access-Control-Allow-Origin: *");

$result = [
'$schema' => 'https://jsonapi.org/schema',
'$id' => $base,
'@context' => 'https://urn.etica.ai/urnresolver-context.jsonld',
// '$schema' => 'https://jsonapi.org/schema',
// @TODO make this also an URN (with htaccess rewirte for performance reason)
'$schema' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.schema.json',
// '$id' => $base,
// '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld',
'@context' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.context.jsonld',
'@id' => $base,
'error' => [
'status' => $http_status_code,
'title' => $http_status_msg,
Expand All @@ -182,7 +214,8 @@ public function execute_output_4xx(
]
];

echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo to_json($result);
die();
}

Expand All @@ -204,9 +237,13 @@ public function execute_output_5xx(
// header("Access-Control-Allow-Origin: *");

$result = [
'$schema' => 'https://jsonapi.org/schema',
'$id' => $base,
'@context' => 'https://urn.etica.ai/urnresolver-context.jsonld',
// '$schema' => 'https://jsonapi.org/schema',
// @TODO make this also an URN (with htaccess rewirte for performance reason)
'$schema' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.schema.json',
// '$id' => $base,
// '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld',
'@context' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.context.jsonld',
'@id' => $base,
'error' => [
'status' => $http_status_code,
'title' => $http_status_msg,
Expand All @@ -224,7 +261,8 @@ public function execute_output_5xx(
]
];

echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo to_json($result);
die();
}

Expand Down Expand Up @@ -578,13 +616,18 @@ public function execute_welcome()
}

$result = [
'$schema' => 'https://jsonapi.org/schema',
'$id' => $this->active_base . $this->active_uri,
'@context' => 'https://urn.etica.ai/urnresolver-context.jsonld',
// '$schema' => 'https://jsonapi.org/schema',
// @TODO make this also an URN (with htaccess rewirte for performance reason)
'$schema' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.schema.json',
// '$id' => $base,
// '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld',
// '@id' => $base,
'@id' => URNRESOLVER_BASE, // home page, display the site URL
'@context' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.context.jsonld',
'@type' => 'schema:Action',
'schema:endTime' => date("c"),
// 'schema:endTime' => date("c"),
'data' => [
'type' => "schema:Action",
// 'type' => "schema:Action",
'id' => $this->config->base_iri,
'relationships' => [
'vurn:resolver:index' => [
Expand All @@ -610,21 +653,23 @@ public function execute_welcome()
// 'links' => [
// 'uptime' => 'https://stats.uptimerobot.com/jYDZlFY8jq'
// ],
// 'meta' => [
// '@type' => 'schema:Message',
// 'schema:name' => 'URN Resolver',
// 'schema:dateCreated' => date("c"),
// // 'schema:mainEntityOfPage' => 'https://github.com/EticaAI/urn-resolver',
// "schema:potentialAction" => [
// "schema:name" => "uptime",
// "schema:url" => "https://stats.uptimerobot.com/jYDZlFY8jq"
// ]
// ]
'meta' => [
'@type' => 'schema:Message',
'schema:name' => 'URN Resolver',
// 'schema:dateCreated' => date("c"),
'schema:endTime' => date("c"),
// 'schema:mainEntityOfPage' => 'https://github.com/EticaAI/urn-resolver',
"schema:potentialAction" => [
"schema:name" => "uptime",
"schema:url" => "https://stats.uptimerobot.com/jYDZlFY8jq"
]
]
];

http_response_code(200);
// $result->_debug['_router'] = $this->meta();
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo to_json($result);
die();
}

Expand Down
4 changes: 3 additions & 1 deletion app/public/meta/README.md → app/public/_/meta/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# URN Resolver / Meta
# URN Resolver (app) / _ / meta

## Files
### urnresolver.context.jsonld
> - [urnresolver.context.jsonld](urnresolver.context.jsonld)
> - Ideal path to serve: `{resolver}/urn:resolver:meta:context:api`
Prefixes uses https://github.com/w3c/json-ld-rc/ (https://w3c.github.io/json-ld-rc/context.jsonld) as reference

### urnresolver.schema.json
> - [urnresolver.schema.json](urnresolver.schema.json)
> - Ideal path to serve: `{resolver}/urn:resolver:meta:context:urnr`
## Relation to other APIs

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
"schema": "http://schema.org/",
"skos": "http://www.w3.org/2004/02/skos/core#",
"xml": "http://www.w3.org/XML/1998/namespace",
"xsd": "http://www.w3.org/2001/XMLSchema#"
"xsd": "http://www.w3.org/2001/XMLSchema#",
"hydra": "http://www.w3.org/ns/hydra/core#",
"data": "@nest",
"error": {
"@id": "hydra:Error"
},
"status": {
"@id": "hydra:statusCode",
"@nest": "error"
},
"title": {
"@id": "hydra:Status",
"@nest": "error"
},
"$schema": null
}
}
33 changes: 33 additions & 0 deletions app/public/_/meta/urnresolver-api.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "urn:resolver:meta:schema:api",
"title": "URN Resolver JSON Schema",
"description": "Early draft. See https://github.com/EticaAI/urn-resolver/issues/2",
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^@": {
"type": [
"string",
"object"
]
}
},
"properties": {
"$id": {
"type": "string"
},
"$schema": {
"type": "string"
},
"data": {
"type": ["array", "object"]
},
"meta": {
"type": "object"
},
"error": {
"type": "object"
}
}
}
2 changes: 1 addition & 1 deletion app/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
// echo $t->getMessage(), " at ", $t->getFile(), ":", $t->getLine(), "\n";
http_response_code(500);
header("Content-type: application/json; charset=utf-8");
echo json_encode( $data, JSON_PRETTY_PRINT);
echo json_encode($data);
die;
}

Expand Down
5 changes: 0 additions & 5 deletions app/public/meta/urnresolver.schema.json

This file was deleted.

0 comments on commit 8828f4f

Please sign in to comment.