mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
refactor: switch router to gin
This commit is contained in:
@@ -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": "",
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user