vanity

package module
v0.1.2 Latest Latest
Warning

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: 3

README

go-vanity

Go vanity URLs

Documentation

Overview

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

Index

Constants

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.

Variables

This section is empty.

Functions

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.

Types

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:
	//
	//     go.yhsif.com
	//
	// Or:
	//
	//     yhsif.com/go
	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:
	//
	//     https://github.com/fishy/go-vanity
	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
	VCS  VCS

	// 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>
<html>
<head>
<title>Vanity go projects for {{.Prefix}}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
{{if .Mappings}}
<ul>
{{range $mapping := .Mappings}}
{{if $mapping.HideInIndex -}}
{{else -}}
<li><a href="https://pkg.go.dev/{{$.Prefix}}{{$mapping.Path}}"><code>{{$.Prefix}}{{$mapping.Path}}</code></a>: (<a href="{{$mapping.URL}}">src</a>)
{{- if $mapping.Description -}}
&nbsp;{{$mapping.Description}}
{{- end -}}
</li>
{{- end}}
{{- end}}
</ul>
{{else}}
<p>Nothing to see here. Come back later.</p>
{{- end}}
</body>
</html>
`))

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>
<html>
<head>
<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=https://pkg.go.dev/{{.Prefix}}{{.Reqpath}}">
</head>
<body><p>
Nothing to see here, <a href="https://pkg.go.dev/{{.Prefix}}{{.Reqpath}}">move along</a>.
</p></body>
</html>
`))

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.

Directories

Path Synopsis
appengine module
cloudrun module

Jump to

Keyboard shortcuts

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