refactor: switch router to gin

This commit is contained in:
Nicolas Meienberger
2025-08-10 13:07:05 +02:00
parent e601d91955
commit 2c38a551cc
15 changed files with 277 additions and 131 deletions

View File

@@ -1,37 +1,35 @@
package driver
import (
"encoding/json"
"ironmount/internal/db"
"net/http"
"github.com/rs/zerolog/hlog"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func Mount(w http.ResponseWriter, r *http.Request) {
func Mount(c *gin.Context) {
var req MountRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err := c.BindJSON(&req); err != nil {
log.Error().Err(err).Msg("Invalid request body")
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("Invalid request body")
http.Error(w, "Invalid request body", http.StatusBadRequest)
c.JSON(http.StatusBadRequest, gin.H{"Err": "Invalid request body"})
return
}
vol, err := db.GetVolumeByName(req.Name)
if err != nil {
log.Error().Err(err).Str("volume", req.Name).Msg("Failed to get volume")
_ = json.NewEncoder(w).Encode(map[string]string{
"Err": err.Error(),
})
c.JSON(http.StatusNotFound, gin.H{"Err": err.Error()})
return
}
log.Info().Str("volume", vol.Name).Str("path", vol.Path).Msg("Mounting volume")
_ = json.NewEncoder(w).Encode(map[string]string{
c.JSON(http.StatusOK, gin.H{
"Name": vol.Name,
"Mountpoint": vol.Path,
"Err": "",
})