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::Config;
11 use IO::Compress::Gzip qw(gzip);
12 use HTTP::Date qw(time2str);
14 our $json = PublicInbox::Config::json();
16 # called by WwwListing
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');
24 sub inject_entry ($$$;$) {
25 my ($ctx, $url_path, $ent, $git_dir) = @_;
26 $ctx->{-abs2urlpath}->{$git_dir // delete $ent->{git_dir}} = $url_path;
27 my $modified = $ent->{modified};
28 $ctx->{-mtime} = $modified if $modified > ($ctx->{-mtime} // 0);
29 $ctx->{manifest}->{$url_path} = $ent;
32 sub manifest_add ($$;$$) {
33 my ($ctx, $ibx, $epoch, $default_desc) = @_;
34 my $url_path = "/$ibx->{name}";
37 $url_path .= "/git/$epoch.git";
38 $git = $ibx->git_epoch($epoch) or return;
42 my $ent = $git->manifest_entry($epoch, $default_desc) or return;
43 inject_entry($ctx, $url_path, $ent, $git->{git_dir});
46 sub slow_manifest_add ($$) {
49 if (defined(my $max = $ibx->max_git_epoch)) {
50 my $desc = $ibx->description;
51 for my $epoch (0..$max) {
52 manifest_add($ctx, $ibx, $epoch, $desc);
55 manifest_add($ctx, $ibx);
60 sub eidx_manifest_add ($$$) {
61 my ($ctx, $ALL, $ibx) = @_;
62 if (my $data = $ALL->misc->inbox_data($ibx)) {
63 $data = $json->decode($data);
64 delete $data->{''}; # private
65 while (my ($url_path, $ent) = each %$data) {
66 inject_entry($ctx, $url_path, $ent);
69 warn "E: `${\$ibx->eidx_key}' not indexed by $ALL->{topdir}\n";
75 my $ALL = $ctx->{www}->{pi_cfg}->ALL;
77 eidx_manifest_add($ctx, $ALL, $ibx);
79 slow_manifest_add($ctx, $ibx);
84 sub hide_key { 'manifest' }
86 # overrides WwwListing->psgi_triple
89 my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
90 my $manifest = delete($ctx->{manifest}) // {};
91 while (my ($url_path, $repo) = each %$manifest) {
92 defined(my $abs = $repo->{reference}) or next;
93 $repo->{reference} = $abs2urlpath->{$abs};
95 $manifest = $json->encode($manifest);
96 gzip(\$manifest => \(my $out));
97 [ 200, [ qw(Content-Type application/gzip),
98 'Last-Modified', time2str($ctx->{-mtime}),
99 'Content-Length', bytes::length($out) ], [ $out ] ]
104 # only one inbox, slow is probably OK
105 slow_manifest_add($ctx, $ctx->{ibx});