regroup backend and frontend in app folder

This commit is contained in:
Blomios
2026-03-07 19:16:14 +01:00
parent 66c72f46a2
commit b5bc405771
28315 changed files with 206 additions and 3227368 deletions

View File

@ -0,0 +1,67 @@
package handlers
import (
"backend/models"
"crypto/rand"
"encoding/hex"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
func (r *NodeHandler) LoginHandler(c *gin.Context) {
log.Println("trying to login")
r.Repo.LoginHandler(c)
}
func GenerateSecureKey() (string, error) {
bytes := make([]byte, 32) // 256 bits
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func (r *NodeHandler) CreateApiKeyHandler(c *gin.Context) {
log.Println("trying to create key")
newApiKey, err := GenerateSecureKey()
if err != nil {
log.Printf("Generate API key error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "key creation error"})
return
}
var key models.CreateApiKeyResponse
key, err = r.Repo.CreateApiKeyHandler(c, newApiKey)
if err != nil {
log.Printf("CreateApiKeyHandler error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "insert new api key error"})
return
}
c.JSON(http.StatusOK, key)
}
func (h *NodeHandler) HandleRetrieveApiKeys(c *gin.Context) {
log.Println("All api keys retrieve request")
apiKeys := make([]models.ApiKey, 0)
apiKeys, err := h.Repo.RetriveApiKeyList()
if err != nil {
log.Printf("Request error on retrieving api keys: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "error retrieving api keys"})
return
}
log.Printf("nb api keys: %v", len(apiKeys))
c.JSON(http.StatusOK, apiKeys)
}

View File

@ -0,0 +1,78 @@
package handlers
import (
"backend/models"
"backend/repositories"
"encoding/json"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gorilla/mux"
)
type NodeHandler struct {
Repo *repositories.NodeRepository
}
var registeredNodes = make(map[string]models.NodeInfo)
func (h *NodeHandler) HandleRegisterNode(c *gin.Context) {
var node models.NodeInfo
if err := c.ShouldBindJSON(&node); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
nodeID, err := h.Repo.RegisterNode(node)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"id": nodeID})
}
func (h *NodeHandler) HandleRetrieveNodeList(c *gin.Context) {
log.Println("All nodes retrieved request")
registeredNodes, err := h.Repo.RetriveNodeList()
if err != nil {
log.Printf("Request error on retrieving all nodes: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Impossible de récupérer la liste des nœuds"})
return
}
log.Printf("nb nodes: %v", len(registeredNodes))
c.JSON(http.StatusOK, registeredNodes)
}
func HandleRetrieveNode(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "GET requested", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
nodeID := params["id"]
node, found := registeredNodes[nodeID]
if found {
json.NewEncoder(w).Encode(node)
} else {
w.WriteHeader(http.StatusNotFound)
errorResponse := map[string]string{"error": "Node non trouvé", "id": nodeID}
json.NewEncoder(w).Encode(errorResponse)
}
}
func UpdateNodes() {
}

View File

@ -0,0 +1,100 @@
package handlers
import (
"backend/models"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
func (h *NodeHandler) HandleRegisterService(c *gin.Context) {
var serviceRegister models.ServiceRegister
if err := c.ShouldBindJSON(&serviceRegister); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
serviceID, err := h.Repo.RegisterService(serviceRegister.Service, serviceRegister.NodeId)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"id": serviceID})
}
func (h *NodeHandler) HandleUpdateServiceStatus(c *gin.Context) {
var serviceUpdate models.ServiceUpdateRequest
if err := c.ShouldBindJSON(&serviceUpdate); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err := h.Repo.UpdateServiceStatus(serviceUpdate)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "updated"})
}
func (h *NodeHandler) HandleDeleteService(c *gin.Context) {
var serviceDelete models.ServiceMinimal
if err := c.ShouldBindJSON(&serviceDelete); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
node, err := h.Repo.RetriveNode(serviceDelete.NodeId)
if err != nil {
log.Printf("Node not found %v error : %v", serviceDelete.NodeId, err)
c.JSON(http.StatusNotFound, gin.H{"error": "Node not found"})
return
}
err = h.Repo.DeleteService(node, serviceDelete.ServiceId)
if err != nil {
log.Printf("Error deleting service: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete service"})
return
}
c.Status(http.StatusNoContent)
}
func (h *NodeHandler) HandleAddService(c *gin.Context) {
var addService models.AddServiceRequest
if err := c.ShouldBindJSON(&addService); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
node, err := h.Repo.RetriveNode(addService.NodeId)
if err != nil {
log.Printf("Node not found %v error : %v", addService.NodeId, err)
c.JSON(http.StatusNotFound, gin.H{"error": "Node not found"})
return
}
var service models.Service
service.Command = addService.Command
service.Name = addService.Name
service.Id = 0
log.Printf("Node api key: %v", addService.NodeApiKey)
err = h.Repo.AddServiceToNode(&service, &node, addService.NodeApiKey)
if err != nil {
log.Printf("Error add service: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to add service"})
return
}
c.Status(http.StatusNoContent)
}