1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # generates manifest.js.gz for grokmirror(1)
5 package PublicInbox::ManifestJsGz;
8 use parent qw(PublicInbox::WwwListing);
10 use PublicInbox::Inbox;
11 use PublicInbox::Config;
13 use IO::Compress::Gzip qw(gzip);
14 use HTTP::Date qw(time2str);
16 our $json = PublicInbox::Config::json();
18 # called by WwwListing
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');
26 sub manifest_add ($$;$$) {
27 my ($ctx, $ibx, $epoch, $default_desc) = @_;
28 my $url_path = "/$ibx->{name}";
29 my $git_dir = $ibx->{inboxdir};
31 $git_dir .= "/git/$epoch.git";
32 $url_path .= "/git/$epoch.git";
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;
42 $ctx->{manifest}->{$url_path} = $ent;
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);
54 manifest_add($ctx, $ibx);
60 sub hide_key { 'manifest' }
62 # overrides WwwListing->psgi_triple
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};
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 ] ]