~blowry/sourcehut-vanity

ref: d2a4ff166e03fec7e157d4adcf1933f030c66e2d sourcehut-vanity/main.go -rw-r--r-- 1.3 KiB
d2a4ff16Benjamin Lowry add support for branch name specification 11 months ago
                                                                                
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
// sourcehut-vanity hosts an HTTP server that redirects HTTP requests to projects hosted on sourcehut. Example usage:
//
//	$ sourcehut-vanity -u blowry -p 0.0.0.0:8080
//	# localhost:8080/sourcehut-vanity redirects to https://git.sr.ht/~blowry/sourcehut-vanity
//	# localhost:8080/sourcehut-vanity/README.md redirects to https://git.sr.ht/~blowry/sourcehut-vanity/tree/master/README.md
//	# localhost:8080/sourcehut-vanity?go-get=1 returns an HTML page with meta tags so go get can find the correct repository
//
// Compatible with go-get(1) and standard browsers.
// SPDX-License-Identifier: GPL-3.0-or-later
package main

import (
	"ben.gmbh/sourcehut-vanity/vanityserver"
	"fmt"
	"git.sr.ht/~sircmpwn/getopt"
	"net/http"
	"os"
)

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 -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, Branch: branch}))
}