<commit_before>package vanity // import "kkn.fi/vanity"

import (
	"fmt"
	"net/http"
)

type (
	// Path is the path component of the HTTP request sent by Go tool or browser.
	Path string
	// Package defines Go package that has vanity import defined by Path,
	// VCS system type and VCS URL.
	Package struct {
		// Path is path component of vanity url.
		Path Path
		// VCSSystem is version control system used by the project.
		// Usually 'git' or 'hg'.
		VCSSystem string
		// VCSURL is the HTTP URL for project's version control system.
		VCSURL string
	}
	// Server is the actual HTTP server for Go vanity domains.
	Server struct {
		// Domain is the vanity domain.
		Domain *string
		// Config contains settings for vanity packages.
		Config map[Path]Package
	}
)

// NewPackage returns a new Package given a path, VCS system and VCS URL.
func NewPackage(path, vcssystem, vcsurl string) *Package {
	p := &Package{
		Path:      Path(path),
		VCSSystem: vcssystem,
		VCSURL:    vcsurl,
	}
	return p
}

// NewServer returns a new Vanity Server given domain name and vanity package configuration.
func NewServer(domain string, config map[Path]Package) *Server {
	s := &Server{
		Domain: &domain,
		Config: config,
	}
	return s
}

// GoImportLink creates the link used in HTML <meta/> tag
// where domain is the domain name of the server.
func (p Package) GoImportLink(domain string) string {
	return fmt.Sprintf("%s%s %s %s", domain, p.Path, p.VCSSystem, p.VCSURL)
}

// GoImportMeta creates the <meta/> HTML tag containing name and content attributes.
func (p Package) GoImportMeta(domain string) string {
	link := p.GoImportLink(domain)
	return fmt.Sprintf(`<meta name="go-import" content="%s">`, link)
}

func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Content-Type", "text/html; charset=utf-8")
	if r.Method != http.MethodGet {
		http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
		return
	}

	pack, ok := s.Config[Path(r.URL.Path)]
	if !ok {
		http.NotFound(w, r)
		return
	}
	if r.FormValue("go-get") != "1" {
		url := fmt.Sprintf("https://godoc.org/%v%v", *s.Domain, r.URL.Path)
		http.Redirect(w, r, url, http.StatusTemporaryRedirect)
		return
	}
	fmt.Fprint(w, pack.GoImportMeta(*s.Domain))
}<commit_msg>Add Package.GoDocURL()<commit_after>package vanity // import "kkn.fi/vanity"

import (
	"fmt"
	"net/http"
)

type (
	// Path is the path component of the HTTP request sent by Go tool or browser.
	Path string
	// Package defines Go package that has vanity import defined by Path,
	// VCS system type and VCS URL.
	Package struct {
		// Path is path component of vanity url.
		Path Path
		// VCSSystem is version control system used by the project.
		// Usually 'git' or 'hg'.
		VCSSystem string
		// VCSURL is the HTTP URL for project's version control system.
		VCSURL string
	}
	// Server is the actual HTTP server for Go vanity domains.
	Server struct {
		// Domain is the vanity domain.
		Domain *string
		// Config contains settings for vanity packages.
		Config map[Path]Package
	}
)

// NewPackage returns a new Package given a path, VCS system and VCS URL.
func NewPackage(path, vcssystem, vcsurl string) *Package {
	p := &Package{
		Path:      Path(path),
		VCSSystem: vcssystem,
		VCSURL:    vcsurl,
	}
	return p
}

// NewServer returns a new Vanity Server given domain name and vanity package configuration.
func NewServer(domain string, config map[Path]Package) *Server {
	s := &Server{
		Domain: &domain,
		Config: config,
	}
	return s
}

// GoDocURL returns the HTTP URL to godoc.org.
func (p Package) GoDocURL(domain, path string) string {
	return fmt.Sprintf("https://godoc.org/%v%v", domain, path)
}

// GoImportLink creates the link used in HTML <meta/> tag
// where domain is the domain name of the server.
func (p Package) GoImportLink(domain string) string {
	return fmt.Sprintf("%s%s %s %s", domain, p.Path, p.VCSSystem, p.VCSURL)
}

// GoImportMeta creates the <meta/> HTML tag containing name and content attributes.
func (p Package) GoImportMeta(domain string) string {
	link := p.GoImportLink(domain)
	return fmt.Sprintf(`<meta name="go-import" content="%s">`, link)
}

func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Content-Type", "text/html; charset=utf-8")
	if r.Method != http.MethodGet {
		http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
		return
	}

	pack, ok := s.Config[Path(r.URL.Path)]
	if !ok {
		http.NotFound(w, r)
		return
	}
	if r.FormValue("go-get") != "1" {
		url := pack.GoDocURL(*s.Domain, r.URL.Path)
		http.Redirect(w, r, url, http.StatusTemporaryRedirect)
		return
	}
	fmt.Fprint(w, pack.GoImportMeta(*s.Domain))
}
{'fraction_non_alphanumeric': 0.12010995982237259, 'fraction_numerical': 0.0008458447874814971, 'mean_word_length': 3.720558882235529, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '587965', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1560, 'n_words': 534}