~blowry/sourcehut-vanity

d2a4ff166e03fec7e157d4adcf1933f030c66e2d — Benjamin Lowry 1 year, 11 months ago 5be0688 main master
add support for branch name specification
2 files changed, 9 insertions(+), 5 deletions(-)

M main.go
M vanityserver/vanityserver.go
M main.go => main.go +4 -2
@@ 20,16 20,18 @@ import (
func main() {
	var port string
	var user string
	var branch string
	getopt.StringVar(&port, "p", "0.0.0.0:8080", "address and port to bind to")
	getopt.StringVar(&user, "u", "", "username to redirect to")
	getopt.StringVar(&branch, "b", "master", "branch to redirect to")
	if e := getopt.Parse(); e != nil {
		panic(e)
	}
	if user == "" || port == "" {
		fmt.Fprintf(os.Stderr, "usage: sourcehut-vanity -u username [-p 0.0.0.0:8080]\n")
		fmt.Fprintf(os.Stderr, "usage: sourcehut-vanity -u username -b branch [-p 0.0.0.0:8080]\n")
		getopt.PrintDefaults()
		os.Exit(1)
	}
	fmt.Printf("Serving %v's projects on %v\n", user, port)
	fmt.Println(http.ListenAndServe(port, vanityserver.VanityServer{Username: user}))
	fmt.Println(http.ListenAndServe(port, vanityserver.VanityServer{Username: user, Branch: branch}))
}

M vanityserver/vanityserver.go => vanityserver/vanityserver.go +5 -3
@@ 21,6 21,7 @@ import (

type VanityServer struct {
	Username string
	Branch string
}

type templatedata struct {


@@ 28,6 29,7 @@ type templatedata struct {
	ImportHost  string
	ProjectName string
	Redir       string
	Branch      string
}

const page = `<!DOCTYPE html>


@@ 35,7 37,7 @@ const page = `<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="{{.ImportHost}} git https://git.sr.ht/~{{.User}}/{{.ProjectName}}">
<meta name="go-source" content="{{.ImportHost}} https://git.sr.ht/~{{.User}}/{{.ProjectName}} https://git.sr.ht/~{{.User}}/{{.ProjectName}}/tree/master{/dir} https://git.sr.ht/~{{.User}}/{{.ProjectName}}/tree/master{/dir}/{file}#L{line}">
<meta name="go-source" content="{{.ImportHost}} https://git.sr.ht/~{{.User}}/{{.ProjectName}} https://git.sr.ht/~{{.User}}/{{.ProjectName}}/tree/{{.Branch}}{/dir} https://git.sr.ht/~{{.User}}/{{.ProjectName}}/tree/{{.Branch}}{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url={{.Redir}}">
</head>
<body>


@@ 53,12 55,12 @@ func (server VanityServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	if len(path) > 2 && path[2] != "" {
		destination = baseurl + "/tree/master/" + strings.Replace(r.URL.Path, "/"+path[1]+"/", "", 1)
		destination = baseurl + "/tree/" + server.Branch + "/" + strings.Replace(r.URL.Path, "/"+path[1]+"/", "", 1)
	} else {
		destination = baseurl
	}
	if r.Form.Get("go-get") == "1" { // go-get(1)
		tmpl.Execute(rw, templatedata{User: server.Username, ImportHost: r.Host + "/" + projectname, ProjectName: projectname, Redir: destination})
		tmpl.Execute(rw, templatedata{User: server.Username, Branch: server.Branch, ImportHost: r.Host + "/" + projectname, ProjectName: projectname, Redir: destination})
	} else { // Standard browser
		http.Redirect(rw, r, destination, 302)
	}