-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.php
75 lines (59 loc) · 2.03 KB
/
server.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
use Aerys\{Host, Request, Response};
use function Aerys\{root, router};
use Functional as λ;
use Slamp\Slack\Channel;
require __DIR__.'/vendor/autoload.php';
function respond(Response $res, $body, int $status = 200)
{
$res->setStatus($status);
$res->addHeader('Content-Type', 'application/json');
$res->end(json_encode($body));
}
//TODO encrypt slack token and get it from a file (travis) //DONE in theory
$slack_token = trim(file_get_contents('.token'));
$geekdpt = new Slamp\WebClient($slack_token);
$router = router();
$router->get('/team-info', function (Request $req, Response $res) use ($geekdpt) {
$chans = yield $geekdpt->channels->listAsync(['exclude_archived' => true]);
$chans = λ\sort($chans, function (Channel $chanA, Channel $chanB) {
return $chanB->getMembersCount() <=> $chanA->getMembersCount();
});
$chans = λ\map($chans, function (Channel $chan) {
return [
'name' => $chan->getName(),
'membersCount' => $chan->getMembersCount(),
'purpose' => $chan->getPurpose(),
];
});
respond($res, ['channels' => $chans]);
});
$router->post('/get-invite', function (Request $req, Response $res) use ($geekdpt) {
$body = json_decode(yield $req->getBody());
try {
yield $geekdpt->users->admin->inviteAsync($body->email);
} catch (Exception $ex) {
respond($res, ['error' => $ex->getMessage()], 400);
return;
}
respond($res, ['err' => null]);
});
$host = (new Host)
->expose('0.0.0.0', 8080) //TODO put this in config
->name('localhost')
->name('geekdpt.io')
->use($router)
->use(root(__DIR__.'/web/dist'));
Amp\run(function () use ($host) {
$logger = new class extends Aerys\Logger {
protected function output(string $message)
{
//TODO print log in a file
print "$message\n"; // log to stdout
}
};
$server = (new Aerys\Bootstrapper(function () use ($host) {
return [$host];
}))->init($logger, []);
$server->start();
});