summaryrefslogtreecommitdiff
path: root/main.go
blob: 9d6f5cfc381054181365409fa72cdc5ff399de50 (plain)
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
package main

import (
	"html/template"
	"log"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/virtyx-technologies/readenv"
)

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

type options struct {
	Port              string        `env:"PORT"`
	CacheExpiry       time.Duration `env:"CACHE_EXPIRY"`
	UpstreamTimeout   time.Duration `env:"UPSTREAM_TIMEOUT"`
	SourcehutUsername string        `env:"SRHT_USERNAME"`
	SourcehutToken    string        `env:"SRHT_TOKEN"`
	GithubUsername    string        `env:"GITHUB_USERNAME"`
	GithubToken       string        `env:"GITHUB_TOKEN"`
	CanonicalPrefix   string        `env:"CANONICAL_PREFIX"`
}

func run() error {
	var opts options
	if err := readenv.ReadEnv(&opts); err != nil {
		return err
	}
	cache := &PackageCache{
		CanonicalPrefix: opts.CanonicalPrefix,
		ExpireAfter:     opts.CacheExpiry,
		UpstreamTimeout: opts.UpstreamTimeout,
		Logger:          log.New(os.Stdout, "", log.LstdFlags),
		Hosts: []RepoHost{
			Sourcehut{Username: opts.SourcehutUsername, Token: opts.SourcehutToken},
			Github{Username: opts.GithubUsername, Token: opts.GithubToken},
		},
	}
	handler := handlePackage(cache)
	return http.ListenAndServe(":"+opts.Port, handler)
}

func handlePackage(pkgs *PackageCache) http.HandlerFunc {
	tmpl, err := 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>`)
	if err != nil {
		return nil
	}
	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, ok := pkgs.Get(r.Context(), parts[0])
		if !ok {
			w.WriteHeader(http.StatusNotFound)
			w.Write([]byte(http.StatusText(http.StatusNotFound)))
			return
		}
		log.Printf("pkg: %v", pkg)
		tmpl.Execute(w, pkg)
	}
}