vanity

package module
v1.0.0-alpha.6 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2021 License: BSD-3-Clause Imports: 5 Imported by: 2

README

kkn.fi/vanity

Build Status GoDoc GoReportCard

Concepts

  • VCS is Version Control System (such as 'git')
  • Repo root is the root path the source code repository (such as 'https://github.com/kare')
  • Domain is the internet address where the Go Vanity server is hosted (such as 9fans.net or kkn.fi). Domain is deduced from HTTP request.
  • Path is the path component of the Go package (such as /cmd/tcpproxy in kkn.fi/cmd/tcpproxy)

Features

  • Redirects Go tool to VCS
  • Redirects browsers to pkg.go.dev module server by default. Module Server URL is configurable.
  • Module server URL options are.
  • Redirects HTTP to HTTPS
  • Automatic configuration of packages:
    • All packages are redirected with full path to vcsroot.
    • Packages whose path is prefixed with "/cmd/" redirect automatically to vcsroot by stripping the "/cmd" prefix from the package path. Example: Redirect request "kkn.fi/cmd/tcpproxy" to "github.com/kare/tcpproxy"
  • Configurable logger which is fully compatible with standard log package. Stdout is default.
  • Supports index HTML file in the domain root and configurable static content directory (for images, CSS, and etc).
  • Supports robots.txt file

Installation

go get kkn.fi/vanity

Specification

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultRobotsTxt = `user-agent: *
Allow: /$
Allow: /.static/*$
Disallow: /`

DefaultRobotsTxt is the default value for /robots.txt file.

Functions

func DefaultIndexPageHandler

func DefaultIndexPageHandler(indexFilePath string) http.Handler

DefaultIndexPageHandler serves given indexFilePath over HTTP via http.ServeFile(w, r, name).

func Handler

func Handler(opts ...Option) http.Handler

Handler is an HTTP middleware that redirects browsers to Go module server (pkg.go.dev or similar) or Go tool to VCS repository. VCS repository is git by default. VCS can be set with VCS(). Configurable Logger defaults to os.Stdout. Logger can be configured with SetLogger(). Module server URL is https://pkg.go.dev/ and it can be configured via ModuleServerURL() func. VCSURL() func must be used to set VCS repository URL (such as https://github.com/kare/).

Example
package main

import (
	"log"
	"net/http"
	"os"

	"kkn.fi/vanity"
)

func main() {
	errorLog := log.New(os.Stderr, "vanity: ", log.Ldate|log.Ltime|log.LUTC)
	srv := vanity.Handler(
		vanity.ModuleServerURL("https://pkg.go.dev"),
		vanity.Log(errorLog),
		vanity.VCSURL("https://github.com/kare"),
		vanity.VCS("git"),
		vanity.StaticDir("testdata", "/.static/"),
		vanity.IndexPageHandler(vanity.DefaultIndexPageHandler("testdata"+"/index.html")),
	)
	http.Handle("/", srv)
}
Output:

Types

type Logger added in v0.2.0

type Logger interface {
	Printf(format string, v ...interface{})
}

Logger describes functions available for logging purposes.

type Option

type Option func(http.Handler)

Option represents a functional option for configuring the vanity middleware.

func IndexPageHandler

func IndexPageHandler(index http.Handler) Option

IndexPageHandler sets a handler for index.html page.

func Log

func Log(l Logger) Option

Log sets the logger used by vanity package's error logger.

func ModuleServerURL

func ModuleServerURL(moduleServerURL string) Option

ModuleServerURL sets Go module server address for browser redirect.

func RobotsTxt

func RobotsTxt(robotsTxt string) Option

RobotsTxt takes in option robotsTxt value. If value is empty, the value of DefaultRobotsTxt is used

func StaticDir

func StaticDir(path, URLPath string) Option

StaticDir serves a file system directory over HTTP. Given path is the local file system path to directory. Given urlPath is the path portition of the URL for the server.

func VCS

func VCS(vcs string) Option

VCS sets the version control type.

func VCSURL

func VCSURL(vcsURL string) Option

VCSURL sets the VCS repository url address.

Directories

Path Synopsis
cmd module
vanity Module

Jump to

Keyboard shortcuts

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