added a bit of zap logging
Some checks failed
Build and Push Docker Image / Build image (push) Failing after 58s

This commit is contained in:
2025-04-09 20:03:34 +03:00
parent 29d13371a8
commit 54174ef418
8 changed files with 58 additions and 14 deletions

View File

@@ -2,9 +2,7 @@ package servers
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"
@@ -14,9 +12,11 @@ import (
"git.acooldomain.co/server-manager/backend/dbhandler"
"git.acooldomain.co/server-manager/backend/factories"
instancemanager "git.acooldomain.co/server-manager/backend/instancemanager"
"git.acooldomain.co/server-manager/backend/logger"
"git.acooldomain.co/server-manager/backend/models"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"go.uber.org/zap"
)
var upgrader = websocket.Upgrader{
@@ -193,16 +193,12 @@ func (con ServersApi) GetServers(ctx *gin.Context) {
server := serverConfigsMap[instanceServer.Id]
var image ImageInfo
log.Printf("server=%#v\t|\tinstanceServer=%#v", server, instanceServer)
if instanceServer.Running {
image = ImageInfo{
Name: instanceServer.RunningImage.Registry,
Version: instanceServer.RunningImage.Tag,
}
} else {
log.Printf("serverImage:%#v", server.Image)
image = ImageInfo{
Name: server.Image.Registry,
Version: server.Image.Tag,
@@ -246,6 +242,7 @@ func (con ServersApi) DeleteServer(ctx *gin.Context) {
err = con.ServersDbHandler.DeleteServer(ctx, serverId)
if err != nil {
logger.Logger.Error("Failed to delete server", zap.String("ServerId", serverId), zap.Error(err))
ctx.AbortWithError(501, err)
return
}
@@ -262,13 +259,15 @@ func (con ServersApi) RunCommand(ctx *gin.Context) {
err := json.NewDecoder(ctx.Request.Body).Decode(&request)
if err != nil {
ctx.AbortWithError(500, err)
return
}
serverId := ctx.Param("server_id")
log.Print("Writing command \"", request.Command, "\"")
logger.Logger.Info("Writing command", zap.String("ServerId", serverId), zap.String("Command", request.Command))
consolePointer, err := con.InstanceManager.InteractiveTerminal(ctx, serverId)
if err != nil {
logger.Logger.Error("Failed getting terminal", zap.String("ServerId", serverId), zap.String("Command", request.Command), zap.Error(err))
ctx.AbortWithError(500, err)
return
}
@@ -278,10 +277,12 @@ func (con ServersApi) RunCommand(ctx *gin.Context) {
_, err = console.Conn.Write([]byte(request.Command + "\n"))
if err != nil {
logger.Logger.Error("Failed writing command", zap.String("ServerId", serverId), zap.String("Command", request.Command), zap.Error(err))
ctx.AbortWithError(500, err)
return
}
logger.Logger.Info("Successfully wrote command", zap.String("ServerId", serverId), zap.String("Command", request.Command))
ctx.JSON(200, "OK")
}
@@ -301,8 +302,9 @@ func (con ServersApi) AttachServer(ctx *gin.Context) {
defer func() {
if err != nil {
log.Printf("The latest error is %s", err)
logger.Logger.Error("Interactive Terminal latest error", zap.Error(err))
}
close(websocketRead)
close(containerRead)
}()
@@ -375,7 +377,7 @@ func (con ServersApi) AttachServer(ctx *gin.Context) {
case "insert":
_, err = hijacked.Conn.Write([]byte(Command.Arguments))
if err != nil {
log.Printf("Write to docker failed %s", errors.Unwrap(err))
logger.Logger.Error("Write to docker failed", zap.Error(err))
stop = true
break
@@ -400,13 +402,13 @@ func (con ServersApi) AttachServer(ctx *gin.Context) {
err2 = hijacked.ResizerFunc(width, height)
if err2 != nil {
log.Printf("Failed to resize container to %dx%d: %s", width, height, err)
logger.Logger.Error("Failed to resize container", zap.Uint("width", width), zap.Uint("height", height), zap.Error(err))
}
}
case data := <-containerRead:
err := ws.WriteJSON(data)
if err != nil {
log.Printf("Write to socket failed %s", errors.Unwrap(err))
logger.Logger.Error("Write to socket failed", zap.Error(err))
stop = true
}
}
@@ -442,7 +444,7 @@ func (con ServersApi) UpdateServer(ctx *gin.Context) {
for user, permissions := range request.UserPermissions {
err = con.ServerAuthorization.SetPermissions(ctx, user, serverId, permissions)
if err != nil {
log.Printf("failed to change user %s permissions for server %s due to %e", user, serverId, err)
logger.Logger.Error("failed to change user's server permissions", zap.String("user", user), zap.String("ServerId", serverId), zap.Error(err))
continue
}
}