🎨 Make API functions more modular

This commit is contained in:
2024-02-28 10:27:02 +01:00
parent 4b567ddb43
commit 4bdeb3a666

121
api.go
View File

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