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
|
package main
import (
"flag"
"html/template"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/BurntSushi/toml"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
type options struct {
BindAddress string `toml:"bind_address"`
CanonicalPrefix string `toml:"canonical_prefix"`
}
func run() error {
cfgPath := flag.String("c", "", "path to configuration file")
flag.Parse()
if cfgPath == nil || *cfgPath == "" {
flag.Usage()
os.Exit(1)
}
var opts options
if _, err := toml.DecodeFile(*cfgPath, &opts); err != nil {
return err
}
log.Printf("starting server to listen on %s...", opts.BindAddress)
return http.ListenAndServe(opts.BindAddress, handlePackage(&opts))
}
var tmpl = template.Must(template.New("package").Parse(`<!DOCTYPE html>
<html>
<head>
<meta name="go-import" content="{{ .Name }} git {{ .Repo }}">
</head>
<body>
<h1>{{ .Name }}</h1>
<ul>
<li><a href="https://godoc.org/{{ .Name }}">Godoc</a></li>
<li><a href="{{ .Repo }}">Code</a></li>
</ul>
</body>
</html>`))
// A Package represents a Go package's canonical name and its source git
// repository.
type Package struct {
Name string
Repo *url.URL
}
func handlePackage(opts *options) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(http.StatusText(http.StatusBadRequest)))
return
}
parts := strings.SplitN(r.URL.Path[1:], "/", 2)
pkg := &Package{
Name: path.Join(opts.CanonicalPrefix, parts[0]),
Repo: &url.URL{
Scheme: "https",
Host: "git.burwell.io",
Path: parts[0],
},
}
log.Printf("pkg: %v", pkg)
if err := tmpl.Execute(w, pkg); err != nil {
log.Printf("could not execute template: %v", err)
}
}
}
|