]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ManifestJsGz.pm
manifest: use ibx->git_epoch method for v2
[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::Config;
11 use IO::Compress::Gzip qw(gzip);
12 use HTTP::Date qw(time2str);
13
14 our $json = PublicInbox::Config::json();
15
16 # called by WwwListing
17 sub url_regexp {
18         my ($ctx) = @_;
19         # grokmirror uses relative paths, so it's domain-dependent
20         # SUPER calls PublicInbox::WwwListing::url_regexp
21         $ctx->SUPER::url_regexp('publicInbox.grokManifest', 'match=domain');
22 }
23
24 sub manifest_add ($$;$$) {
25         my ($ctx, $ibx, $epoch, $default_desc) = @_;
26         my $url_path = "/$ibx->{name}";
27         my $git;
28         if (defined $epoch) {
29                 $url_path .= "/git/$epoch.git";
30                 $git = $ibx->git_epoch($epoch) or return;
31         } else {
32                 $git = $ibx->git;
33         }
34         my $ent = $git->manifest_entry($epoch, $default_desc) or return;
35         $ctx->{-abs2urlpath}->{$git->{git_dir}} = $url_path;
36         my $modified = $ent->{modified};
37         if ($modified > ($ctx->{-mtime} // 0)) {
38                 $ctx->{-mtime} = $modified;
39         }
40         $ctx->{manifest}->{$url_path} = $ent;
41 }
42
43 sub ibx_entry {
44         my ($ctx, $ibx) = @_;
45         eval {
46                 if (defined(my $max = $ibx->max_git_epoch)) {
47                         my $desc = $ibx->description;
48                         for my $epoch (0..$max) {
49                                 manifest_add($ctx, $ibx, $epoch, $desc);
50                         }
51                 } else {
52                         manifest_add($ctx, $ibx);
53                 }
54         };
55         warn "E: $@" if $@;
56 }
57
58 sub hide_key { 'manifest' }
59
60 # overrides WwwListing->psgi_triple
61 sub psgi_triple {
62         my ($ctx) = @_;
63         my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
64         my $manifest = delete($ctx->{manifest}) // {};
65         while (my ($url_path, $repo) = each %$manifest) {
66                 defined(my $abs = $repo->{reference}) or next;
67                 $repo->{reference} = $abs2urlpath->{$abs};
68         }
69         $manifest = $json->encode($manifest);
70         gzip(\$manifest => \(my $out));
71         [ 200, [ qw(Content-Type application/gzip),
72                  'Last-Modified', time2str($ctx->{-mtime}),
73                  'Content-Length', bytes::length($out) ], [ $out ] ]
74 }
75
76 1;