-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomEndpoint.go
75 lines (62 loc) · 2.02 KB
/
customEndpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package controllers
import (
"fmt"
"github.com/cemalkilic/jsonServer/database"
"github.com/cemalkilic/jsonServer/service"
"github.com/cemalkilic/jsonServer/utils"
"github.com/cemalkilic/jsonServer/utils/validator"
"github.com/gin-gonic/gin"
"strings"
)
type CustomEndpointController struct {
dataStore database.DataStore
validator *validator.CustomValidator
}
func NewCustomEndpointController(db database.DataStore, v *validator.CustomValidator) *CustomEndpointController {
return &CustomEndpointController{
dataStore: db,
validator: v,
}
}
func (cec *CustomEndpointController) SetDB(dataStore database.DataStore) {
cec.dataStore = dataStore
}
func (cec *CustomEndpointController) AddCustomEndpoint(c *gin.Context) {
var addEndpointRequest service.AddEndpointParams
_ = c.ShouldBindJSON(&addEndpointRequest)
ctxUsername, exists := c.Get("username")
if exists {
addEndpointRequest.Username = fmt.Sprintf("%s", ctxUsername)
}
srv := service.NewService(cec.dataStore, cec.validator)
response, err := srv.AddEndpoint(addEndpointRequest)
if err != nil {
internalError(c, err)
return
}
if e, ok := response.Err.(error); ok && e != nil {
internalError(c, e)
return
}
fullEndpointURL := utils.GetFullHTTPUrl(c.Request.Host, response.Endpoint, c.Request.TLS != nil)
c.JSON(200, gin.H{
"endpoint": fullEndpointURL,
})
}
func (cec *CustomEndpointController) GetCustomEndpoint(c *gin.Context) {
url := c.Request.URL.Path
srv := service.NewService(cec.dataStore, cec.validator)
response, err := srv.GetCustomEndpoint(service.GetEndpointParams{Endpoint: url})
if err != nil {
internalError(c, err)
return
}
if e, ok := response.Err.(error); ok && e != nil {
internalError(c, e)
return
}
c.DataFromReader(response.StatusCode,
int64(len(response.Content)),
gin.MIMEJSON,
strings.NewReader(response.Content), nil)
}