Skip to content

Commit

Permalink
Add an endpoint to view raw crash reports (#24)
Browse files Browse the repository at this point in the history
closes #23
  • Loading branch information
TwistedAsylumMC committed Mar 7, 2021
1 parent f46db24 commit d0087cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/handler/view.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handler

import (
"bytes"
"encoding/json"
"log"
"net/http"
"regexp"
Expand Down Expand Up @@ -47,6 +49,28 @@ func ViewIDGet(db *database.DB) http.HandlerFunc {
}
}

func ViewIDRawGet(db *database.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
reportID, err := strconv.Atoi(chi.URLParam(r, "reportID"))
if err != nil {
template.ErrorTemplate(w, r, "Please specify a report", http.StatusNotFound)
return
}

report, err := db.FetchRawReport(int64(reportID))
if err != nil {
log.Printf("error fetching report: %v", err)
template.ErrorTemplate(w, r, "Report not found", http.StatusNotFound)
return
}

var buffer bytes.Buffer
json.Indent(&buffer, report, "", " ")
w.Header().Set("content-type", "application/json")
_, _ = w.Write(buffer.Bytes())
}
}

var cleanRE = regexp.MustCompile(`[^A-Za-z0-9_\-\.\,\;\:/\#\(\)\\ +]`)

func clean(v string) string {
Expand Down
3 changes: 2 additions & 1 deletion app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/go-chi/chi/middleware"

"github.com/pmmp/CrashArchive/app"
"github.com/pmmp/CrashArchive/app/database"
"github.com/pmmp/CrashArchive/app/handler"
"github.com/pmmp/CrashArchive/app/template"
"github.com/pmmp/CrashArchive/app/database"
"github.com/pmmp/CrashArchive/app/user"
"github.com/pmmp/CrashArchive/app/webhook"
)
Expand Down Expand Up @@ -42,6 +42,7 @@ func New(db *database.DB, wh *webhook.Webhook, config *app.Config) *chi.Mux {
r.Get("/logout", handler.LogoutGet)
r.Get("/list", handler.ListGet(db))
r.Get("/view/{reportID}", handler.ViewIDGet(db))
r.Get("/view/{reportID}/raw", handler.ViewIDRawGet(db))
r.Get("/download/{reportID}", handler.DownloadGet(db))
r.Get("/delete/{reportID}", handler.DeleteGet(db))

Expand Down
1 change: 1 addition & 0 deletions templates/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ul>
<li><a href="/search/report?id={{.ReportID}}" class="btn-floating fab-text-option">Find duplicates</a></li>
<li><a href="/download/{{.ReportID}}" class="btn-floating fab-text-option">Download</a></li>
<li><a href="/view/{{.ReportID}}/raw" class="btn-floating fab-text-option">View raw</a></li>
{{if .HasDeletePerm}}
<li><a onclick="deleteHandler()" class="btn-floating fab-text-option">Delete</a></li>
{{end}}
Expand Down

0 comments on commit d0087cc

Please sign in to comment.