-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
edge.php
59 lines (50 loc) · 1.75 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
52
53
54
55
56
57
58
59
<?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 as SyncIn;
use Utopia\Queue\Connection\Redis as QueueRedis;
use Utopia\Registry\Registry;
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('register')
->action(function (array $keys, Request $request, Response $response, Registry $register) {
if (empty($keys)) {
throw new Exception(Exception::KEY_NOT_FOUND);
}
$pools = $register->get('pools');
$queue = $pools
->get('queue')
->pop()
->getResource()
;
$client = new SyncIn('syncIn', new QueueRedis(fn() => $queue));
$client->enqueue(['value' => ['keys' => $keys]]);
$response->dynamic(new Document([
'keys' => $keys
]), Response::MODEL_EDGE_SYNC);
});