From b3585cbc9638fd7a5641323c059fa9afc67443c9 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Mon, 8 Feb 2016 00:15:22 -0800 Subject: [PATCH] Added an http server --- cmd/ysvd/main.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++- server.go | 15 ++++++++++++++ ysv.go | 18 +++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 server.go diff --git a/cmd/ysvd/main.go b/cmd/ysvd/main.go index da29a2c..dfe130e 100644 --- a/cmd/ysvd/main.go +++ b/cmd/ysvd/main.go @@ -1,4 +1,54 @@ package main -func main() { +import ( + "fmt" + "log" + "net/http" + "os" + + "mcquay.me/ysv" + + "github.com/kelseyhightower/envconfig" +) + +const usage = `ysvd + +environment vars: + +YSV_PORT: tcp listen port +` + +type config struct { + Port int +} + +func main() { + c := &config{ + Port: 4040, + } + if err := envconfig.Process("ysv", c); err != nil { + fmt.Fprintf(os.Stderr, "problem processing environment: %v", err) + os.Exit(1) + } + if len(os.Args) > 1 { + switch os.Args[1] { + case "env", "e", "help", "h": + fmt.Fprintf(os.Stderr, "%s\n", usage) + os.Exit(1) + } + } + 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() + ysv.NewServer(sm) + addr := fmt.Sprintf(":%d", c.Port) + if err := http.ListenAndServe(addr, sm); err != nil { + log.Printf("problem with http server: %v", err) + os.Exit(1) + } } diff --git a/server.go b/server.go new file mode 100644 index 0000000..eba1deb --- /dev/null +++ b/server.go @@ -0,0 +1,15 @@ +package ysv + +import "net/http" + +type Server struct { +} + +func NewServer(sm *http.ServeMux) *Server { + s := &Server{} + addRoutes(sm, s) + return s +} + +func addRoutes(sm *http.ServeMux, s *Server) { +} diff --git a/ysv.go b/ysv.go index cb92fb0..882e808 100644 --- a/ysv.go +++ b/ysv.go @@ -5,6 +5,24 @@ when searching for packages: +this is simply a service for aggregating a collection of prefix, vcs, and +repo-root tuples, and serving the appropriate header over http. + +API + +In order to add a new package POST a json object to the following route: + +POST /v0/package/ + +A sample json object: + + { + "path": "mcquay.me/ysv", + "repo": "https://s.mcquay.me/sm/ysv" + } + +Naming + ysv stands for You're so Vain, the song by Carly Simon. */ package ysv