mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-20 16:14:29 +01:00
Add Endpoint for linking devices #9
Add /v1/link/:device_name as end point to be able to link to an existing device.
This commit is contained in:
22
src/main.go
22
src/main.go
@@ -7,14 +7,14 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/h2non/filetype"
|
|
||||||
"github.com/satori/go.uuid"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/h2non/filetype"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const groupPrefix = "group."
|
const groupPrefix = "group."
|
||||||
@@ -32,7 +32,6 @@ func convertInternalGroupIdToGroupId(internalId string) string {
|
|||||||
return groupPrefix + base64.StdEncoding.EncodeToString([]byte(internalId))
|
return groupPrefix + base64.StdEncoding.EncodeToString([]byte(internalId))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func getStringInBetween(str string, start string, end string) (result string) {
|
func getStringInBetween(str string, start string, end string) (result string) {
|
||||||
i := strings.Index(str, start)
|
i := strings.Index(str, start)
|
||||||
if i == -1 {
|
if i == -1 {
|
||||||
@@ -210,7 +209,6 @@ func runSignalCli(wait bool, args []string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan error, 1)
|
done := make(chan error, 1)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
done <- cmd.Wait()
|
done <- cmd.Wait()
|
||||||
}()
|
}()
|
||||||
@@ -230,7 +228,7 @@ func runSignalCli(wait bool, args []string) (string, error) {
|
|||||||
} else {
|
} else {
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return "", err
|
||||||
}
|
}
|
||||||
cmd.Start()
|
cmd.Start()
|
||||||
buf := bufio.NewReader(stdout) // Notice that this is not in a loop
|
buf := bufio.NewReader(stdout) // Notice that this is not in a loop
|
||||||
@@ -258,22 +256,20 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/v1/link/:device_name", func(c *gin.Context) {
|
router.POST("/v1/link/:device_name", func(c *gin.Context) {
|
||||||
device_name := c.Param("device_name")
|
deviceName := c.Param("device_name")
|
||||||
log.Info("Linking device")
|
if deviceName == "" {
|
||||||
if device_name == "" {
|
|
||||||
c.JSON(400, gin.H{"error": "Please provide a name for the device"})
|
c.JSON(400, gin.H{"error": "Please provide a name for the device"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
command := []string{"--config", *signalCliConfig, "link", "-n", device_name}
|
command := []string{"--config", *signalCliConfig, "link", "-n", deviceName}
|
||||||
|
|
||||||
out, err := runSignalCli(false, command)
|
out, err := runSignalCli(false, command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
c.String(200, gin.H{"uri": string(out)})
|
||||||
c.String(200, string(out))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/v1/register/:number", func(c *gin.Context) {
|
router.POST("/v1/register/:number", func(c *gin.Context) {
|
||||||
|
|||||||
Reference in New Issue
Block a user