summaryrefslogtreecommitdiff
path: root/repomap.go
blob: 0fa5d180fd1e3312ba2c90d8be2b9fe5adf75db1 (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
package main

// parses /etc/gowebd/repomap
// this file defines what repositories show up on go.wit.com

import (
	"fmt"
	"net/http"
	"os"
	"strings"

	"go.wit.com/log"
)

// this makes the "go-source" / "go-import" page
// this redirects the go path "go.wit.com/apps/go-clone" to the git path "gitea.wit.com/wit/go-clone"
func repoHTML(w http.ResponseWriter, gourl string, realurl string) {
	realurl = "https://" + realurl
	log.Info("go repo =", gourl, "real url =", realurl)
	fmt.Fprintln(w, "<!DOCTYPE html>")
	fmt.Fprintln(w, "<html>")
	fmt.Fprintln(w, "<head>")
	// fmt.Fprintln(w,
	fmt.Fprintln(w, "<meta name=\"go-import\" content=\"", gourl, "git", realurl+"\">")
	fmt.Fprintln(w, "<meta name=\"go-source\" content=\"", gourl, realurl, realurl+"/tree/master{/dir}", realurl+"tree/master{/dir}/{file}#L{line}", "\"", ">")

	fmt.Fprintln(w, "<meta http-equiv=\"refresh\" content=\"0; url="+realurl+"\">")
	fmt.Fprintln(w, "</head>")
	fmt.Fprintln(w, "<body>")
	fmt.Fprintln(w, "Redirecting to git repo url. Please <a href=\""+realurl+"\">move along</a>.\"")
	fmt.Fprintln(w, "</body>")
	fmt.Fprintln(w, "</html>")
}

func findkey(url string) (string, string) {
	key := "go.wit.com" + url
	if repoMap[key] != "" {
		return key, repoMap[key]
	}
	return key, ""
	// parts := strings.Split(key, "/")
}

func readRepomap() {
	var pfile []byte
	var err error

	pfile, err = os.ReadFile(REPOMAP)
	if err != nil {
		log.Error(err, "no repository map file found in /etc/gowebd/")
		log.Error(err, "falling back to the repository map file built into the gowebd binary")
		pfile, err = resources.ReadFile("resources/repomap")
		if err != nil {
			log.Error(err, "missing repomap in the binary")
			return
		}
	}
	configfile = strings.Split(string(pfile), "\n")
	for _, line := range configfile {
		fields := strings.Fields(line)
		if len(fields) < 2 {
			continue
		}
		gopath := fields[0]
		giturl := fields[1]
		repoMap[gopath] = giturl

		repo := forge.FindByGoPath(gopath)
		if repo != nil {
			gitMap[gopath] = repo
		} else {
			log.Info("repo =", gopath, "real url =", repoMap[gopath], "not found")
		}
	}
}