1 # Copyright (C) 2020-2021 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);
9 use PublicInbox::Config;
10 use IO::Compress::Gzip qw(gzip);
11 use HTTP::Date qw(time2str);
13 my $json = PublicInbox::Config::json();
17 # grokmirror uses relative paths, so it's domain-dependent
18 # SUPER calls PublicInbox::WwwListing::url_filter
19 $ctx->SUPER::url_filter('publicInbox.grokManifest', 'match=domain');
22 sub inject_entry ($$$;$) {
23 my ($ctx, $url_path, $ent, $git_dir) = @_;
24 $ctx->{-abs2urlpath}->{$git_dir // delete $ent->{git_dir}} = $url_path;
25 my $modified = $ent->{modified};
26 $ctx->{-mtime} = $modified if $modified > ($ctx->{-mtime} // 0);
27 $ctx->{manifest}->{$url_path} = $ent;
30 sub manifest_add ($$;$$) { # slow path w/o extindex "all"
31 my ($ctx, $ibx, $epoch, $default_desc) = @_;
32 my $url_path = "/$ibx->{name}";
35 $url_path .= "/git/$epoch.git";
36 $git = $ibx->git_epoch($epoch) or return;
40 my $ent = $git->manifest_entry($epoch, $default_desc) or return;
41 inject_entry($ctx, $url_path, $ent, $git->{git_dir});
44 sub slow_manifest_add ($$) {
47 if (defined(my $max = $ibx->max_git_epoch)) {
48 my $desc = $ibx->description;
49 for my $epoch (0..$max) {
50 manifest_add($ctx, $ibx, $epoch, $desc);
53 manifest_add($ctx, $ibx);
58 sub eidx_manifest_add ($$$) {
59 my ($ctx, $ALL, $ibx) = @_;
60 if (my $data = $ALL->misc->inbox_data($ibx)) {
61 $data = $json->decode($data);
62 delete $data->{''}; # private
63 while (my ($url_path, $ent) = each %$data) {
64 inject_entry($ctx, $url_path, $ent);
67 warn "E: `${\$ibx->eidx_key}' not indexed by $ALL->{topdir}\n";
72 my ($class, $ctx) = @_;
74 my ($re, undef) = $ctx->url_filter;
75 $re // return psgi_triple($ctx);
76 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
77 $ctx->can('list_match_i'), $re, $ctx);
79 $ctx->{-wcb} = $_[0]; # HTTP server callback
80 $ctx->{env}->{'pi-httpd.async'} ?
81 $iter->event_step : $iter->each_section;
87 my $ALL = $ctx->{www}->{pi_cfg}->ALL;
88 if ($ALL) { # FIXME: test this in t/
89 eidx_manifest_add($ctx, $ALL, $ibx);
91 slow_manifest_add($ctx, $ibx);
96 sub hide_key { 'manifest' } # for WwwListing->list_match_i
100 my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
101 my $manifest = delete($ctx->{manifest}) // {};
102 while (my ($url_path, $repo) = each %$manifest) {
103 defined(my $abs = $repo->{reference}) or next;
104 $repo->{reference} = $abs2urlpath->{$abs};
106 $manifest = $json->encode($manifest);
107 gzip(\$manifest => \(my $out));
108 [ 200, [ qw(Content-Type application/gzip),
109 'Last-Modified', time2str($ctx->{-mtime}),
110 'Content-Length', length($out) ], [ $out ] ]
115 # only one inbox, slow is probably OK
116 slow_manifest_add($ctx, $ctx->{ibx});