-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (45 loc) · 1.2 KB
/
main.go
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
package main
import (
"embed"
"flag"
"html/template"
"net/http"
"github.com/blackieops/synonym/config"
"github.com/gin-gonic/gin"
)
//go:embed tmpl/*
var tmplFS embed.FS
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)
}
r := gin.Default()
tmpls := template.Must(template.New("").ParseFS(tmplFS, "tmpl/*"))
r.SetHTMLTemplate(tmpls)
r.GET("/*importPath", handleGetRepo(conf))
r.Run(":6969")
}
func handleGetRepo(conf *config.Config) func(c *gin.Context) {
return func(c *gin.Context) {
name := c.Param("importPath")[1:]
target := buildTarget(conf, name)
if c.Query("go-get") == "1" {
c.HTML(http.StatusOK, "go-get.html", gin.H{
"Source": buildSource(conf, name),
"Target": target,
"DefaultBranchName": conf.DefaultBranchName,
})
return
}
c.Redirect(http.StatusPermanentRedirect, target)
}
}
func buildTarget(config *config.Config, repo string) string {
return "https://" + config.TargetBaseURL + "/" + repo
}
func buildSource(config *config.Config, repo string) string {
return config.Hostname + "/" + repo
}