aboutsummaryrefslogtreecommitdiff
path: root/handler.go
blob: 5144cafc65f901318b6a47e09b8af8d968099f0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main

import (
	"fmt"
	"html/template"
	"net/http"
	"strings"

	"go.uber.org/sally/templates"
)

var (
	indexTemplate = template.Must(
		template.New("index.html").Parse(templates.Index))
	packageTemplate = template.Must(
		template.New("package.html").Parse(templates.Package))
)

// CreateHandler creates a Sally http.Handler
func CreateHandler(config *Config) http.Handler {
	mux := http.NewServeMux()

	mux.Handle("/", &indexHandler{config: config})
	for name, pkg := range config.Packages {
		handler := newPackageHandler(config, name, pkg)
		// Double-register so that "/foo"
		// does not redirect to "/foo/" with a 300.
		mux.Handle("/"+name, handler)
		mux.Handle("/"+name+"/", handler)
	}

	return mux
}

type indexHandler struct {
	config *Config
}

func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Index handler only supports '/'.
	// ServeMux will call us for any '/foo' that is not a known package.
	if r.Method != http.MethodGet || r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}

	if err := indexTemplate.Execute(w, h.config); err != nil {
		http.Error(w, err.Error(), 500)
	}
}

type packageHandler struct {
	// Hostname of the godoc server, e.g. "godoc.org".
	godocHost string

	// Name of the package relative to the vanity base URL.
	// For example, "zap" for "go.uber.org/zap".
	name string

	// Path at which the Git repository is hosted.
	// For example, "github.com/uber-go/zap".
	gitURL string

	// Default branch of the Git repository.
	defaultBranch string

	// Canonical import path for the package.
	canonicalURL string
}

func newPackageHandler(cfg *Config, name string, pkg PackageConfig) *packageHandler {
	baseURL := cfg.URL
	if pkg.URL != "" {
		baseURL = pkg.URL
	}
	canonicalURL := fmt.Sprintf("%s/%s", baseURL, name)

	return &packageHandler{
		godocHost:     cfg.Godoc.Host,
		name:          name,
		canonicalURL:  canonicalURL,
		gitURL:        pkg.Repo,
		defaultBranch: pkg.Branch,
	}
}

func (h *packageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		http.NotFound(w, r)
		return
	}

	// Extract the relative path to subpackages, if any.
	//	"/foo/bar" => "/bar"
	//	"/foo" => ""
	relPath := strings.TrimPrefix(r.URL.Path, "/"+h.name)

	data := struct {
		Repo         string
		Branch       string
		CanonicalURL string
		GodocURL     string
	}{
		Repo:         h.gitURL,
		Branch:       h.defaultBranch,
		CanonicalURL: h.canonicalURL,
		GodocURL:     fmt.Sprintf("https://%s/%s%s", h.godocHost, h.canonicalURL, relPath),
	}
	if err := packageTemplate.Execute(w, data); err != nil {
		http.Error(w, err.Error(), 500)
	}
}