fix: Fixes API routes.

This commit is contained in:
Tera << 8 2024-12-23 20:47:01 -05:00
parent fe8980b265
commit bcf97fde6d
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37
15 changed files with 284 additions and 132 deletions

View file

@ -4,9 +4,11 @@ import (
"fmt"
"net/http"
"git.terah.dev/imterah/hermes/api/backendruntime"
"git.terah.dev/imterah/hermes/api/dbcore"
"git.terah.dev/imterah/hermes/api/jwtcore"
"git.terah.dev/imterah/hermes/api/permissions"
"git.terah.dev/imterah/hermes/commonbackend"
"github.com/charmbracelet/log"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
@ -17,10 +19,6 @@ type ProxyRemovalRequest struct {
ID uint `validate:"required" json:"id"`
}
type ProxyRemovalResponse struct {
Success bool `json:"success"`
}
func RemoveProxy(c *gin.Context) {
var req ProxyRemovalRequest
@ -100,7 +98,56 @@ func RemoveProxy(c *gin.Context) {
return
}
c.JSON(http.StatusOK, &ProxyRemovalResponse{
Success: true,
backend, ok := backendruntime.RunningBackends[proxy.BackendID]
if !ok {
log.Warnf("Couldn't fetch backend runtime from backend ID #%d", proxy.BackendID)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Couldn't fetch backend runtime",
})
return
}
backend.RuntimeCommands <- &commonbackend.RemoveProxy{
Type: "removeProxy",
SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort,
Protocol: proxy.Protocol,
}
backendResponse := <-backend.RuntimeCommands
switch responseMessage := backendResponse.(type) {
case error:
log.Warnf("Failed to get response for backend #%d: %s", proxy.BackendID, responseMessage.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to get response from backend. Proxy was still successfully deleted",
})
return
case *commonbackend.ProxyStatusResponse:
if responseMessage.IsActive {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to stop proxy. Proxy was still successfully deleted",
})
return
}
default:
log.Errorf("Got illegal response type for backend #%d: %T", proxy.BackendID, responseMessage)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Got invalid response from backend. Proxy was still successfully deleted",
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}