|
|
|
package bugbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EnclosureStore describes an interface to managing Enclosures
|
|
|
|
type EnclosureStore interface {
|
|
|
|
// Retrieves an enclosure by name
|
|
|
|
GetEnclosure(id uint64) (enclosure *Enclosure, ok bool)
|
|
|
|
RecordEvent(id uint64, event string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BugBoxServer struct {
|
|
|
|
EnclosureStore EnclosureStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// This implements the Handler interface
|
|
|
|
func (b *BugBoxServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPost:
|
|
|
|
b.processPostRequest(w, r)
|
|
|
|
case http.MethodGet:
|
|
|
|
b.retrieveEnclosure(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BugBoxServer) processPostRequest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
strId := strings.TrimPrefix(r.URL.Path, "/enclosure/")
|
|
|
|
|
|
|
|
// TODO this is getting big but once again we are just trying to get it to pass
|
|
|
|
if strings.Contains(strId, "events") {
|
|
|
|
event := []byte{}
|
|
|
|
strId = strings.Split(strId, "/")[0]
|
|
|
|
id, _ := strconv.ParseUint(strId, 10, 64)
|
|
|
|
|
|
|
|
// Handle the case where the body is emtpy
|
|
|
|
if r.Body == nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Body.Read(event)
|
|
|
|
err := b.EnclosureStore.RecordEvent(id, string(event))
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
} else {
|
|
|
|
b.createEnclosure(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BugBoxServer) createEnclosure(w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BugBoxServer) retrieveEnclosure(w http.ResponseWriter, r *http.Request) {
|
|
|
|
strId := strings.TrimPrefix(r.URL.Path, "/enclosure/")
|
|
|
|
id, err := strconv.ParseUint(strId, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e, ok := b.EnclosureStore.GetEnclosure(id)
|
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(w, e.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEnclosure retrieves the requested enclosure
|
|
|
|
func GetEnclosure(name string) string {
|
|
|
|
return name
|
|
|
|
}
|