ai slop ah
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -13,6 +12,7 @@ import (
|
||||
|
||||
"github.com/example/sndit/backend/internal/auth"
|
||||
"github.com/example/sndit/backend/internal/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -35,28 +35,36 @@ func NewHandler(db *sql.DB, objectStorage storage.Storage, authService *auth.Ser
|
||||
return &Handler{db: db, storage: objectStorage, auth: authService, config: config}
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterRoutes(mux *http.ServeMux, require func(http.Handler) http.Handler) {
|
||||
mux.Handle("GET /api/dev/diagnostics", require(http.HandlerFunc(h.Diagnostics)))
|
||||
mux.Handle("POST /api/dev/storage-check", require(http.HandlerFunc(h.StorageCheck)))
|
||||
func (h *Handler) RegisterRoutes(router gin.IRouter, require gin.HandlerFunc) {
|
||||
router.GET("/api/dev/diagnostics", require, h.Diagnostics)
|
||||
router.POST("/api/dev/storage-check", require, h.StorageCheck)
|
||||
}
|
||||
|
||||
func (h *Handler) Diagnostics(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := auth.UserFromContext(r.Context())
|
||||
// Diagnostics godoc
|
||||
// @Summary Inspect local development dependencies
|
||||
// @Tags development
|
||||
// @Produce json
|
||||
// @Security studioSession
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Router /api/dev/diagnostics [get]
|
||||
func (h *Handler) Diagnostics(c *gin.Context) {
|
||||
user, _ := auth.UserFromContext(c)
|
||||
databaseError := ""
|
||||
databaseContext, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
||||
databaseContext, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||||
if err := h.db.PingContext(databaseContext); err != nil {
|
||||
databaseError = err.Error()
|
||||
}
|
||||
cancel()
|
||||
|
||||
storageError := ""
|
||||
storageContext, storageCancel := context.WithTimeout(r.Context(), 3*time.Second)
|
||||
storageContext, storageCancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
|
||||
if err := h.storage.EnsureBucket(storageContext); err != nil {
|
||||
storageError = err.Error()
|
||||
}
|
||||
storageCancel()
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
writeJSON(c, http.StatusOK, map[string]any{
|
||||
"environment": "development",
|
||||
"now": time.Now().UTC().Format(time.RFC3339),
|
||||
"user": map[string]string{
|
||||
@@ -84,33 +92,43 @@ func (h *Handler) Diagnostics(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) StorageCheck(w http.ResponseWriter, r *http.Request) {
|
||||
if err := h.storage.EnsureBucket(r.Context()); err != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"ok": false, "step": "bucket", "error": err.Error()})
|
||||
// StorageCheck godoc
|
||||
// @Summary Check MinIO storage read/write access
|
||||
// @Tags development
|
||||
// @Produce json
|
||||
// @Security studioSession
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Failure 401 {object} map[string]string
|
||||
// @Failure 500 {object} map[string]interface{}
|
||||
// @Failure 503 {object} map[string]interface{}
|
||||
// @Router /api/dev/storage-check [post]
|
||||
func (h *Handler) StorageCheck(c *gin.Context) {
|
||||
if err := h.storage.EnsureBucket(c.Request.Context()); err != nil {
|
||||
writeJSON(c, http.StatusServiceUnavailable, map[string]any{"ok": false, "step": "bucket", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
var randomBytes [12]byte
|
||||
if _, err := rand.Read(randomBytes[:]); err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"ok": false, "step": "random", "error": err.Error()})
|
||||
writeJSON(c, http.StatusInternalServerError, map[string]any{"ok": false, "step": "random", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
key := fmt.Sprintf("dev/diagnostics/%s.txt", hex.EncodeToString(randomBytes[:]))
|
||||
contents := "northline storage check " + time.Now().UTC().Format(time.RFC3339Nano)
|
||||
if err := h.storage.Put(r.Context(), key, strings.NewReader(contents), int64(len(contents)), "text/plain"); err != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"ok": false, "step": "put", "error": err.Error()})
|
||||
contents := "studio storage check " + time.Now().UTC().Format(time.RFC3339Nano)
|
||||
if err := h.storage.Put(c.Request.Context(), key, strings.NewReader(contents), int64(len(contents)), "text/plain"); err != nil {
|
||||
writeJSON(c, http.StatusServiceUnavailable, map[string]any{"ok": false, "step": "put", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
info, statErr := h.storage.Stat(r.Context(), key)
|
||||
deleteErr := h.storage.Delete(r.Context(), key)
|
||||
info, statErr := h.storage.Stat(c.Request.Context(), key)
|
||||
deleteErr := h.storage.Delete(c.Request.Context(), key)
|
||||
if statErr != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"ok": false, "step": "stat", "error": statErr.Error()})
|
||||
writeJSON(c, http.StatusServiceUnavailable, map[string]any{"ok": false, "step": "stat", "error": statErr.Error()})
|
||||
return
|
||||
}
|
||||
if deleteErr != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"ok": false, "step": "delete", "error": deleteErr.Error()})
|
||||
writeJSON(c, http.StatusServiceUnavailable, map[string]any{"ok": false, "step": "delete", "error": deleteErr.Error()})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "bytes": info.Size, "contentType": info.ContentType})
|
||||
writeJSON(c, http.StatusOK, map[string]any{"ok": true, "bytes": info.Size, "contentType": info.ContentType})
|
||||
}
|
||||
|
||||
func splitOrigins(value string) []string {
|
||||
@@ -123,8 +141,6 @@ func splitOrigins(value string) []string {
|
||||
return result
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
func writeJSON(c *gin.Context, status int, value any) {
|
||||
c.JSON(status, value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user