-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog_handlers.go
81 lines (67 loc) · 1.62 KB
/
log_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"golang.org/x/net/context"
)
func httpLogHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
switch r.Method {
case "GET":
return handleWithReadLock(doHTTPLogGetHandler, ctx, w, r)
default:
return sendResponse(w, http.StatusMethodNotAllowed, nil)
}
}
func doHTTPLogGetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
vars := mux.Vars(r)
name := vars["name"]
curr, err := state.Archive.GetDist(name)
switch {
case err == nil:
case os.IsNotExist(err):
return sendResponse(w, http.StatusNotFound, nil)
default:
return &appError{Error: fmt.Errorf("failed to retrieve store reference, %v", err)}
}
w.Write([]byte("["))
defer w.Write([]byte("]"))
displayTrimmed := false
trimmerActive := false
trimAfter := int32(0)
for {
output, err := json.Marshal(curr)
if err != nil {
log.Println("Could not marshal json object, " + err.Error())
continue
}
w.Write(output)
if !displayTrimmed && !trimmerActive && curr.TrimAfter != 0 {
trimmerActive = true
trimAfter = curr.TrimAfter
}
curr, err = state.Archive.GetRelease(curr.ParentID)
if err != nil {
log.Println("Could not get parent, " + err.Error())
return nil
}
// Reached end of history
if state.Archive.EmptyFileID().String() == curr.ParentID.String() ||
curr.Release.String() == "" {
return nil
}
if curr.ParentID == nil {
return nil
}
if !displayTrimmed && trimmerActive {
if trimAfter == 0 {
return nil
}
trimAfter--
}
w.Write([]byte(","))
}
}