]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ManifestJsGz.pm
37ee63d041929782a3d407d74027251e3a5b0cbb
[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 my $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 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;
30 }
31
32 sub manifest_add ($$;$$) {
33         my ($ctx, $ibx, $epoch, $default_desc) = @_;
34         my $url_path = "/$ibx->{name}";
35         my $git;
36         if (defined $epoch) {
37                 $url_path .= "/git/$epoch.git";
38                 $git = $ibx->git_epoch($epoch) or return;
39         } else {
40                 $git = $ibx->git;
41         }
42         my $ent = $git->manifest_entry($epoch, $default_desc) or return;
43         inject_entry($ctx, $url_path, $ent, $git->{git_dir});
44 }
45
46 sub slow_manifest_add ($$) {
47         my ($ctx, $ibx) = @_;
48         eval {
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);
53                         }
54                 } else {
55                         manifest_add($ctx, $ibx);
56                 }
57         };
58 }
59
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);
67                 }
68         } else {
69                 warn "E: `${\$ibx->eidx_key}' not indexed by $ALL->{topdir}\n";
70         }
71 }
72
73 sub ibx_entry {
74         my ($ctx, $ibx) = @_;
75         my $ALL = $ctx->{www}->{pi_cfg}->ALL;
76         if ($ALL) {
77                 eidx_manifest_add($ctx, $ALL, $ibx);
78         } else {
79                 slow_manifest_add($ctx, $ibx);
80         }
81         warn "E: $@" if $@;
82 }
83
84 sub hide_key { 'manifest' }
85
86 # overrides WwwListing->psgi_triple
87 sub psgi_triple {
88         my ($ctx) = @_;
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};
94         }
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 ] ]
100 }
101
102 sub per_inbox {
103         my ($ctx) = @_;
104         # only one inbox, slow is probably OK
105         slow_manifest_add($ctx, $ctx->{ibx});
106         psgi_triple($ctx);
107 }
108
109 1;