about summary refs log tree commit diff
path: root/tools/govanity/main.go
blob: d74db9ba05f3aed9bf766fa857642389b21fc706 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main

import (
	"bytes"
	"embed"
	"flag"
	"fmt"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"
	"strings"

	"gopkg.in/yaml.v3"
)

//go:embed templates
var tpls embed.FS

type repository struct {
	Name string `yaml:"name"`
	Repo string `yaml:"repo"`
}

type config struct {
	BaseUrl      string       `yaml:"baseUrl"`
	VCS          string       `yaml:"vcs"`
	Repositories []repository `yaml:"repositories"`
}

type moduleTmpl struct {
	Name    string
	Repo    string
	VCS     string
	BaseUrl string
}

func main() {
	flag.Parse()

	cfg, err := loadConfig()
	if err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte("ok"))
	})

	http.HandleFunc("/", goGet(cfg))

	log.Printf("starting web server on :8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func loadConfig() (*config, error) {
	configPath := "./vanity.yaml"

	configYaml, err := ioutil.ReadFile(configPath)
	if err != nil {
		return nil, fmt.Errorf("failed to read configuration file %s: %v", configPath, err)
	}

	var cfg config
	err = yaml.Unmarshal(configYaml, &cfg)
	if err != nil {
		return nil, fmt.Errorf("failed to unmarshall configuration: %v", err)
	}

	return &cfg, nil
}

func goGet(cfg *config) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodGet {
			status := http.StatusMethodNotAllowed
			http.Error(w, http.StatusText(status), status)
			return
		}

		if r.FormValue("go-get") == "1" {
			pathParts := strings.Split(r.URL.Path, "/")
			for _, m := range cfg.Repositories {
				if pathParts[1] == m.Name {
					goGetModule(w, r, m, cfg)
					return
				}
			}
			status := http.StatusNotFound
			http.Error(w, http.StatusText(status), status)
			return
		}
		browserURL(w, r, cfg)
	}
}

func goGetModule(w http.ResponseWriter, r *http.Request, m repository, cfg *config) {
	tmpl, err := template.ParseFS(tpls, "templates/module.html.tpl")
	if err != nil {
		log.Fatal(err)
	}
	mod := moduleTmpl{
		VCS:     cfg.VCS,
		BaseUrl: cfg.BaseUrl,
		Name:    m.Name,
		Repo:    m.Repo,
	}
	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, mod); err != nil {
		log.Printf("error: %+v", err)
		status := http.StatusInternalServerError
		http.Error(w, http.StatusText(status), status)
	} else {
		w.Header().Set("Cache-Control", "no-store")
		w.Write(buf.Bytes())
	}
}

func browserURL(w http.ResponseWriter, r *http.Request, cfg *config) {
	tmpl, err := template.ParseFS(tpls, "templates/index.html.tpl")
	if err != nil {
		log.Fatal(err)
	}
	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, cfg); err != nil {
		log.Printf("error: %+v", err)
		status := http.StatusInternalServerError
		http.Error(w, http.StatusText(status), status)
	} else {
		w.Header().Set("Cache-Control", "no-store")
		w.Write(buf.Bytes())
	}
}