Compare commits

..

No commits in common. '121c2af7b0627ca6c292ca0a017affcff7c41b2c' and 'a43cae385c048eeb35b00e17ae8d8efa5a250501' have entirely different histories.

@ -1,10 +1,11 @@
#GOFLAGS=""
LDFLAGS=-ldflags "-s -w" LDFLAGS=-ldflags "-s -w"
BINARY=metrorama BINARY=metrorama
.DEFAULT_GOAL: $(BINARY) .DEFAULT_GOAL: $(BINARY)
$(BINARY): clean $(BINARY): clean
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build ${GOFLAGS} ${LDFLAGS} -o ${BINARY} go build ${GOFLAGS} ${LDFLAGS} -o ${BINARY}
install: install:
go install ${GOFLAGS} ${LDFLAGS} -o ${BINARY} go install ${GOFLAGS} ${LDFLAGS} -o ${BINARY}

@ -1,24 +0,0 @@
#!/bin/bash
# Remember to run buildah unshare before launching this
# Build container
echo "Start creating first stage "
buildctrl=$(buildah from golang:1.15.2)
buildmnt=$(buildah mount $buildctrl)
buildah copy $buildctrl *.go .
buildah copy $buildctrl Makefile .
buildah run $buildctrl make deps
buildah run $buildctrl make
# Runtime container
echo "Start creating second stage"
buildctrl2=$(buildah from alpine:latest)
buildmnt2=$(buildah mount $builctrl2)
buildah copy $buildctrl2 $buildmnt/go/metrorama
buildah config --cmd ./metrorama $buildctrl2
buildah config --port 8080 $buildctrl2
buildah unmount $buildctrl
buildah unmount $buildctrl2
buildah commit $buildctrl2 metrorama:latest
buildah rm $buildctrl $buildctrl2

@ -86,7 +86,7 @@ func (t *Test) collectData() {
return // really needed? return // really needed?
} }
func (t *Test) Start(w http.ResponseWriter, req *http.Request) { func (t *Test) startTest(w http.ResponseWriter, req *http.Request) {
// Check if another collectData is running (better: check sync.Mutex) // Check if another collectData is running (better: check sync.Mutex)
if t.Status == "ONGOING" { if t.Status == "ONGOING" {
http.Error(w, "Another test is currently ongoing, stop or try again later", 409) //better error code http.Error(w, "Another test is currently ongoing, stop or try again later", 409) //better error code
@ -144,7 +144,7 @@ func (t *Test) Start(w http.ResponseWriter, req *http.Request) {
go t.collectData() go t.collectData()
} }
func (t *Test) Stop(w http.ResponseWriter, req *http.Request) { func (t *Test) stopTest(w http.ResponseWriter, req *http.Request) {
if t.Status != "ONGOING" { if t.Status != "ONGOING" {
e := fmt.Sprintf("Cannot stop test in %s status: try start first", t.Status) e := fmt.Sprintf("Cannot stop test in %s status: try start first", t.Status)
http.Error(w, e, 400) http.Error(w, e, 400)
@ -157,54 +157,18 @@ func (t *Test) Stop(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "%v\n", t.Result) fmt.Fprintf(w, "%v\n", t.Result)
} }
func (t *Test) getStatus(w http.ResponseWriter, req *http.Request) { func (t *Test) testStatus(w http.ResponseWriter, req *http.Request) {
qStream := req.URL.Query().Get("stream")
qFormat := req.URL.Query().Get("format") qFormat := req.URL.Query().Get("format")
flusher, ok := w.(http.Flusher)
if !ok {
if qStream == "true" {
log.Printf("Http server does not support flusher\n")
http.Error(w, "Server does not support flusher",
http.StatusInternalServerError)
return
}
}
if qStream == "true" {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
}
if qFormat == "json" { if qFormat == "json" {
for { b, err := json.Marshal(t.Result)
b, err := json.Marshal(t.Result) if err != nil {
if err != nil { log.Printf("Cannot marshal json: %s\n", err)
log.Printf("Cannot marshal json: %s\n", err) http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), 500) return
return
}
fmt.Fprintf(w, "%s\n", b)
if qStream == "true" {
time.Sleep(time.Duration(t.Rate) * time.Millisecond)
flusher.Flush()
} else {
return
}
} }
fmt.Fprintf(w, "%s\n", b)
} else { } else {
for { fmt.Fprintf(w, "%v\n", t.Result)
fmt.Fprintf(w, "%+ v\n", t.Result)
if qStream == "true" {
time.Sleep(time.Duration(t.Rate) * time.Millisecond)
flusher.Flush()
} else {
return
}
}
fmt.Fprintf(w, "Test %s, status: %s\n", t.Name, t.Status) fmt.Fprintf(w, "Test %s, status: %s\n", t.Name, t.Status)
} }
} }
@ -229,9 +193,9 @@ func main() {
t := newTest() t := newTest()
log.Printf("%+v\n", t) log.Printf("%+v\n", t)
http.HandleFunc("/start", t.Start) http.HandleFunc("/start", t.startTest)
http.HandleFunc("/status", t.getStatus) http.HandleFunc("/status", t.testStatus)
http.HandleFunc("/stop", t.Stop) http.HandleFunc("/stop", t.stopTest)
log.Printf("Listening on %s\n", *listen) log.Printf("Listening on %s\n", *listen)
log.Fatal(http.ListenAndServe(*listen, nil)) log.Fatal(http.ListenAndServe(*listen, nil))

Loading…
Cancel
Save