aboutsummaryrefslogtreecommitdiff
path: root/cmd/within.website/main.go
blob: 28e54addd374fb7145f1788bd8c604c86b7a1282 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Command within.website is the vanity import server for https://within.website.
package main

import (
	"flag"
	"fmt"
	"log/slog"
	"net/http"
	"os"

	"github.com/a-h/templ"
	"go.jetpack.io/tyson"
	"within.website/x/internal"
	"within.website/x/web/vanity"
	"within.website/x/xess"
)

var (
	domain      = flag.String("domain", "within.website", "domain this is run on")
	port        = flag.String("port", "2134", "HTTP port to listen on")
	tysonConfig = flag.String("tyson-config", "./config.ts", "TySON config file")
)

type Repo struct {
	Kind        string `json:"kind"`
	Domain      string `json:"domain"`
	User        string `json:"user"`
	Repo        string `json:"repo"`
	Description string `json:"description"`
}

func (r Repo) URL() string {
	return fmt.Sprintf("https://%s/%s/%s", r.Domain, r.User, r.Repo)
}

func (r Repo) GodocURL() string {
	return fmt.Sprintf("https://pkg.go.dev/within.website/%s", r.Repo)
}

func (r Repo) GodocBadge() string {
	return fmt.Sprintf("https://pkg.go.dev/badge/within.website/%s.svg", r.Repo)
}

func (r Repo) LogValue() slog.Value {
	return slog.GroupValue(
		slog.String("kind", r.Kind),
		slog.String("domain", r.Domain),
		slog.String("user", r.User),
		slog.String("repo", r.Repo),
	)
}

func (r Repo) RegisterHandlers(mux *http.ServeMux, lg *slog.Logger) {
	switch r.Kind {
	case "gitea":
		mux.Handle("/"+r.Repo, vanity.GogsHandler(*domain+"/"+r.Repo, r.Domain, r.User, r.Repo, "https"))
		mux.Handle("/"+r.Repo+"/", vanity.GogsHandler(*domain+"/"+r.Repo, r.Domain, r.User, r.Repo, "https"))
	case "github":
		mux.Handle("/"+r.Repo, vanity.GitHubHandler(*domain+"/"+r.Repo, r.User, r.Repo, "https"))
		mux.Handle("/"+r.Repo+"/", vanity.GitHubHandler(*domain+"/"+r.Repo, r.User, r.Repo, "https"))
	}
	lg.Debug("registered repo handler", "repo", r)
}

//go:generate go tool templ generate

func main() {
	internal.HandleStartup()

	lg := slog.Default().With("domain", *domain, "configPath", *tysonConfig)

	var repos []Repo
	if err := tyson.Unmarshal(*tysonConfig, &repos); err != nil {
		lg.Error("can't unmarshal config", "err", err)
		os.Exit(1)
	}

	mux := http.NewServeMux()

	for _, repo := range repos {
		repo.RegisterHandlers(mux, lg)
	}

	xess.Mount(mux)

	mux.Handle("/{$}", templ.Handler(
		xess.Base(
			"within.website Go packages",
			nil,
			nil,
			Index(repos),
			footer(),
		),
	))

	mux.Handle("/", templ.Handler(
		xess.Simple("Not found", NotFound()),
		templ.WithStatus(http.StatusNotFound)),
	)

	mux.Handle("/.x.botinfo", templ.Handler(
		xess.Simple("x repo bots", BotInfo()),
	))

	lg.Info("listening", "port", *port)
	http.ListenAndServe(":"+*port, mux)
}