mirror of
https://github.com/ddworken/hishtory.git
synced 2025-04-13 12:08:28 +02:00
Move all handlers into either api_handlers.go for client-exposed handlers, or debug_handlers.go for debug/admin functionality
This commit is contained in:
parent
9fc1140ab9
commit
a3b865fa6b
@ -7,6 +7,8 @@ import (
|
|||||||
"html"
|
"html"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
@ -242,3 +244,71 @@ func (s *Server) apiRegisterHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Content-Length", "0")
|
w.Header().Set("Content-Length", "0")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) getDeletionRequestsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userId := getRequiredQueryParam(r, "user_id")
|
||||||
|
deviceId := getRequiredQueryParam(r, "device_id")
|
||||||
|
|
||||||
|
// Increment the ReadCount
|
||||||
|
err := s.db.DeletionRequestInc(r.Context(), userId, deviceId)
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
// Return all the deletion requests
|
||||||
|
deletionRequests, err := s.db.DeletionRequestsForUserAndDevice(r.Context(), userId, deviceId)
|
||||||
|
checkGormError(err)
|
||||||
|
if err := json.NewEncoder(w).Encode(deletionRequests); err != nil {
|
||||||
|
panic(fmt.Errorf("failed to JSON marshall the dump requests: %w", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) addDeletionRequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var request shared.DeletionRequest
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||||
|
panic(fmt.Errorf("failed to decode: %w", err))
|
||||||
|
}
|
||||||
|
request.ReadCount = 0
|
||||||
|
fmt.Printf("addDeletionRequestHandler: received request containg %d messages to be deleted\n", len(request.Messages.Ids))
|
||||||
|
|
||||||
|
err := s.db.DeletionRequestCreate(r.Context(), &request)
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Length", "0")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) slsaStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// returns "OK" unless there is a current SLSA bug
|
||||||
|
v := getHishtoryVersion(r)
|
||||||
|
if !strings.Contains(v, "v0.") {
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vNum, err := strconv.Atoi(strings.Split(v, ".")[1])
|
||||||
|
if err != nil {
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if vNum < 159 {
|
||||||
|
w.Write([]byte("Sigstore deployed a broken change. See https://github.com/slsa-framework/slsa-github-generator/issues/1163"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) feedbackHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var feedback shared.Feedback
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&feedback)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to decode: %w", err))
|
||||||
|
}
|
||||||
|
fmt.Printf("feedbackHandler: received request containg feedback %#v\n", feedback)
|
||||||
|
err = s.db.FeedbackCreate(r.Context(), &feedback)
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
if s.statsd != nil {
|
||||||
|
s.statsd.Incr("hishtory.uninstall", []string{}, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Length", "0")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
132
backend/server/internal/server/debug_handlers.go
Normal file
132
backend/server/internal/server/debug_handlers.go
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ddworken/hishtory/shared"
|
||||||
|
|
||||||
|
"github.com/rodaine/table"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if s.isProductionEnvironment {
|
||||||
|
encHistoryEntryCount, err := s.db.CountApproximateHistoryEntries(r.Context())
|
||||||
|
checkGormError(err)
|
||||||
|
if encHistoryEntryCount < 1000 {
|
||||||
|
panic("Suspiciously few enc history entries!")
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceCount, err := s.db.CountAllDevices(r.Context())
|
||||||
|
checkGormError(err)
|
||||||
|
if deviceCount < 100 {
|
||||||
|
panic("Suspiciously few devices!")
|
||||||
|
}
|
||||||
|
// Check that we can write to the DB. This entry will get written and then eventually cleaned by the cron.
|
||||||
|
err = s.db.AddHistoryEntries(r.Context(), &shared.EncHistoryEntry{
|
||||||
|
EncryptedData: []byte("data"),
|
||||||
|
Nonce: []byte("nonce"),
|
||||||
|
DeviceId: "healthcheck_device_id",
|
||||||
|
UserId: "healthcheck_user_id",
|
||||||
|
Date: time.Now(),
|
||||||
|
EncryptedId: "healthcheck_enc_id",
|
||||||
|
ReadCount: 10000,
|
||||||
|
})
|
||||||
|
checkGormError(err)
|
||||||
|
} else {
|
||||||
|
err := s.db.Ping()
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to ping DB: %w", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) triggerCronHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
err := s.cronFn(r.Context(), s.db, s.statsd)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Length", "0")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) usageStatsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
usageData, err := s.db.UsageDataStats(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("db.UsageDataStats: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
tbl := table.New("Registration Date", "Num Devices", "Num Entries", "Num Queries", "Last Active", "Last Query", "Versions", "IPs")
|
||||||
|
tbl.WithWriter(w)
|
||||||
|
for _, data := range usageData {
|
||||||
|
versions := strings.ReplaceAll(strings.ReplaceAll(data.Versions, "Unknown", ""), ", ", "")
|
||||||
|
lastQueryStr := strings.ReplaceAll(data.LastQueried.Format(shared.DateOnly), "1970-01-01", "")
|
||||||
|
tbl.AddRow(
|
||||||
|
data.RegistrationDate.Format(shared.DateOnly),
|
||||||
|
data.NumDevices,
|
||||||
|
data.NumEntries,
|
||||||
|
data.NumQueries,
|
||||||
|
data.LastUsedDate.Format(shared.DateOnly),
|
||||||
|
lastQueryStr,
|
||||||
|
versions,
|
||||||
|
data.IpAddresses,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
tbl.Print()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) statsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
numDevices, err := s.db.CountAllDevices(r.Context())
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
numEntriesProcessed, err := s.db.UsageDataTotal(r.Context())
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
numDbEntries, err := s.db.CountApproximateHistoryEntries(r.Context())
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
oneWeek := time.Hour * 24 * 7
|
||||||
|
weeklyActiveInstalls, err := s.db.CountActiveInstalls(r.Context(), oneWeek)
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
weeklyQueryUsers, err := s.db.CountQueryUsers(r.Context(), oneWeek)
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
lastRegistration, err := s.db.DateOfLastRegistration(r.Context())
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
_, _ = fmt.Fprintf(w, "Num devices: %d\n", numDevices)
|
||||||
|
_, _ = fmt.Fprintf(w, "Num history entries processed: %d\n", numEntriesProcessed)
|
||||||
|
_, _ = fmt.Fprintf(w, "Num DB entries: %d\n", numDbEntries)
|
||||||
|
_, _ = fmt.Fprintf(w, "Weekly active installs: %d\n", weeklyActiveInstalls)
|
||||||
|
_, _ = fmt.Fprintf(w, "Weekly active queries: %d\n", weeklyQueryUsers)
|
||||||
|
_, _ = fmt.Fprintf(w, "Last registration: %s\n", lastRegistration)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) wipeDbEntriesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Host == "api.hishtory.dev" || s.isProductionEnvironment {
|
||||||
|
panic("refusing to wipe the DB for prod")
|
||||||
|
}
|
||||||
|
if !s.isTestEnvironment {
|
||||||
|
panic("refusing to wipe the DB non-test environment")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := s.db.Unsafe_DeleteAllHistoryEntries(r.Context())
|
||||||
|
checkGormError(err)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Length", "0")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) getNumConnectionsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
stats, err := s.db.Stats()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _ = fmt.Fprintf(w, "%#v", stats.OpenConnections)
|
||||||
|
}
|
@ -2,18 +2,14 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/DataDog/datadog-go/statsd"
|
"github.com/DataDog/datadog-go/statsd"
|
||||||
"github.com/ddworken/hishtory/backend/server/internal/database"
|
"github.com/ddworken/hishtory/backend/server/internal/database"
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
"github.com/rodaine/table"
|
|
||||||
httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
|
httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -139,194 +135,6 @@ func (s *Server) UpdateReleaseVersion(v string, updateInfo shared.UpdateInfo) {
|
|||||||
s.updateInfo = updateInfo
|
s.updateInfo = updateInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getDeletionRequestsHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
userId := getRequiredQueryParam(r, "user_id")
|
|
||||||
deviceId := getRequiredQueryParam(r, "device_id")
|
|
||||||
|
|
||||||
// Increment the ReadCount
|
|
||||||
err := s.db.DeletionRequestInc(r.Context(), userId, deviceId)
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
// Return all the deletion requests
|
|
||||||
deletionRequests, err := s.db.DeletionRequestsForUserAndDevice(r.Context(), userId, deviceId)
|
|
||||||
checkGormError(err)
|
|
||||||
if err := json.NewEncoder(w).Encode(deletionRequests); err != nil {
|
|
||||||
panic(fmt.Errorf("failed to JSON marshall the dump requests: %w", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) addDeletionRequestHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var request shared.DeletionRequest
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
||||||
panic(fmt.Errorf("failed to decode: %w", err))
|
|
||||||
}
|
|
||||||
request.ReadCount = 0
|
|
||||||
fmt.Printf("addDeletionRequestHandler: received request containg %d messages to be deleted\n", len(request.Messages.Ids))
|
|
||||||
|
|
||||||
err := s.db.DeletionRequestCreate(r.Context(), &request)
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
w.Header().Set("Content-Length", "0")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) triggerCronHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
err := s.cronFn(r.Context(), s.db, s.statsd)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Length", "0")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) slsaStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// returns "OK" unless there is a current SLSA bug
|
|
||||||
v := getHishtoryVersion(r)
|
|
||||||
if !strings.Contains(v, "v0.") {
|
|
||||||
w.Write([]byte("OK"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vNum, err := strconv.Atoi(strings.Split(v, ".")[1])
|
|
||||||
if err != nil {
|
|
||||||
w.Write([]byte("OK"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if vNum < 159 {
|
|
||||||
w.Write([]byte("Sigstore deployed a broken change. See https://github.com/slsa-framework/slsa-github-generator/issues/1163"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Write([]byte("OK"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) feedbackHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var feedback shared.Feedback
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&feedback)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("failed to decode: %w", err))
|
|
||||||
}
|
|
||||||
fmt.Printf("feedbackHandler: received request containg feedback %#v\n", feedback)
|
|
||||||
err = s.db.FeedbackCreate(r.Context(), &feedback)
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
if s.statsd != nil {
|
|
||||||
s.statsd.Incr("hishtory.uninstall", []string{}, 1.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Length", "0")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if s.isProductionEnvironment {
|
|
||||||
encHistoryEntryCount, err := s.db.CountApproximateHistoryEntries(r.Context())
|
|
||||||
checkGormError(err)
|
|
||||||
if encHistoryEntryCount < 1000 {
|
|
||||||
panic("Suspiciously few enc history entries!")
|
|
||||||
}
|
|
||||||
|
|
||||||
deviceCount, err := s.db.CountAllDevices(r.Context())
|
|
||||||
checkGormError(err)
|
|
||||||
if deviceCount < 100 {
|
|
||||||
panic("Suspiciously few devices!")
|
|
||||||
}
|
|
||||||
// Check that we can write to the DB. This entry will get written and then eventually cleaned by the cron.
|
|
||||||
err = s.db.AddHistoryEntries(r.Context(), &shared.EncHistoryEntry{
|
|
||||||
EncryptedData: []byte("data"),
|
|
||||||
Nonce: []byte("nonce"),
|
|
||||||
DeviceId: "healthcheck_device_id",
|
|
||||||
UserId: "healthcheck_user_id",
|
|
||||||
Date: time.Now(),
|
|
||||||
EncryptedId: "healthcheck_enc_id",
|
|
||||||
ReadCount: 10000,
|
|
||||||
})
|
|
||||||
checkGormError(err)
|
|
||||||
} else {
|
|
||||||
err := s.db.Ping()
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("failed to ping DB: %w", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
w.Write([]byte("OK"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) usageStatsHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
usageData, err := s.db.UsageDataStats(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("db.UsageDataStats: %w", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
tbl := table.New("Registration Date", "Num Devices", "Num Entries", "Num Queries", "Last Active", "Last Query", "Versions", "IPs")
|
|
||||||
tbl.WithWriter(w)
|
|
||||||
for _, data := range usageData {
|
|
||||||
versions := strings.ReplaceAll(strings.ReplaceAll(data.Versions, "Unknown", ""), ", ", "")
|
|
||||||
lastQueryStr := strings.ReplaceAll(data.LastQueried.Format(shared.DateOnly), "1970-01-01", "")
|
|
||||||
tbl.AddRow(
|
|
||||||
data.RegistrationDate.Format(shared.DateOnly),
|
|
||||||
data.NumDevices,
|
|
||||||
data.NumEntries,
|
|
||||||
data.NumQueries,
|
|
||||||
data.LastUsedDate.Format(shared.DateOnly),
|
|
||||||
lastQueryStr,
|
|
||||||
versions,
|
|
||||||
data.IpAddresses,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
tbl.Print()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) statsHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
numDevices, err := s.db.CountAllDevices(r.Context())
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
numEntriesProcessed, err := s.db.UsageDataTotal(r.Context())
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
numDbEntries, err := s.db.CountApproximateHistoryEntries(r.Context())
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
oneWeek := time.Hour * 24 * 7
|
|
||||||
weeklyActiveInstalls, err := s.db.CountActiveInstalls(r.Context(), oneWeek)
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
weeklyQueryUsers, err := s.db.CountQueryUsers(r.Context(), oneWeek)
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
lastRegistration, err := s.db.DateOfLastRegistration(r.Context())
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
_, _ = fmt.Fprintf(w, "Num devices: %d\n", numDevices)
|
|
||||||
_, _ = fmt.Fprintf(w, "Num history entries processed: %d\n", numEntriesProcessed)
|
|
||||||
_, _ = fmt.Fprintf(w, "Num DB entries: %d\n", numDbEntries)
|
|
||||||
_, _ = fmt.Fprintf(w, "Weekly active installs: %d\n", weeklyActiveInstalls)
|
|
||||||
_, _ = fmt.Fprintf(w, "Weekly active queries: %d\n", weeklyQueryUsers)
|
|
||||||
_, _ = fmt.Fprintf(w, "Last registration: %s\n", lastRegistration)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) wipeDbEntriesHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Host == "api.hishtory.dev" || s.isProductionEnvironment {
|
|
||||||
panic("refusing to wipe the DB for prod")
|
|
||||||
}
|
|
||||||
if !s.isTestEnvironment {
|
|
||||||
panic("refusing to wipe the DB non-test environment")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := s.db.Unsafe_DeleteAllHistoryEntries(r.Context())
|
|
||||||
checkGormError(err)
|
|
||||||
|
|
||||||
w.Header().Set("Content-Length", "0")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) getNumConnectionsHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
stats, err := s.db.Stats()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _ = fmt.Fprintf(w, "%#v", stats.OpenConnections)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) handleNonCriticalError(err error) {
|
func (s *Server) handleNonCriticalError(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if s.isProductionEnvironment {
|
if s.isProductionEnvironment {
|
||||||
|
Loading…
Reference in New Issue
Block a user