|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "github.com/riotpot/api" |
| 10 | + "github.com/riotpot/internal/proxy" |
| 11 | + "github.com/riotpot/pkg/services" |
| 12 | +) |
| 13 | + |
| 14 | +// Structures used to serialize data |
| 15 | +type Proxy struct { |
| 16 | + ID int `json:"id" binding:"required" gorm:"primary_key"` |
| 17 | + Port int `json:"port" binding:"required"` |
| 18 | + Protocol string `json:"protocol" binding:"required"` |
| 19 | + Status bool `json:"status"` |
| 20 | + Service *Service `json:"service"` |
| 21 | +} |
| 22 | + |
| 23 | +type Service struct { |
| 24 | + ID int `json:"id" binding:"required" gorm:"primary_key"` |
| 25 | + Port int `json:"port" binding:"required"` |
| 26 | + Name string `json:"name" binding:"required"` |
| 27 | + Host string `json:"host" binding:"required"` |
| 28 | +} |
| 29 | + |
| 30 | +// Routes |
| 31 | +var ( |
| 32 | + // Proxy Routes |
| 33 | + proxyRoutes = []api.Route{ |
| 34 | + // Get proxies |
| 35 | + api.NewRoute("/", "GET", getProxies), |
| 36 | + // Get a proxy by port |
| 37 | + api.NewRoute("/proxy/:port", "GET", getProxy), |
| 38 | + // Post proxy by port |
| 39 | + api.NewRoute("/proxy/:port", "POST", postProxy), |
| 40 | + // Delete a proxy by port |
| 41 | + api.NewRoute("/proxy/:port", "DELETE", delProxy), |
| 42 | + // Patch (Not update) a proxy by port |
| 43 | + api.NewRoute("/proxy/:port", "PATCH", patchProxy), |
| 44 | + } |
| 45 | +) |
| 46 | + |
| 47 | +// Routers |
| 48 | +var ( |
| 49 | + ProxyRouter = api.NewRouter("/proxies", proxyRoutes, nil) |
| 50 | +) |
| 51 | + |
| 52 | +func newService(serv services.Service) (sv *Service) { |
| 53 | + if serv != nil { |
| 54 | + sv = &Service{ |
| 55 | + Port: serv.GetPort(), |
| 56 | + Name: serv.GetName(), |
| 57 | + Host: serv.GetName(), |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return |
| 62 | +} |
| 63 | + |
| 64 | +func newProxy(px proxy.Proxy) *Proxy { |
| 65 | + serv := newService(px.Service()) |
| 66 | + |
| 67 | + return &Proxy{ |
| 68 | + Port: px.Port(), |
| 69 | + Protocol: px.Protocol(), |
| 70 | + Status: px.Alive(), |
| 71 | + Service: serv, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// GET proxies registered |
| 76 | +// Contains a filter to get proxies by port |
| 77 | +func getProxies(ctx *gin.Context) { |
| 78 | + casted := []Proxy{} |
| 79 | + |
| 80 | + // Iterate through the proxies registered |
| 81 | + for _, px := range proxy.Proxies.Proxies() { |
| 82 | + // Serialize the proxy |
| 83 | + pr := newProxy(px) |
| 84 | + // Append the proxy tot he casted |
| 85 | + casted = append(casted, *pr) |
| 86 | + } |
| 87 | + |
| 88 | + // Set the header and transform the struct to JSON format |
| 89 | + ctx.JSON(http.StatusOK, casted) |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +// GET proxy by port ":port" |
| 94 | +func getProxy(ctx *gin.Context) { |
| 95 | + port, err := strconv.Atoi(ctx.Param("port")) |
| 96 | + if err != nil { |
| 97 | + log.Fatal(err) |
| 98 | + } |
| 99 | + |
| 100 | + // Get the proxy |
| 101 | + px, err := proxy.Proxies.GetProxy(port) |
| 102 | + |
| 103 | + // If the proxy could not be found, let the user know |
| 104 | + if err != nil { |
| 105 | + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + // Serialize the proxy and send it as a response |
| 110 | + pr := newProxy(px) |
| 111 | + ctx.JSON(http.StatusOK, pr) |
| 112 | +} |
| 113 | + |
| 114 | +// POST a proxy by port ":port" |
| 115 | +func postProxy(ctx *gin.Context) {} |
| 116 | + |
| 117 | +// DELETE a proxy by port ":port" |
| 118 | +func delProxy(ctx *gin.Context) {} |
| 119 | + |
| 120 | +// PATCH proxy by port ":port" |
| 121 | +// Can update port, protocol, status and service |
| 122 | +func patchProxy(ctx *gin.Context) {} |
| 123 | + |
| 124 | +// Routers |
| 125 | +/* |
| 126 | +var ( |
| 127 | + ProxyRouter = &api.AbstractRouter{ |
| 128 | + path: "proxy", |
| 129 | + routes: []Route{ |
| 130 | + GetProxies, |
| 131 | + GetProxy, |
| 132 | + }, |
| 133 | + } |
| 134 | +) |
| 135 | +*/ |
| 136 | + |
| 137 | +/* |
| 138 | +func getProxies(ctx *gin.Context) { |
| 139 | + // List of proxies |
| 140 | + var proxies []Proxy |
| 141 | +
|
| 142 | + // Send the response with the proxies |
| 143 | + ctx.Header("Content-Type", "application/json") |
| 144 | + ctx.JSON(http.StatusOK, proxies) |
| 145 | +
|
| 146 | +} |
| 147 | +*/ |
0 commit comments