mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
42 lines
1022 B
Go
42 lines
1022 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"novamd/internal/db"
|
|
"novamd/internal/storage"
|
|
)
|
|
|
|
// ErrorResponse is a generic error response
|
|
type ErrorResponse struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// Handler provides common functionality for all handlers
|
|
type Handler struct {
|
|
DB db.Database
|
|
Storage storage.Manager
|
|
}
|
|
|
|
// NewHandler creates a new handler with the given dependencies
|
|
func NewHandler(db db.Database, s storage.Manager) *Handler {
|
|
return &Handler{
|
|
DB: db,
|
|
Storage: s,
|
|
}
|
|
}
|
|
|
|
// respondJSON is a helper to send JSON responses
|
|
func respondJSON(w http.ResponseWriter, data interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(data); err != nil {
|
|
respondError(w, "Failed to encode response", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
// respondError is a helper to send error responses
|
|
func respondError(w http.ResponseWriter, message string, code int) {
|
|
w.WriteHeader(code)
|
|
respondJSON(w, ErrorResponse{Message: message})
|
|
}
|