mirror of
https://github.com/h44z/wg-portal.git
synced 2025-08-10 15:32:24 +00:00
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
model2 "github.com/h44z/wg-portal/internal/app/api/v0/model"
|
|
"net/http"
|
|
|
|
"github.com/h44z/wg-portal/internal/domain"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/h44z/wg-portal/internal/app"
|
|
)
|
|
|
|
type peerEndpoint struct {
|
|
app *app.App
|
|
authenticator *authenticationHandler
|
|
}
|
|
|
|
func (e peerEndpoint) GetName() string {
|
|
return "PeerEndpoint"
|
|
}
|
|
|
|
func (e peerEndpoint) RegisterRoutes(g *gin.RouterGroup, authenticator *authenticationHandler) {
|
|
apiGroup := g.Group("/peer", e.authenticator.LoggedIn())
|
|
|
|
apiGroup.GET("/all/:id", e.handlePeersGet())
|
|
}
|
|
|
|
// handlePeersGet returns a gorm handler function.
|
|
//
|
|
// @ID peers_handlePeersGet
|
|
// @Tags Peer
|
|
// @Summary Get peers for the given interface.
|
|
// @Produce json
|
|
// @Success 200 {object} []model.Peer
|
|
// @Failure 500 {object} model.Error
|
|
// @Router /peer/all/{id} [get]
|
|
func (e peerEndpoint) handlePeersGet() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
interfaceId := c.Param("id")
|
|
if interfaceId == "" {
|
|
c.JSON(http.StatusBadRequest, model2.Error{Code: http.StatusInternalServerError, Message: "missing id parameter"})
|
|
return
|
|
}
|
|
|
|
_, peers, err := e.app.WireGuard.GetInterfaceAndPeers(c.Request.Context(), domain.InterfaceIdentifier(interfaceId))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model2.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model2.NewPeers(peers))
|
|
}
|
|
}
|