vanity

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2023 License: AGPL-3.0 Imports: 7 Imported by: 0

README

Vanity

builds.sr.ht status Common Changelog

Vanity is a simple Go vanity URL server.

Concepts

Go tools like the go get and go install commands use a Go package's import path to locate the repository containing that package. When locating a package, go get makes an HTTP GET request to the URL designated by the import path and looks for a <meta> tag in the response. The contents of this tag tells go get where the repository containing that package is located and what type of version control system is used. (For more information about how Go locates a repository, see the Go Modules Reference).

Popular code hosting providers like SourceHut and GitHub serve this <meta> tag automatically, so import paths like github.com/johndoe/mymodule/mypkg need no configuration to work with go get. However, many package authors prefer to use their own domain name in the package path to make it possible to switch code hosting providers without changing the import path. These custom import paths are more informally known as vanity URLs. Vanity is a bare-bones web server that serves the necessary <meta> tag so that go get can locate the package's repository.

In order to function, Vanity requires a single piece of information: the repository base URL. Vanity assumes that all repositories are immediate children of this base URL. For example, if you store your code in GitHub and your GitHub username is johndoe, then the repository base URL would be https://github.com/johndoe.

When receiving an HTTP request, Vanity extracts the first path segment from the request URL and uses this as the repository name. Any additional path segments would correspond to repository subdirectories and are ignored by Vanity. This repository name is appended to the repository base URL to form the full repo URL that is returned in the response. If the repository name is not the first path segment after the domain, as in golang.org/x/mymodule, use a reverse proxy to remove the path prefix before passing the request to Vanity (e.g. GET /x/mymodule HTTP/1.1 should be rewritten to GET /mymodule HTTP/1.1).

Note that Vanity can work on multiple domains simultaneously; Vanity obtains the domain name from the Host request header. The only requirement is that the first path segment match the repository name.

Installation

To install the self-contained Vanity binary, run:

go install dwh.moe/vanity/cmd/vanity@latest

Usage

To start a vanity server, simply pass the repository base URL on the command line:

vanity [flags] repo-base-url

The server will listen for requests on port 8000 by default. Use the -p flag to change this. For example, to run a server on port 4000 pointing to the GitHub repositories of johndoe, run:

vanity -p 4000 https://github.com/johndoe

Use the -vcs flag to specify the version control system (defaults to Git). The supported options are bzr, fossil, git, hg, or svn.

Integration

To integrate Vanity into an existing Go web app, import dwh.moe/vanity and create a server with vanity.New():

package main

import "dwh.moe/vanity"

const (
	repoBaseURL = "https://git.sr.ht/~johndoe"
	vcs         = vanity.VCSGit
)

func main() {
	// vanitySrv implements net/http.Handler
	vanitySrv := vanity.New(repoBaseURL, vcs)
	http.Handle("/", vanitySrv)
}

Documentation

Overview

Package vanity provides a Go vanity URL server.

Index

Constants

View Source
const (
	VCSBazaar     = "bzr"
	VCSFossil     = "fossil"
	VCSGit        = "git"
	VCSMercurial  = "hg"
	VCSSubversion = "svn"
)

Supported version control systems (VCS).

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// RepoBaseURL is the base URL common to all repositories. At many VCS
	// hosting services, this would take the form "https://domain.org/username".
	RepoBaseURL string

	// RepoSuffix is an optional string to append to repository URLs.
	RepoSuffix string

	// The version control system used. Must be one of "bzr", "fossil", "git",
	// "hg", or "svn". For convenience, you may use the server.VCS* constants.
	VCS string
}

A Server handles HTTP requests made to URLs derived from a Go import path, directing the `go get` tool to the location of the module's source code repository.

The repository URL is formed by joining RepoBaseURL and the first segment of the request URL path, and then appending RepoSuffix.

If RepoBaseURL is relative, blank, or cannot be parsed, the Server responds with HTTP 500 Internal Server Error. If the request URL path is "/", HTTP 404 Not Found is returned. The Content-Type of a successful response is "text/plain".

func New

func New(repoBaseURL, vcs string) *Server

New returns a new Server using the provided repository base URL and VCS.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
cmd
vanity
Vanity starts a simple Go vanity web server.
Vanity starts a simple Go vanity web server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL