package module
Version: v0.1.2 Latest Latest

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

Go to latest
Published: Aug 13, 2022 License: BSD-3-Clause Imports: 6 Imported by: 2



Go vanity URLs



Package vanity provides http handler implementations for go vanity URL support.



View Source
const MetaTemplateLine = `<meta name="go-import" content="{{.Prefix}}{{.Path}} {{.VCS}} {{.URL}}">`

MetaTemplateLine is the html->head->meta line that must exist in vanity page html template.


This section is empty.


func Handler

func Handler(args Args) http.HandlerFunc

Handler creates an HTTP handler that handles go vanity URL requests.

func IndexHandler

func IndexHandler(args Args) http.HandlerFunc

IndexHandler creates a handler to render the index page.

Note that by default this is already included in Handler so you don't need to use it directly. It's provided, along with Args.NoIndex, so that you could put the index page on a different path.


type Args

type Args struct {
	Config Config

	// If set to true, do not render and handle the index page.
	NoIndex bool

Args defines the args used by Handler function.

type Config

type Config struct {
	// The vanity import prefix shared by all paths, without URL schema and
	// trailing "/".
	// Example:
	// Or:
	Prefix string `yaml:"prefix" json:"prefix"`

	// The mappings of the repositories.
	Mappings []Mapping `yaml:"mappings" json:"mappings"`

Config defines the configuration used by the http handler.

type Mapping

type Mapping struct {
	// The path of the vanity URL, with leading "/"/
	// Example:
	//     /vanity
	Path string `yaml:"path" json:"path"`

	// The full URL of the actual repository.
	// Example:
	URL string `yaml:"url" json:"url"`

	// The VCS of the repository. Default to DefaultVCS ("git").
	VCS VCS `yaml:"vcs" json:"vcs"`

	// A brief description to be shown on the index page.
	Description string `yaml:"description" json:"description"`

	// If set to true, do not show this mapping in index page.
	HideInIndex bool `yaml:"unlist" json:"unlist"`

Mapping defines a mapping from a vanity path to an actual repository.

type PageTmplData

type PageTmplData struct {
	// From Config.Prefix.
	Prefix string

	// From Config.Mappings
	Path string
	URL  string

	// The path from the request,
	// could be different from Path but always have Path as prefix.
	Reqpath string

PageTmplData is the data used to execute PageTmpl.

type Templater

type Templater interface {
	Execute(io.Writer, any) error

Templater is the minimal interface shared between go's text and html templates.

var IndexTmpl Templater = template.Must(template.New("index").Parse(`<!DOCTYPE html>
<title>Vanity go projects for {{.Prefix}}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
{{if .Mappings}}
{{range $mapping := .Mappings}}
{{if $mapping.HideInIndex -}}
{{else -}}
<li><a href="{{$.Prefix}}{{$mapping.Path}}"><code>{{$.Prefix}}{{$mapping.Path}}</code></a>: (<a href="{{$mapping.URL}}">src</a>)
{{- if $mapping.Description -}}
{{- end -}}
{{- end}}
{{- end}}
<p>Nothing to see here. Come back later.</p>
{{- end}}

IndexTmpl is the html template used to render the index page.

It's exported so users of this package could replace it to a different one if they so desire.

The data used to execute the template is Config.

var PageTmpl Templater = template.Must(template.New("vanity").Parse(`<!DOCTYPE html>
<title>Vanity page for {{.Prefix}}{{.Reqpath}}</title>
` + MetaTemplateLine + `
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="refresh" content="0; url={{.Prefix}}{{.Reqpath}}">
Nothing to see here, <a href="{{.Prefix}}{{.Reqpath}}">move along</a>.

PageTmpl is the html template used to render the vanity pages.

It's exported so users of this package could replace it to a different one if they so desire. But when replacing it, MetaTemplateLine (or equivalent) must exists in the template, or the vanity page might be invalid.

The data used to execute the template is defined in PageTmplData.

type VCS

type VCS string

VCS defines the vcs ("git", "mod", "hg", etc.) used in Mapping.

It treats zero value (empty string) as DefaultVCS, and behave the same as strings otherwise.

const (
	DefaultVCS VCS = "git"

The default VCS to be used.

func (VCS) MarshalText

func (v VCS) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler.

func (VCS) String

func (v VCS) String() string

func (*VCS) UnmarshalText

func (v *VCS) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.


Path Synopsis
appengine module

Jump to

Keyboard shortcuts

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