NAME
CPAN::InGit::Server - A Mojolicious::Controller that serves the ArchiveTrees from a git repo
DESCRIPTION
This controller serves paths that can be used directly as a CPAN Mirror by tools like 'cpanm'. By default, it serves all branches, but you can also configure it to serve only a single branch. It serves content directly from the Git repo and does not need a working directory checked out.
ATTRIBUTES
cpan_repo
Reference to the CPAN::InGit object from which files will be served
archive_tree
Reference to an ArchvieTree object. If set, the controller will serve only the tree of this branch from whatever path it was rooted at. If not set, this attribute will be lazy-built from attribute tree_name and cached in tree_cache. The lazy-building of this attribute may return undef, and should be checked before dispatching to an action.
tree_name
Explicit selector containing two path components: ~branch/feature~demo, ~tag/v1.0, or ~commit/OBJECT_ID. Slashes in ref names are encoded as ~.
tree_cache
A hashref storing a cache of ArchiveTree objects. This needs to be supplied in the stash if you are loading branches by name at runtime, or else it will be creating an ArchiveTree object on every request.
branch_head_only
If true, this prevents serving files from a Git::Raw tree which is not the HEAD of a branch. By default, any Git sha-1, tag, or branch name may be used so long as it references a tree that meets the requirements for an ArchiveTree. Restricting these to branches-only can help ensure that a malicious user can't pollute the cache and create excessive server load by touching every tree that ever existed.
index_branches
If true, list the available local CPAN archive branches on the repository index. Disabled by default; only applies when serving multiple trees.
module_index_max_rows
Set this to the maximum number of rows to return from the JSON endpoint that searches modules, used by the per-tree index page.
METHODS
serve_repo_index
Serve the repository landing page. Local branches containing CPAN configuration and a package index are listed only when index_branches is enabled in the stash.
serve_index
Serve an HTML index for a CPAN tree, including its commit date, package index date, and the first 5000 matching modules. When import sources are configured, show the newest version of each module in their committed package indexes, and where a source has a newer distribution for the module (as decided by "find_upgrades" in CPAN::InGit::ArchiveTree, the same rule cpangit-audit uses) link the version to a comparison of the two distributions. In single-tree mounts, source branches with a canonical web URL link to that URL.
serve_module_list
Serve modules/ as JSON. filter is a case-insensitive literal substring matched against the entire original 02packages row (module, version, and distribution path). after and before are exclusive module-name boundaries; supply at most one. Rows are sorted by module name, with at most 500 returned per request.
The response contains modules (objects with name, version, path, and text, plus source_version and review_url where applicable), the applied filter, the Git tree ID, and next_after and previous_before cursors (null at either end). complete is true only when this response includes every row matching the filter, including rows outside the requested boundary. A client may then narrow that filter locally.
serve_dist_member
Serve a path within a distribution; a directory listing, a file, or one of the special names ~changes, ~meta, or ~diff. If the distribution is a public CPAN release which can't be found (or has no change log), this redirects to the equivalent page of metacpan.org.
serve_dist_diff
Compare the distribution named by the URL with another distribution, for reviewing an upgrade (or downgrade, or local patch). Query parameters:
- to
-
Required. The author path of the distribution to compare with.
- source
-
Optional name of an import source in which to look for the distribution of the URL.
- to_source
-
Optional name of an import source in which to look for the
todistribution. - path
-
Optional, and may be repeated. Limit the file comparison to these paths.
- patch
-
Set to
0to omit the patches from the response. - hide_version_diff
-
Set to
0to show the hunks which only change the version number. See "compare" in CPAN::InGit::DistTree. - as
-
jsonreturns the structure described in "compare" in CPAN::InGit::DistTree.textreturns the plain-text rendering of "format_comparison" in CPAN::InGit::DistTree. The default is an HTML page.
If either distribution can't be found, this redirects to the change log of the to distribution on metacpan.org when that is a public release, and otherwise returns 404.
check_tree_exists
This checks that the "tree_name" could be successfully resolved to a "archive_tree". It returns true, or sets a 404 error and returns undef, and can be used as ->under(\&check_tree_exists).
serve_package_details
Serve modules/02packages.details.txt[.gz].
serve_author_file
Serve all files under authors/id/.... (All files in that path of the git branch are considered public) This performs automatic creation of .tar.gz files if a directory and matching .meta file exist, allowing distributions to be unpacked within the git repo, but served as .tar.gz files. A trailing slash after an archive filename browses its contents, and a path after the slash is handled by "serve_dist_member".
render_gzipped
Serve content after gzipping it and setting the content type to application/gzip.
ROUTES
Cpan Mirror Paths
A functioning CPAN mirror requires a file /modules/02packages.details.txt.gz which lists the full contents of the latest version of every module. That file also lists the author paths for each module, which are relative to /authors/id/
In CPAN::InGit, /modules/02packages.details.txt is stored uncompressed, and gzipped as it is served. Each author dist is stored as a .tar.gz file and a metadata file with extension .meta and may also optionally be unpacked at a directory of the same name minus the .tar.gz extension. A special author "LOCAL" is used for custom overrides of upstream packages.
Summary:
/ # browsable tree index
/modules/?filter=TEXT&after=MODULE # filtered module list
/modules/02packages.details.txt # index of the mirror
/authors/id/D/DP/DPARIS/Crypt-DES-2.07.tar.gz # copy of upstream
/authors/id/D/DP/DPARIS/Crypt-DES-2.07.meta # extracted metadata
/authors/id/L/LO/LOCAL/Crypt-DES-2.07_01/... # untarred customized dist
/authors/id/L/LO/LOCAL/Crypt-DES-2.07_01.meta # untarred customized dist's metadata
Distribution Contents
A path that continues beyond the file name of a distribution archive refers to the contents of that distribution. The files of the distribution are found directly beneath the archive name (the customary wrapper directory of the archive is not part of the path) and names beginning with ~ are special views of the distribution:
/authors/id/D/DP/DPARIS/Crypt-DES-2.07.tar.gz/ # directory listing
/authors/id/D/DP/DPARIS/Crypt-DES-2.07.tar.gz/lib/ # directory listing
/authors/id/D/DP/DPARIS/Crypt-DES-2.07.tar.gz/DES.pm # one file
/authors/id/D/DP/DPARIS/Crypt-DES-2.07.tar.gz/~changes # the change log, whatever its name
/authors/id/D/DP/DPARIS/Crypt-DES-2.07.tar.gz/~meta # CPAN Meta (version 2) as JSON
/authors/id/D/DP/DPARIS/Crypt-DES-2.07.tar.gz/~diff?to=D/DP/DPARIS/Crypt-DES-2.08.tar.gz
All of these accept a query parameter source naming one of the tree's default_import_sources, to look for the distribution in that tree instead. See "serve_dist_diff" for the parameters of ~diff.
mount
my $route= app->routes->any('/pan');
my $repo= CPAN::InGit->new(git_repo => '/path/to/.git');
CPAN::InGit::Server->mount($route, $repo, app => app, %options);
# %options:
# archive_tree => $ArchiveTree_obj,
# tree_cache => $branch_cache,
# index_branches => 1, # optionally list local CPAN archive branches
# branch_head_only => 1, # only serve actual branch HEADs
This class method sets up the routes for serving the mirror under a provided route. $route is the route under which you want to serve the 'PAN. The app option supplies the owning Mojolicious application and is required for multi-tree mounts, to install the before_routes path-rewriting hook.
If you specify 'archive_tree', that `PAN will be directly mounted at the route. Otherwise, all trees (or just branches if you use option branch_head_only) will be mounted as:
/ # repository index
/~static/... # static files served publicly
/~branch/:branch_name/*tree # branch, with '/' replaced by '~'
/~tag/:tag_name/*tree # tag, with '/' replaced by '~'
/~commit/:sha1/*tree # SHA-1 Hex commit ID
/*name/*tree # branch, else tag, else commit
The route's root serves a repository index; index_branches enables branch links on that page and defaults to false.
Browsing and comparing the contents of distributions unpacks each archive once into "dist_cache_repo" in CPAN::InGit.
Files in share/public are served verbatim under ~static/ within the mount, using Mojo's static file handler.
The ~branch, ~tag, and ~commit prefixes are followed by a single path component that unambiguously represents a tree; branch and tag names containing '/' have that character replaced by '~'. In the last form, path components are consumed from the URL in sequence until it matches a branch or tag or SHA1, which is convenient but can be ambiguous. For example, if a tag named issue exists and a branch named issue/123 exists, a url for issue/123/modules resolves to the path 123/modules within tag "issue", and does not find the branch. Most users won't have overlapping branches and tags, though, so these URLs are more convenient.
VERSION
version 0.007
AUTHOR
Michael Conrad <mike@nrdvana.net>
COPYRIGHT AND LICENSE
This software is copyright (c) 2026 by Michael Conrad, and IntelliTree Solutions.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.