-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
67 lines (59 loc) · 1.67 KB
/
api.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
package main
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func newAPIServer() *echo.Echo {
e := echo.New()
e.HideBanner = true
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.Recover())
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${method} ${uri} | ${remote_ip} | ${status} ${error}\n",
}))
e.POST("/v1/proxy/add", addProxyAPI)
e.POST("/v1/proxy/remove", removeProxyAPI)
e.POST("/v1/proxy/exist", isExistProxyAPI)
e.GET("/v1/proxy/list", listProxyAPI)
return e
}
func addProxyAPI(c echo.Context) error {
var pr ProxyRecord
if err := c.Bind(&pr); err != nil {
return &echo.HTTPError{Code: 400, Message: "Invalid request"}
}
// add the proxy
success, err := proxyManager.Add(pr)
if err != nil {
c.JSON(200, AddProxyResponse{Success: false, Error: err.Error()})
} else {
c.JSON(200, AddProxyResponse{Success: success, Error: ""})
}
return nil
}
func removeProxyAPI(c echo.Context) error {
var pr ProxyRecord
if err := c.Bind(&pr); err != nil {
return &echo.HTTPError{Code: 400, Message: "Invalid request"}
}
if success, err := proxyManager.Remove(pr); err != nil {
c.JSON(200, RemoveProxyResponse{Success: false, Error: err.Error()})
} else {
c.JSON(200, RemoveProxyResponse{Success: success, Error: ""})
}
return nil
}
func isExistProxyAPI(c echo.Context) error {
var pr ProxyRecord
if err := c.Bind(&pr); err != nil {
return &echo.HTTPError{Code: 400, Message: "Invalid request"}
}
exist := proxyManager.Exist(pr)
c.JSON(200, IsExistProxyResponse{Exist: exist})
return nil
}
func listProxyAPI(c echo.Context) error {
proxies := proxyManager.List()
c.JSON(200, proxies)
return nil
}