ci: add Gitea Actions workflows and placeholder tests
Some checks failed
CI / ci (push) Failing after 47s
Some checks failed
CI / ci (push) Failing after 47s
This commit is contained in:
58
internal/handler/interest.go
Normal file
58
internal/handler/interest.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/daniil/homelab-api/internal/service"
|
||||
)
|
||||
|
||||
type InterestHandler struct {
|
||||
service *service.InterestService
|
||||
secretKey string
|
||||
}
|
||||
|
||||
func NewInterestHandler(db *sqlx.DB) *InterestHandler {
|
||||
secretKey := os.Getenv("INTERNAL_API_KEY")
|
||||
if secretKey == "" {
|
||||
secretKey = "pulse-internal-2026"
|
||||
}
|
||||
return &InterestHandler{
|
||||
service: service.NewInterestService(db),
|
||||
secretKey: secretKey,
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRoutes регистрирует internal routes
|
||||
func (h *InterestHandler) RegisterRoutes(r chi.Router) {
|
||||
r.Post("/internal/calculate-interest", h.CalculateInterest)
|
||||
}
|
||||
|
||||
// CalculateInterest запускает расчёт процентов для всех вкладов
|
||||
// POST /internal/calculate-interest
|
||||
// Header: X-Internal-Key: <secret>
|
||||
func (h *InterestHandler) CalculateInterest(w http.ResponseWriter, r *http.Request) {
|
||||
// Verify internal API key
|
||||
key := r.Header.Get("X-Internal-Key")
|
||||
if key != h.secretKey {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
results, err := h.service.CalculateAllDepositsInterest()
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"message": "Interest calculation completed",
|
||||
"results": results,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user