-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (65 loc) 路 1.75 KB
/
Copy pathmain.go
File metadata and controls
84 lines (65 loc) 路 1.75 KB
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
package main
import (
"embed"
"flag"
"fmt"
"html/template"
"net/http"
"strings"
"github.com/blackieops/synonym/config"
)
//go:embed tmpl/*
var tmplFS embed.FS
var tmpls = template.Must(template.New("").ParseFS(tmplFS, "tmpl/*"))
var configPath = flag.String("config", "config.yaml", "Path to configuration file.")
func main() {
flag.Parse()
conf, err := config.LoadConfig(*configPath)
if err != nil {
panic(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/_healthz", handleHealthz)
mux.HandleFunc("/", handleGetRepo(conf))
if err := http.ListenAndServe(fmt.Sprintf(":%d", conf.Port), mux); err != nil {
panic(err)
}
}
func handleHealthz(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
}
func handleGetRepo(conf *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:]
target := buildTarget(conf, name)
if r.URL.Query().Get("go-get") == "1" {
data := struct {
Source string
Target string
DefaultBranchName string
}{
Source: buildSource(conf, name),
Target: target,
DefaultBranchName: conf.DefaultBranchName,
}
if err := tmpls.ExecuteTemplate(w, "go-get.html", data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
http.Redirect(w, r, target, http.StatusMovedPermanently)
}
}
func buildTarget(config *config.Config, repo string) string {
target := config.TargetBaseURL + "/" + repo
for _, mapping := range config.CustomMappings {
if strings.TrimLeft(mapping.Path, "/") == repo {
target = mapping.Target
}
}
return "https://" + target
}
func buildSource(config *config.Config, repo string) string {
return config.Hostname + "/" + repo
}