- Kompletna Go implementacija licencnog servera (19 Go fajlova) - Klijentski API: activate, deactivate, validate - Admin API: CRUD licence, stats, audit log - Admin dashboard: htmx + Go templates - RSA-2048 potpisivanje licencnih podataka - Rate limiting i API key autentifikacija - MySQL migracije i seed podaci (ESIR, ARV, LIGHT_TICKET) - Unit testovi: keygen, crypto, model, middleware (24 testa) - Dokumentacija: SPEC.md, ARCHITECTURE.md, SETUP.md, API.md, TESTING.md, README.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
163 lines
4.8 KiB
Go
163 lines
4.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"dal-license-server/internal/model"
|
|
"dal-license-server/internal/repository"
|
|
"dal-license-server/internal/service"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type AdminHandler struct {
|
|
licenses *service.LicenseService
|
|
activation *service.ActivationService
|
|
audit *repository.AuditRepo
|
|
}
|
|
|
|
func NewAdminHandler(licenses *service.LicenseService, activation *service.ActivationService, audit *repository.AuditRepo) *AdminHandler {
|
|
return &AdminHandler{licenses: licenses, activation: activation, audit: audit}
|
|
}
|
|
|
|
func (h *AdminHandler) ListProducts(w http.ResponseWriter, r *http.Request) {
|
|
products, err := h.licenses.GetProducts()
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Greska pri ucitavanju proizvoda")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, products)
|
|
}
|
|
|
|
func (h *AdminHandler) ListLicenses(w http.ResponseWriter, r *http.Request) {
|
|
product := r.URL.Query().Get("product")
|
|
status := r.URL.Query().Get("status")
|
|
search := r.URL.Query().Get("search")
|
|
|
|
licenses, err := h.licenses.List(product, status, search)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Greska pri ucitavanju licenci")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, licenses)
|
|
}
|
|
|
|
func (h *AdminHandler) CreateLicense(w http.ResponseWriter, r *http.Request) {
|
|
var req model.CreateLicenseRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "INVALID_REQUEST", "Neispravan zahtev")
|
|
return
|
|
}
|
|
|
|
license, err := h.licenses.Create(&req, clientIP(r))
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "CREATE_FAILED", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, license)
|
|
}
|
|
|
|
func (h *AdminHandler) GetLicense(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "INVALID_ID", "Neispravan ID")
|
|
return
|
|
}
|
|
|
|
license, err := h.licenses.GetByID(id)
|
|
if err != nil {
|
|
writeError(w, http.StatusNotFound, "NOT_FOUND", "Licenca nije pronadjena")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, license)
|
|
}
|
|
|
|
func (h *AdminHandler) UpdateLicense(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "INVALID_ID", "Neispravan ID")
|
|
return
|
|
}
|
|
|
|
var req model.UpdateLicenseRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "INVALID_REQUEST", "Neispravan zahtev")
|
|
return
|
|
}
|
|
|
|
if err := h.licenses.Update(id, &req, clientIP(r)); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "UPDATE_FAILED", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]string{"message": "Licenca azurirana"})
|
|
}
|
|
|
|
func (h *AdminHandler) RevokeLicense(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "INVALID_ID", "Neispravan ID")
|
|
return
|
|
}
|
|
|
|
var req model.RevokeRequest
|
|
json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err := h.licenses.Revoke(id, req.Reason, clientIP(r)); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "REVOKE_FAILED", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]string{"message": "Licenca opozvana"})
|
|
}
|
|
|
|
func (h *AdminHandler) ReleaseLicense(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "INVALID_ID", "Neispravan ID")
|
|
return
|
|
}
|
|
|
|
if err := h.activation.ForceRelease(id, clientIP(r)); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "RELEASE_FAILED", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]string{"message": "Aktivacija oslobodjena"})
|
|
}
|
|
|
|
func (h *AdminHandler) ListActivations(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "INVALID_ID", "Neispravan ID")
|
|
return
|
|
}
|
|
|
|
acts, err := h.activation.ListByLicense(id)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Greska")
|
|
return
|
|
}
|
|
if acts == nil {
|
|
acts = []model.Activation{}
|
|
}
|
|
writeJSON(w, http.StatusOK, acts)
|
|
}
|
|
|
|
func (h *AdminHandler) AuditLog(w http.ResponseWriter, r *http.Request) {
|
|
entries, err := h.audit.Recent(100)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Greska")
|
|
return
|
|
}
|
|
if entries == nil {
|
|
entries = []model.AuditEntry{}
|
|
}
|
|
writeJSON(w, http.StatusOK, entries)
|
|
}
|
|
|
|
func (h *AdminHandler) Stats(w http.ResponseWriter, r *http.Request) {
|
|
stats, err := h.licenses.GetStats()
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Greska")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, stats)
|
|
}
|