Dragonfly2

Форк
0
137 строк · 3.5 Кб
1
/*
2
 *     Copyright 2022 The Dragonfly Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package handlers
18

19
import (
20
	"net/http"
21

22
	"github.com/gin-gonic/gin"
23

24
	// nolint
25
	_ "d7y.io/dragonfly/v2/manager/models"
26
	"d7y.io/dragonfly/v2/manager/types"
27
)
28

29
// @Summary Create Peer
30
// @Description Create by json config
31
// @Tags Peer
32
// @Accept json
33
// @Produce json
34
// @Param Peer body types.CreatePeerRequest true "Peer"
35
// @Success 200 {object} models.Peer
36
// @Failure 400
37
// @Failure 404
38
// @Failure 500
39
// @Router /peers [post]
40
func (h *Handlers) CreatePeer(ctx *gin.Context) {
41
	var json types.CreatePeerRequest
42
	if err := ctx.ShouldBindJSON(&json); err != nil {
43
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
44
		return
45
	}
46

47
	peer, err := h.service.CreatePeer(ctx.Request.Context(), json)
48
	if err != nil {
49
		ctx.Error(err) // nolint: errcheck
50
		return
51
	}
52

53
	ctx.JSON(http.StatusOK, peer)
54
}
55

56
// @Summary Destroy Peer
57
// @Description Destroy by id
58
// @Tags Peer
59
// @Accept json
60
// @Produce json
61
// @Param id path string true "id"
62
// @Success 200
63
// @Failure 400
64
// @Failure 404
65
// @Failure 500
66
// @Router /peers/{id} [delete]
67
func (h *Handlers) DestroyPeer(ctx *gin.Context) {
68
	var params types.PeerParams
69
	if err := ctx.ShouldBindUri(&params); err != nil {
70
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
71
		return
72
	}
73

74
	if err := h.service.DestroyPeer(ctx.Request.Context(), params.ID); err != nil {
75
		ctx.Error(err) // nolint: errcheck
76
		return
77
	}
78

79
	ctx.Status(http.StatusOK)
80
}
81

82
// @Summary Get Peer
83
// @Description Get Peer by id
84
// @Tags Peer
85
// @Accept json
86
// @Produce json
87
// @Param id path string true "id"
88
// @Success 200 {object} models.Peer
89
// @Failure 400
90
// @Failure 404
91
// @Failure 500
92
// @Router /peers/{id} [get]
93
func (h *Handlers) GetPeer(ctx *gin.Context) {
94
	var params types.PeerParams
95
	if err := ctx.ShouldBindUri(&params); err != nil {
96
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
97
		return
98
	}
99

100
	peer, err := h.service.GetPeer(ctx.Request.Context(), params.ID)
101
	if err != nil {
102
		ctx.Error(err) // nolint: errcheck
103
		return
104
	}
105

106
	ctx.JSON(http.StatusOK, peer)
107
}
108

109
// @Summary Get Peers
110
// @Description Get Peers
111
// @Tags Peer
112
// @Accept json
113
// @Produce json
114
// @Param page query int true "current page" default(0)
115
// @Param per_page query int true "return max item count, default 10, max 50" default(10) minimum(2) maximum(50)
116
// @Success 200 {object} []models.Peer
117
// @Failure 400
118
// @Failure 404
119
// @Failure 500
120
// @Router /peers [get]
121
func (h *Handlers) GetPeers(ctx *gin.Context) {
122
	var query types.GetPeersQuery
123
	if err := ctx.ShouldBindQuery(&query); err != nil {
124
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
125
		return
126
	}
127

128
	h.setPaginationDefault(&query.Page, &query.PerPage)
129
	peers, count, err := h.service.GetPeers(ctx.Request.Context(), query)
130
	if err != nil {
131
		ctx.Error(err) // nolint: errcheck
132
		return
133
	}
134

135
	h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
136
	ctx.JSON(http.StatusOK, peers)
137
}
138

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.