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 package main // go.jonnrb.io/vanity
import ( import (
"bufio" "bufio"
"fmt"
"io"
"log" "log"
"net/http" "net/http"
"net/url"
"os" "os"
"strings" "strings"
"time" "time"
@ -11,9 +17,18 @@ import (
"go.jonnrb.io/vanity" "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 importPath := host + "/" + root
var h http.Handler var h http.Handler
if vcsHost == "github.com" { if vcsHost == "github.com" {
@ -25,28 +40,47 @@ func serveRepo(mux *http.ServeMux, root, vcsScheme, vcsHost, user, repo string)
mux.Handle("/"+root+"/", h) mux.Handle("/"+root+"/", h)
} }
func main() { func buildMux(mux *http.ServeMux, r io.Reader) {
mux := http.NewServeMux() sc := bufio.NewScanner(r)
if f, err := os.Open("/repos"); err != nil {
log.Fatalf("Error opening conf: %v", err)
} else {
sc := bufio.NewScanner(f)
for sc.Scan() { for sc.Scan() {
fields := strings.Fields(sc.Text()) fields := strings.Fields(sc.Text())
switch len(fields) { switch len(fields) {
case 0: case 0:
continue continue
case 5: case 2:
// Pass // Pass
default: 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 := path := fields[0]
fields[0], fields[1], fields[2], fields[3], fields[4] u, err := url.Parse(fields[1])
serveRepo(mux, path, vcsScheme, vcsHost, user, repo) 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{ srv := &http.Server{