🎨 Make API functions more modular
This commit is contained in:
121
api.go
121
api.go
@ -29,6 +29,19 @@ func initApi() {
|
||||
router.Run(fmt.Sprintf("%s:%d", config.Web.Bind, config.Web.Port))
|
||||
}
|
||||
|
||||
func httpSendRes(c *gin.Context, err gin.H, res gin.H) {
|
||||
if err != nil {
|
||||
c.JSON(400, err)
|
||||
return
|
||||
}
|
||||
|
||||
if res == nil {
|
||||
c.JSON(200, gin.H{"success": true})
|
||||
} else {
|
||||
c.JSON(200, res)
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetPort(c *gin.Context) {
|
||||
id := c.Query("UUID")
|
||||
if id == "" {
|
||||
@ -58,13 +71,17 @@ func handleCreatePort(c *gin.Context) {
|
||||
c.JSON(500, gin.H{"success": false, "error": "Parsing request body failed!"})
|
||||
return
|
||||
}
|
||||
|
||||
res, err := createPort(body)
|
||||
httpSendRes(c, err, res)
|
||||
}
|
||||
|
||||
func createPort(body CreatePortRequest) (gin.H, gin.H) {
|
||||
if body.Name == "" || body.Backend == "" || body.Channels == 0 || body.Type == "" {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Required parameters missing!"})
|
||||
return
|
||||
return nil, gin.H{"success": false, "error": "Required parameters missing!"}
|
||||
}
|
||||
if body.Type != "input" && body.Type != "output" {
|
||||
c.JSON(400, gin.H{"success": false, "error": "`Type` must either be `input` or `output`"})
|
||||
return
|
||||
return nil, gin.H{"success": false, "error": "`Type` must either be `input` or `output`"}
|
||||
}
|
||||
|
||||
id := uuid.New()
|
||||
@ -91,29 +108,24 @@ func handleCreatePort(c *gin.Context) {
|
||||
portConfig.Output = append(portConfig.Output, newPort)
|
||||
}
|
||||
saveConfig("ports", &portConfig)
|
||||
|
||||
c.JSON(200, gin.H{"success": true, "UUID": newPort.UUID})
|
||||
}
|
||||
|
||||
type DeletePortRequest struct {
|
||||
UUID string
|
||||
return gin.H{"success": true, "UUID": id}, nil
|
||||
}
|
||||
|
||||
func handleDeletePort(c *gin.Context) {
|
||||
var body DeletePortRequest
|
||||
if err := c.BindJSON(&body); err != nil {
|
||||
c.JSON(500, gin.H{"success": false, "error": "Parsing request body failed!"})
|
||||
return
|
||||
}
|
||||
if body.UUID == "" {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Required parameters missing!"})
|
||||
return
|
||||
id := c.Query("UUID")
|
||||
|
||||
res := deletePort(id)
|
||||
httpSendRes(c, res, nil)
|
||||
}
|
||||
|
||||
func deletePort(id string) gin.H {
|
||||
if id == "" {
|
||||
return gin.H{"success": false, "error": "Required parameters missing!"}
|
||||
}
|
||||
|
||||
port, portIndex, portType := findPort(body.UUID)
|
||||
port, portIndex, portType := findPort(id)
|
||||
if portIndex < 0 {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Port with provided UUID does not exist!"})
|
||||
return
|
||||
return gin.H{"success": false, "error": "Port with provided UUID does not exist!"}
|
||||
}
|
||||
|
||||
// Remove the port from the configuration
|
||||
@ -127,6 +139,7 @@ func handleDeletePort(c *gin.Context) {
|
||||
unregisterPort(port)
|
||||
|
||||
saveConfig("ports", &portConfig)
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleGetState(c *gin.Context) {
|
||||
@ -160,15 +173,19 @@ func handleSetState(c *gin.Context) {
|
||||
}
|
||||
|
||||
id := c.Query("UUID")
|
||||
|
||||
res, errMsg := setState(id, body)
|
||||
httpSendRes(c, errMsg, res)
|
||||
}
|
||||
|
||||
func setState(id string, body SetPortRequest) (gin.H, gin.H) {
|
||||
if id == "" {
|
||||
c.JSON(400, gin.H{"success": false, "error": "`UUID` parameter missing!"})
|
||||
return
|
||||
return nil, gin.H{"success": false, "error": "`UUID` parameter missing!"}
|
||||
}
|
||||
|
||||
port, portIndex, _ := findPort(id)
|
||||
if portIndex < 0 {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Port with provided UUID does not exist!"})
|
||||
return
|
||||
return nil, gin.H{"success": false, "error": "Port with provided UUID does not exist!"}
|
||||
}
|
||||
|
||||
if body.Mute != nil {
|
||||
@ -181,8 +198,8 @@ func handleSetState(c *gin.Context) {
|
||||
port.State.Balance = *body.Balance
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"success": true, "state": port.State})
|
||||
saveConfig("ports", &portConfig)
|
||||
return gin.H{"success": true, "state": port.State}, nil
|
||||
}
|
||||
|
||||
type CreateRouteRequest struct {
|
||||
@ -198,26 +215,28 @@ func handleCreateRoute(c *gin.Context) {
|
||||
}
|
||||
|
||||
id := c.Query("UUID")
|
||||
|
||||
res := createRoute(id, body)
|
||||
httpSendRes(c, res, nil)
|
||||
}
|
||||
|
||||
func createRoute(id string, body CreateRouteRequest) gin.H {
|
||||
if id == "" || body.To == "" {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Required parameters missing!"})
|
||||
return
|
||||
return gin.H{"success": false, "error": "Required parameters missing!"}
|
||||
}
|
||||
|
||||
from, fromIndex, fromType := findPort(id)
|
||||
_, toIndex, toType := findPort(body.To)
|
||||
if fromIndex < 0 || toIndex < 0 {
|
||||
c.JSON(400, gin.H{"success": false, "error": "One of `UUID` or `To` does not exist!"})
|
||||
return
|
||||
return gin.H{"success": false, "error": "One of `UUID` or `To` does not exist!"}
|
||||
}
|
||||
if fromType != "input" || toType != "output" {
|
||||
c.JSON(400, gin.H{"success": false, "error": "`UUID` needs to be an input and `To` an output port!"})
|
||||
return
|
||||
return gin.H{"success": false, "error": "`UUID` needs to be an input and `To` an output port!"}
|
||||
}
|
||||
|
||||
from.Route = append(from.Route, PortRoute{ToUUID: body.To, Mute: true, Volume: 0, Balance: 0})
|
||||
saveConfig("ports", &portConfig)
|
||||
|
||||
c.JSON(200, gin.H{"success": true})
|
||||
return nil
|
||||
}
|
||||
|
||||
type SetRouteRequest struct {
|
||||
@ -236,15 +255,19 @@ func handleSetRoute(c *gin.Context) {
|
||||
|
||||
fromId := c.Query("UUID")
|
||||
toId := c.Query("To")
|
||||
|
||||
res, errMsg := setRoute(fromId, toId, body)
|
||||
httpSendRes(c, errMsg, res)
|
||||
}
|
||||
|
||||
func setRoute(fromId string, toId string, body SetRouteRequest) (gin.H, gin.H) {
|
||||
if fromId == "" || toId == "" {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Required parameters missing!"})
|
||||
return
|
||||
return nil, gin.H{"success": false, "error": "Required parameters missing!"}
|
||||
}
|
||||
|
||||
from, fromIndex, _ := findPort(fromId)
|
||||
if fromIndex < 0 {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Port with provided UUID does not exist!"})
|
||||
return
|
||||
return nil, gin.H{"success": false, "error": "Port with provided UUID does not exist!"}
|
||||
}
|
||||
for i, r := range from.Route {
|
||||
if r.ToUUID != toId {
|
||||
@ -262,25 +285,28 @@ func handleSetRoute(c *gin.Context) {
|
||||
}
|
||||
|
||||
saveConfig("ports", &portConfig)
|
||||
c.JSON(200, gin.H{"success": true})
|
||||
return
|
||||
return gin.H{"success": true, "state": from.Route[i]}, nil
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"success": false, "error": "No such route exists!"})
|
||||
return nil, gin.H{"success": false, "error": "No such route exists!"}
|
||||
}
|
||||
|
||||
func handleDeleteRoute(c *gin.Context) {
|
||||
fromId := c.Query("UUID")
|
||||
toId := c.Query("To")
|
||||
|
||||
res := deleteRoute(fromId, toId)
|
||||
httpSendRes(c, res, nil)
|
||||
}
|
||||
|
||||
func deleteRoute(fromId string, toId string) gin.H {
|
||||
if fromId == "" || toId == "" {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Required parameters missing!"})
|
||||
return
|
||||
return gin.H{"success": false, "error": "Required parameters missing!"}
|
||||
}
|
||||
|
||||
from, fromIndex, _ := findPort(fromId)
|
||||
if fromIndex < 0 {
|
||||
c.JSON(400, gin.H{"success": false, "error": "Port with provided UUID does not exist!"})
|
||||
return
|
||||
return gin.H{"success": false, "error": "Port with provided UUID does not exist!"}
|
||||
}
|
||||
for i, r := range from.Route {
|
||||
if r.ToUUID != toId {
|
||||
@ -289,9 +315,8 @@ func handleDeleteRoute(c *gin.Context) {
|
||||
|
||||
from.Route = append(from.Route[:i], from.Route[i+1:]...)
|
||||
saveConfig("ports", &portConfig)
|
||||
c.JSON(200, gin.H{"success": true})
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"success": false, "error": "No such route exists!"})
|
||||
return gin.H{"success": false, "error": "No such route exists!"}
|
||||
}
|
||||
|
Reference in New Issue
Block a user