-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
edge.php
51 lines (44 loc) · 1.5 KB
/
edge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
use Ahc\Jwt\JWT;
use Ahc\Jwt\JWTException;
use Appwrite\Extend\Exception;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Utopia\App;
use Utopia\Database\Document;
use Utopia\Queue\Client;
use Utopia\Validator\ArrayList;
use Utopia\Validator\Text;
App::init()
->groups(['edge'])
->inject('request')
->action(function (Request $request) {
$token = $request->getHeader('authorization');
$token = str_replace(["Bearer"," "], "", $token);
$jwt = new JWT(App::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 600, 10);
try {
$payload = $jwt->decode($token);
} catch (JWTException $error) {
throw new Exception(Exception::USER_JWT_INVALID, 'Failed to verify JWT. ' . $error->getMessage());
}
});
App::post('/v1/edge/sync')
->desc('Purge cache keys')
->groups(['edge'])
->label('scope', 'public')
->param('keys', '', new ArrayList(new Text(100), 1000), 'Cache keys. an array containing alphanumerical cache keys')
->inject('request')
->inject('response')
->inject('queueForCacheSyncIn')
->action(function (array $keys, Request $request, Response $response, Client $queueForCacheSyncIn) {
if (empty($keys)) {
throw new Exception(Exception::KEY_NOT_FOUND);
}
$queueForCacheSyncIn
->enqueue([
'keys' => $keys
]);
$response->dynamic(new Document([
'keys' => $keys
]), Response::MODEL_EDGE_SYNC);
});