Skip to content

Commit

Permalink
Add http endpoint for simple storage utilization monitoring
Browse files Browse the repository at this point in the history
This endpoint is /storage-status
  • Loading branch information
espebra committed Aug 8, 2022
1 parent 4384c78 commit 61861bc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (h *HTTP) Init() (err error) {
h.router.HandleFunc("/", h.BanLookup(h.Index)).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/", h.BanLookup(h.UploadFile)).Methods(http.MethodPost)
h.router.HandleFunc("/filebin-status", h.FilebinStatus).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/storage-status", h.StorageStatus).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/robots.txt", h.Robots).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/about", h.BanLookup(h.About)).Methods(http.MethodHead, http.MethodGet)
h.router.HandleFunc("/api", h.BanLookup(h.API)).Methods(http.MethodHead, http.MethodGet)
Expand Down
39 changes: 39 additions & 0 deletions http_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,45 @@ func (h *HTTP) FilebinStatus(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, string(out))
}

func (h *HTTP) StorageStatus(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Robots-Tag", "none")
w.Header().Set("Cache-Control", "max-age=1")

type Data struct {
S3Status bool `json:"s3-status"`
S3Full bool `json:"s3-full"`
}
var data Data

code := 200

if h.s3.Status() {
data.S3Status = true
} else {
data.S3Status = false
fmt.Printf("S3 unavailable during status check\n")
code = 503
}

if h.config.LimitStorageBytes > 0 {
totalBytesConsumed := h.dao.Info().StorageBytesAllocated()
if totalBytesConsumed >= h.config.LimitStorageBytes {
data.S3Full = true
code = 507
}
}

w.Header().Set("Content-Type", "application/json")
out, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Printf("Failed to parse json: %s\n", err.Error())
http.Error(w, "Errno 201", http.StatusInternalServerError)
return
}
w.WriteHeader(code)
io.WriteString(w, string(out))
}

func (h *HTTP) Robots(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=3600")

Expand Down

0 comments on commit 61861bc

Please sign in to comment.