2016-02-11 12:03:03 -08:00
|
|
|
/*
|
2016-02-16 23:27:53 -08:00
|
|
|
vaind, a webserver for hosting go get vanity urls.
|
2016-02-15 01:09:11 -08:00
|
|
|
|
|
|
|
The go get command searches for the following header when searching for
|
|
|
|
packages:
|
|
|
|
|
|
|
|
<meta name="go-import" content="import-prefix vcs repo-root">
|
|
|
|
|
|
|
|
this is simply a service for aggregating a collection of prefix, vcs, and
|
|
|
|
repo-root tuples, and serving the appropriate header over http. For more
|
|
|
|
information please refer to the documentation for the go tool found at
|
|
|
|
https://golang.org/cmd/go/#hdr-Remote_import_paths
|
|
|
|
|
|
|
|
API
|
|
|
|
|
2016-02-16 23:27:53 -08:00
|
|
|
Assume an instance of vaind at example.org. In order to add a package
|
2016-02-15 01:09:11 -08:00
|
|
|
example.org/foo that points at bitbucket.org/example/foo (a mercurial
|
|
|
|
repository) POST the following json object:
|
|
|
|
|
|
|
|
{
|
|
|
|
"vcs": "mercurial",
|
|
|
|
"repo": "https://bitbucket.org/example/foo"
|
|
|
|
}
|
|
|
|
|
|
|
|
to https://example.org/foo.
|
|
|
|
|
|
|
|
Doing so, then visiting https://example.org/foo?go-get=1 will yield a header
|
|
|
|
that looks like:
|
|
|
|
|
|
|
|
|
|
|
|
<meta name="go-import" content="example.org/foo hg https://bitbucket.org/foo">
|
|
|
|
|
|
|
|
|
|
|
|
The json object sent to server can have two fields: "repo" and "vcs". "repo" is
|
|
|
|
required; leaving off the "vcs" member defaults to "git".
|
2016-03-02 00:06:12 -08:00
|
|
|
|
|
|
|
In order to delete a package:
|
|
|
|
|
|
|
|
DELETE /<package name>
|
2016-02-11 12:03:03 -08:00
|
|
|
*/
|
2016-02-07 23:54:55 -08:00
|
|
|
package main
|
|
|
|
|
2016-02-08 00:15:22 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-05-14 21:30:58 -07:00
|
|
|
"time"
|
2016-02-08 00:15:22 -08:00
|
|
|
|
2016-02-11 11:57:16 -08:00
|
|
|
"mcquay.me/vain"
|
2016-02-08 00:15:22 -08:00
|
|
|
|
|
|
|
"github.com/kelseyhightower/envconfig"
|
|
|
|
)
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
const usage = "vaind [init] <dbname>"
|
2016-02-08 00:15:22 -08:00
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Port int
|
2016-04-17 18:26:45 -07:00
|
|
|
|
|
|
|
Cert string
|
|
|
|
Key string
|
2016-04-23 22:16:40 -07:00
|
|
|
|
|
|
|
Static string
|
2016-05-14 21:30:58 -07:00
|
|
|
|
|
|
|
EmailTimeout time.Duration `envconfig:"email_timeout"`
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
|
|
|
|
2016-02-07 23:54:55 -08:00
|
|
|
func main() {
|
2016-04-11 20:43:18 -07:00
|
|
|
if len(os.Args) < 2 {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", usage)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Args[1] == "init" {
|
|
|
|
if len(os.Args) != 3 {
|
|
|
|
fmt.Fprintf(os.Stderr, "missing db name: %s\n", usage)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := vain.NewDB(os.Args[2])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "couldn't open db: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
if err := db.Init(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "problem initializing the db: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := vain.NewDB(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "couldn't open db: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:15:22 -08:00
|
|
|
c := &config{
|
2016-05-14 21:30:58 -07:00
|
|
|
Port: 4040,
|
|
|
|
EmailTimeout: 5 * time.Minute,
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
if err := envconfig.Process("vain", c); err != nil {
|
2016-02-08 00:15:22 -08:00
|
|
|
fmt.Fprintf(os.Stderr, "problem processing environment: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-05-14 21:30:58 -07:00
|
|
|
log.Printf("%+v", c)
|
2016-02-08 00:15:22 -08:00
|
|
|
if len(os.Args) > 1 {
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "env", "e", "help", "h":
|
2016-02-14 22:19:41 -08:00
|
|
|
fmt.Printf("%s\n", usage)
|
|
|
|
os.Exit(0)
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hostname := "localhost"
|
|
|
|
if hn, err := os.Hostname(); err != nil {
|
|
|
|
log.Printf("problem getting hostname:", err)
|
|
|
|
} else {
|
|
|
|
hostname = hn
|
|
|
|
}
|
|
|
|
log.Printf("serving at: http://%s:%d/", hostname, c.Port)
|
|
|
|
sm := http.NewServeMux()
|
2016-05-14 21:30:58 -07:00
|
|
|
vain.NewServer(sm, db, c.Static, c.EmailTimeout)
|
2016-02-08 00:15:22 -08:00
|
|
|
addr := fmt.Sprintf(":%d", c.Port)
|
2016-04-17 18:26:45 -07:00
|
|
|
|
|
|
|
if c.Cert == "" || c.Key == "" {
|
|
|
|
log.Printf("INSECURE MODE")
|
|
|
|
if err := http.ListenAndServe(addr, sm); err != nil {
|
|
|
|
log.Printf("problem with http server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := http.ListenAndServeTLS(addr, c.Cert, c.Key, sm); err != nil {
|
|
|
|
log.Printf("problem with http server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
2016-02-07 23:54:55 -08:00
|
|
|
}
|