2016-02-11 11:57:16 -08:00
|
|
|
package vain
|
2016-02-08 00:15:22 -08:00
|
|
|
|
2016-02-11 12:12:13 -08:00
|
|
|
import (
|
2016-02-13 01:18:06 -08:00
|
|
|
"encoding/json"
|
2016-02-11 12:12:13 -08:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2016-02-13 01:18:06 -08:00
|
|
|
"strings"
|
2016-02-11 12:12:13 -08:00
|
|
|
)
|
2016-02-08 00:15:22 -08:00
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
// NewServer populates a server, adds the routes, and returns it for use.
|
2016-02-16 23:18:54 -08:00
|
|
|
func NewServer(sm *http.ServeMux, store Storage) *Server {
|
2016-02-15 01:10:14 -08:00
|
|
|
s := &Server{
|
2016-02-16 23:18:54 -08:00
|
|
|
storage: store,
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
addRoutes(sm, s)
|
2016-02-15 01:10:14 -08:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server serves up the http.
|
2016-02-08 00:15:22 -08:00
|
|
|
type Server struct {
|
2016-02-16 23:18:54 -08:00
|
|
|
storage Storage
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
|
|
|
|
2016-02-11 12:12:13 -08:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
2016-02-13 01:18:06 -08:00
|
|
|
fmt.Fprintf(w, "<!DOCTYPE html>\n<html><head>\n")
|
|
|
|
for _, p := range s.storage.All() {
|
|
|
|
fmt.Fprintf(w, "%s\n", p)
|
|
|
|
}
|
2016-03-02 22:07:06 -08:00
|
|
|
fmt.Fprintf(w, "</head>\n<body><p>go tool metadata in head</p>\n</html>\n")
|
2016-02-11 12:12:13 -08:00
|
|
|
case "POST":
|
2016-02-13 01:18:06 -08:00
|
|
|
if req.URL.Path == "/" {
|
|
|
|
http.Error(w, fmt.Sprintf("invalid path %q", req.URL.Path), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Package{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&p); err != nil {
|
2016-02-15 01:10:14 -08:00
|
|
|
http.Error(w, fmt.Sprintf("unable to parse json from body: %v", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.Repo == "" {
|
|
|
|
http.Error(w, fmt.Sprintf("invalid repository %q", req.URL.Path), http.StatusBadRequest)
|
2016-02-13 01:18:06 -08:00
|
|
|
return
|
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
if p.Vcs == "" {
|
|
|
|
p.Vcs = "git"
|
|
|
|
}
|
|
|
|
if !valid(p.Vcs) {
|
|
|
|
http.Error(w, fmt.Sprintf("invalid vcs %q", p.Vcs), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-02-16 23:18:54 -08:00
|
|
|
p.path = fmt.Sprintf("%s/%s", req.Host, strings.Trim(req.URL.Path, "/"))
|
2016-02-15 01:10:14 -08:00
|
|
|
if !Valid(p.path, s.storage.All()) {
|
2016-02-13 10:44:41 -08:00
|
|
|
http.Error(w, fmt.Sprintf("invalid path; prefix already taken %q", req.URL.Path), http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
2016-02-14 22:19:41 -08:00
|
|
|
if err := s.storage.Add(p); err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("unable to add package: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-03-01 23:34:31 -08:00
|
|
|
case "DELETE":
|
|
|
|
p := fmt.Sprintf("%s/%s", req.Host, strings.Trim(req.URL.Path, "/"))
|
|
|
|
if !s.storage.Contains(p) {
|
|
|
|
http.Error(w, fmt.Sprintf("package %q not found", p), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := s.storage.Remove(p); err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("unable to delete package: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-02-11 12:12:13 -08:00
|
|
|
default:
|
2016-03-01 23:34:31 -08:00
|
|
|
http.Error(w, fmt.Sprintf("unsupported method %q; accepted: POST, GET, DELETE", req.Method), http.StatusMethodNotAllowed)
|
2016-02-11 12:12:13 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
|
|
|
|
func (s *Server) db(w http.ResponseWriter, req *http.Request) {
|
|
|
|
all := s.storage.All()
|
|
|
|
w.Header().Set("Content-type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(&all)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addRoutes(sm *http.ServeMux, s *Server) {
|
|
|
|
sm.Handle("/", s)
|
|
|
|
sm.HandleFunc("/db/", s.db)
|
|
|
|
}
|