-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
173 lines (137 loc) · 3.34 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"os"
"strconv"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)
// App ...
type App struct {
Router *gin.Engine
DB *sql.DB
}
func (a *App) getEndpoints(c *gin.Context) {
endpoints, err := getEndpoints(a.DB, 0, 10)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, endpoints)
}
func (a *App) updateEndpoint(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.String(http.StatusBadRequest, "Invalid endpoint ID")
return
}
var e endpoint
if c.ShouldBind(&e) != nil {
c.String(http.StatusBadRequest, "Invalid request body")
}
e.ID = id
err = e.updateEndpoint(a.DB)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, e)
}
func (a *App) getEndpoint(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.String(http.StatusBadRequest, "Invalid endpoint ID")
return
}
e := endpoint{ID: id}
if err := e.getEndpoint(a.DB); err != nil {
switch err {
case sql.ErrNoRows:
c.JSON(http.StatusNotFound, gin.H{"error": "Endpoint not found"})
default:
c.JSON(http.StatusInternalServerError, err.Error())
}
return
}
c.JSON(http.StatusOK, e)
}
func (a *App) createEndpoint(c *gin.Context) {
var e endpoint
if c.ShouldBind(&e) != nil {
c.String(http.StatusBadRequest, "Invalid request body")
return
}
if !e.isValidURL() {
c.String(http.StatusBadRequest, "Invalid URL")
return
}
err := e.createEndpoint(a.DB)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusCreated, e)
}
func (a *App) deleteEndpoint(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.String(http.StatusBadRequest, "Invalid endpoint ID")
return
}
e := endpoint{ID: id}
if err := e.deleteEndpoint(a.DB); err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"result": "success",
})
}
func (a *App) getClientContent(c *gin.Context) {
url := c.Param("url")
e := endpoint{URL: url}
if err := e.getEndpointByURL(a.DB); err != nil {
switch err {
case sql.ErrNoRows:
c.JSON(http.StatusNotFound, gin.H{"error": "Endpoint not found"})
default:
c.JSON(http.StatusInternalServerError, err.Error())
}
return
}
var result map[string]interface{}
json.Unmarshal([]byte(e.Content), &result)
c.JSON(http.StatusOK, result)
}
func (a *App) initializeRoutes() {
a.Router.Use(cors.New(cors.Config{
AllowOrigins: []string{os.Getenv("CLIENT_URL")},
AllowCredentials: true,
AllowMethods: []string{"PUT", "PATCH", "GET", "DELETE"},
AllowHeaders: []string{"Origin", "content-type"},
}))
a.Router.GET("/endpoints", a.getEndpoints)
a.Router.POST("/endpoint", a.createEndpoint)
a.Router.GET("/endpoint/:id", a.getEndpoint)
a.Router.PUT("/endpoint/:id", a.updateEndpoint)
a.Router.DELETE("/endpoint/:id", a.deleteEndpoint)
a.Router.GET("/temp/:url", a.getClientContent)
}
// Initialize ...
func (a *App) Initialize(connectionString string) {
var err error
a.DB, err = sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
a.Router = gin.Default()
a.initializeRoutes()
}
// Run ...
func (a *App) Run() {
a.Router.Run()
}