Clean up vanityserver cmd

git-svn-id: file:///srv/svn/repo/toyohime/trunk@90 922d331f-388e-da47-97a9-ad700dc0b8b9
This commit is contained in:
jonbetti 2018-06-06 04:23:59 +00:00
parent 4872118f0b
commit 43bb62fe5b
2 changed files with 73 additions and 20 deletions

View File

@ -0,0 +1,19 @@
# vanityserver
Runs a barebones vanity server over HTTP.
## Usage
```
./vanityserver fqdn [repo file]
```
If repo file is not given, "./repos" is used. The file has the following format:
```
pkgroot vcsScheme://vcsHost/user/repo
pkgroot2 vcsScheme://vcsHost/user/repo2
```
vcsHost is either a Gogs server (that's what I use) or github.com. I'm open to
supporting other VCSs but I'm not sure what that would look like.

View File

@ -1,9 +1,15 @@
/*
Runs a barebones vanity server over HTTP.
*/
package main // go.jonnrb.io/vanity
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
@ -11,9 +17,18 @@ import (
"go.jonnrb.io/vanity"
)
var host = os.Getenv("HOST")
var host string
func serveRepo(mux *http.ServeMux, root string, u *url.URL) {
vcsScheme, vcsHost := u.Scheme, u.Host
// Get ["", "user", "repo"].
pathParts := strings.Split(u.Path, "/")
if len(pathParts) != 3 {
log.Fatalf("Repo URL must be of the form vcsScheme://vcsHost/user/repo but got %q", u.String())
}
user, repo := pathParts[1], pathParts[2]
func serveRepo(mux *http.ServeMux, root, vcsScheme, vcsHost, user, repo string) {
importPath := host + "/" + root
var h http.Handler
if vcsHost == "github.com" {
@ -25,28 +40,47 @@ func serveRepo(mux *http.ServeMux, root, vcsScheme, vcsHost, user, repo string)
mux.Handle("/"+root+"/", h)
}
func main() {
mux := http.NewServeMux()
if f, err := os.Open("/repos"); err != nil {
log.Fatalf("Error opening conf: %v", err)
} else {
sc := bufio.NewScanner(f)
func buildMux(mux *http.ServeMux, r io.Reader) {
sc := bufio.NewScanner(r)
for sc.Scan() {
fields := strings.Fields(sc.Text())
switch len(fields) {
case 0:
continue
case 5:
case 2:
// Pass
default:
log.Fatalf("Expected line of form \"path vcsScheme vcsHost user repo\" but got %q", sc.Text())
log.Fatalf("Expected line of form \"path vcsScheme://vcsHost/user/repo\" but got %q", sc.Text())
}
path, vcsScheme, vcsHost, user, repo :=
fields[0], fields[1], fields[2], fields[3], fields[4]
serveRepo(mux, path, vcsScheme, vcsHost, user, repo)
path := fields[0]
u, err := url.Parse(fields[1])
if err != nil {
log.Fatalf("Repo was not a valid URL: %q", fields[1])
}
serveRepo(mux, path, u)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "usage: %s fqdn [repos file]", os.Args[0])
os.Exit(-1)
}
host = os.Args[1]
reposPath := "repos"
if len(os.Args) > 2 {
reposPath = os.Args[2]
}
mux := http.NewServeMux()
if f, err := os.Open(reposPath); err != nil {
log.Fatalf("Error opening repos path: %v", err)
} else {
buildMux(mux, f)
}
srv := &http.Server{