Added an http server

This commit is contained in:
Stephen McQuay 2016-02-08 00:15:22 -08:00
parent 95b282ea42
commit b3585cbc96
3 changed files with 84 additions and 1 deletions

View File

@ -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)
}
}

15
server.go Normal file
View File

@ -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) {
}

18
ysv.go
View File

@ -5,6 +5,24 @@ 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.
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