Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds filters to the getProxies route #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 deletions pkg/api/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/riotpot/pkg/proxy"
Expand Down Expand Up @@ -80,20 +81,75 @@ func NewProxy(px proxy.Proxy) *GetProxy {
}
}

// TODO [7/17/2022]: Add filters to this method
// GET proxies registered
// Contains a filter to get proxies by port
// Contains a filter to get proxies by port, id, status
func getProxies(ctx *gin.Context) {
casted := []GetProxy{}
id := ctx.Query("id")
portStr := ctx.Query("port")
status := ctx.Query("status")

// Iterate through the proxies registered
switch {
case id != "":
getProxiesByID(ctx, id)

case portStr != "":
getProxiesByPort(ctx, portStr)

case status != "":
getProxiesByStatus(ctx, status)

default:
getAllProxies(ctx)
}
}

func getProxiesByID(ctx *gin.Context, id string) {
pr, err := proxy.Proxies.GetProxy(id)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Serialize the proxy and send it as a response
px := NewProxy(pr)
ctx.JSON(http.StatusOK, px)
}

func getProxiesByPort(ctx *gin.Context, portStr string) {
port, err := strconv.Atoi(portStr)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid port number"})
return
}

pr, err := proxy.Proxies.GetProxyByPort(port)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Serialize the proxy and send it as a response
px := NewProxy(pr)
ctx.JSON(http.StatusOK, px)
}

func getProxiesByStatus(ctx *gin.Context, status string) {
casted := make([]GetProxy, 0)
for _, pr := range proxy.Proxies.GetProxyByStatus(status) {
// Serialize the proxy
px := NewProxy(pr)
casted = append(casted, *px)
}

ctx.JSON(http.StatusOK, casted)
}

func getAllProxies(ctx *gin.Context) {
casted := make([]GetProxy, 0)
for _, px := range proxy.Proxies.GetProxies() {
// Serialize the proxy
pr := NewProxy(px)
// Append the proxy to the casted
//Append the proxy to the casted
casted = append(casted, *pr)
}

// Set the header and transform the struct to JSON format
ctx.JSON(http.StatusOK, casted)
}
Expand Down Expand Up @@ -275,4 +331,4 @@ func changeProxyPort(ctx *gin.Context) {
// Serialize the proxy and send it as a response
pr := NewProxy(pe)
ctx.JSON(http.StatusOK, pr)
}
}
28 changes: 28 additions & 0 deletions pkg/proxy/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,31 @@ func NewProxyManager() *proxyManager {
proxies: make([]Proxy, 0),
}
}

// Returns proxy at a particular port
func (pm *proxyManager)GetProxyByPort(port int) (pe Proxy, err error) {
// Get all the proxies registered
proxies := pm.GetProxies()

for _, proxy := range proxies {
if proxy.GetPort() == port {
pe = proxy
return
}
}
err = fmt.Errorf("proxy not found")
return
}

// Returns proxy of the provided status
func (pm *proxyManager) GetProxyByStatus(status string) (pe []Proxy) {
proxies := pm.GetProxies()

for _, proxy := range proxies {
if proxy.IsRunning().String() == status {
pe = append(pe, proxy)
}
}

return pe
}