]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ManifestJsGz.pm
git: add manifest_entry method
[public-inbox.git] / lib / PublicInbox / ManifestJsGz.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # generates manifest.js.gz for grokmirror(1)
5 package PublicInbox::ManifestJsGz;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::WwwListing);
9 use bytes (); # length
10 use PublicInbox::Inbox;
11 use PublicInbox::Config;
12 use PublicInbox::Git;
13 use IO::Compress::Gzip qw(gzip);
14 use HTTP::Date qw(time2str);
15
16 our $json = PublicInbox::Config::json();
17
18 # called by WwwListing
19 sub url_regexp {
20         my ($ctx) = @_;
21         # grokmirror uses relative paths, so it's domain-dependent
22         # SUPER calls PublicInbox::WwwListing::url_regexp
23         $ctx->SUPER::url_regexp('publicInbox.grokManifest', 'match=domain');
24 }
25
26 sub manifest_add ($$;$$) {
27         my ($ctx, $ibx, $epoch, $default_desc) = @_;
28         my $url_path = "/$ibx->{name}";
29         my $git_dir = $ibx->{inboxdir};
30         if (defined $epoch) {
31                 $git_dir .= "/git/$epoch.git";
32                 $url_path .= "/git/$epoch.git";
33         }
34         return unless -d $git_dir;
35         my $git = PublicInbox::Git->new($git_dir);
36         my $ent = $git->manifest_entry($epoch, $default_desc) or return;
37         $ctx->{-abs2urlpath}->{$git_dir} = $url_path;
38         my $modified = $ent->{modified};
39         if ($modified > ($ctx->{-mtime} // 0)) {
40                 $ctx->{-mtime} = $modified;
41         }
42         $ctx->{manifest}->{$url_path} = $ent;
43 }
44
45 sub ibx_entry {
46         my ($ctx, $ibx) = @_;
47         eval {
48                 if (defined(my $max = $ibx->max_git_epoch)) {
49                         my $desc = $ibx->description;
50                         for my $epoch (0..$max) {
51                                 manifest_add($ctx, $ibx, $epoch, $desc);
52                         }
53                 } else {
54                         manifest_add($ctx, $ibx);
55                 }
56         };
57         warn "E: $@" if $@;
58 }
59
60 sub hide_key { 'manifest' }
61
62 # overrides WwwListing->psgi_triple
63 sub psgi_triple {
64         my ($ctx) = @_;
65         my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
66         my $manifest = delete($ctx->{manifest}) // {};
67         while (my ($url_path, $repo) = each %$manifest) {
68                 defined(my $abs = $repo->{reference}) or next;
69                 $repo->{reference} = $abs2urlpath->{$abs};
70         }
71         $manifest = $json->encode($manifest);
72         gzip(\$manifest => \(my $out));
73         [ 200, [ qw(Content-Type application/gzip),
74                  'Last-Modified', time2str($ctx->{-mtime}),
75                  'Content-Length', bytes::length($out) ], [ $out ] ]
76 }
77
78 1;