You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.9 KiB
Go

package bugbox
import (
"fmt"
"net/http"
"strconv"
"strings"
)
2 months ago
// 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
}
2 months ago
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 {
2 months ago
w.WriteHeader(http.StatusNotFound)
return
2 months ago
}
fmt.Fprint(w, e.String())
2 months ago
}
// GetEnclosure retrieves the requested enclosure
func GetEnclosure(name string) string {
return name
}