added configuration endpoints

This commit is contained in:
Bernhard B
2021-01-23 19:21:17 +01:00
parent 7020d6efd6
commit 04e67d058b
5 changed files with 358 additions and 9 deletions

View File

@@ -39,6 +39,14 @@ type GroupEntry struct {
InviteLink string `json:"invite_link"` InviteLink string `json:"invite_link"`
} }
type LoggingConfiguration struct {
Level string `json:"Level"`
}
type Configuration struct {
Logging LoggingConfiguration `json:"logging"`
}
type SignalCliGroupEntry struct { type SignalCliGroupEntry struct {
Name string `json:"name"` Name string `json:"name"`
Id string `json:"id"` Id string `json:"id"`
@@ -127,6 +135,19 @@ func cleanupTmpFiles(paths []string) {
} }
} }
func getContainerId() (string, error) {
data, err := ioutil.ReadFile("/proc/1/cpuset")
if err != nil {
return "", err
}
lines := strings.Split(string(data), "\n")
if len(lines) == 0 {
return "", errors.New("Couldn't get docker container id (empty)")
}
containerId := strings.Replace(lines[0], "/docker/", "", -1)
return containerId, nil
}
func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string, func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string,
recipients []string, base64Attachments []string, isGroup bool) { recipients []string, base64Attachments []string, isGroup bool) {
cmd := []string{"--config", signalCliConfig, "-u", number, "send"} cmd := []string{"--config", signalCliConfig, "-u", number, "send"}
@@ -280,6 +301,17 @@ func getGroups(number string, signalCliConfig string) ([]GroupEntry, error) {
} }
func runSignalCli(wait bool, args []string, stdin string) (string, error) { func runSignalCli(wait bool, args []string, stdin string) (string, error) {
containerId, err := getContainerId()
log.Debug("If you want to run this command manually, run the following steps on your host system:")
if err == nil {
log.Debug("*) docker exec -it ", containerId, " /bin/bash")
} else {
log.Debug("*) docker exec -it <container id> /bin/bash")
}
log.Debug("*) su signal-api")
log.Debug("*) signal-cli ", strings.Join(args, " "))
cmd := exec.Command("signal-cli", args...) cmd := exec.Command("signal-cli", args...)
if stdin != "" { if stdin != "" {
cmd.Stdin = strings.NewReader(stdin) cmd.Stdin = strings.NewReader(stdin)
@@ -311,6 +343,7 @@ func runSignalCli(wait bool, args []string, stdin string) (string, error) {
return "", errors.New(errBuffer.String()) return "", errors.New(errBuffer.String())
} }
} }
return outBuffer.String(), nil return outBuffer.String(), nil
} else { } else {
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
@@ -1006,3 +1039,58 @@ func (a *Api) TrustIdentity(c *gin.Context) {
} }
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
} }
// @Summary Set the REST API configuration.
// @Tags General
// @Description Set the REST API configuration.
// @Accept json
// @Produce json
// @Success 201 {string} string "OK"
// @Failure 400 {object} Error
// @Param data body Configuration true "Configuration"
// @Router /v1/configuration [post]
func (a *Api) SetConfiguration(c *gin.Context) {
var req Configuration
err := c.BindJSON(&req)
if err != nil {
c.JSON(400, Error{Msg: "Couldn't process request - invalid request"})
log.Error(err.Error())
return
}
if req.Logging.Level != "" {
if req.Logging.Level == "debug" {
log.SetLevel(log.DebugLevel)
} else if req.Logging.Level == "info" {
log.SetLevel(log.InfoLevel)
} else if req.Logging.Level == "warn" {
log.SetLevel(log.WarnLevel)
} else {
c.JSON(400, Error{Msg: "Couldn't set log level - invalid log level"})
return
}
}
c.Status(http.StatusNoContent)
}
// @Summary List the REST API configuration.
// @Tags General
// @Description List the REST API configuration.
// @Accept json
// @Produce json
// @Success 200 {object} Configuration
// @Failure 400 {object} Error
// @Router /v1/configuration [get]
func (a *Api) GetConfiguration(c *gin.Context) {
logLevel := ""
if log.GetLevel() == log.DebugLevel {
logLevel = "debug"
} else if log.GetLevel() == log.InfoLevel {
logLevel = "info"
} else if log.GetLevel() == log.WarnLevel {
logLevel = "warn"
}
configuration := Configuration{Logging: LoggingConfiguration{Level: logLevel}}
c.JSON(200, configuration)
}

View File

@@ -142,6 +142,73 @@ var doc = `{
} }
} }
}, },
"/v1/configuration": {
"get": {
"description": "List the REST API configuration.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"General"
],
"summary": "List the REST API configuration.",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.Configuration"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
},
"post": {
"description": "Set the REST API configuration.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"General"
],
"summary": "Set the REST API configuration.",
"parameters": [
{
"description": "Configuration",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.Configuration"
}
}
],
"responses": {
"201": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"/v1/groups/{number}": { "/v1/groups/{number}": {
"get": { "get": {
"description": "List all Signal Groups.", "description": "List all Signal Groups.",
@@ -662,6 +729,15 @@ var doc = `{
} }
} }
}, },
"api.Configuration": {
"type": "object",
"properties": {
"logging": {
"type": "object",
"$ref": "#/definitions/api.LoggingConfiguration"
}
}
},
"api.CreateGroup": { "api.CreateGroup": {
"type": "object", "type": "object",
"properties": { "properties": {
@@ -681,9 +757,6 @@ var doc = `{
"api.GroupEntry": { "api.GroupEntry": {
"type": "object", "type": "object",
"properties": { "properties": {
"active": {
"type": "boolean"
},
"blocked": { "blocked": {
"type": "boolean" "type": "boolean"
}, },
@@ -693,6 +766,9 @@ var doc = `{
"internal_id": { "internal_id": {
"type": "string" "type": "string"
}, },
"invite_link": {
"type": "string"
},
"members": { "members": {
"type": "array", "type": "array",
"items": { "items": {
@@ -701,6 +777,18 @@ var doc = `{
}, },
"name": { "name": {
"type": "string" "type": "string"
},
"pending_invites": {
"type": "array",
"items": {
"type": "string"
}
},
"pending_requests": {
"type": "array",
"items": {
"type": "string"
}
} }
} }
}, },
@@ -724,6 +812,14 @@ var doc = `{
} }
} }
}, },
"api.LoggingConfiguration": {
"type": "object",
"properties": {
"Level": {
"type": "string"
}
}
},
"api.SendMessageV1": { "api.SendMessageV1": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -127,6 +127,73 @@
} }
} }
}, },
"/v1/configuration": {
"get": {
"description": "List the REST API configuration.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"General"
],
"summary": "List the REST API configuration.",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.Configuration"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
},
"post": {
"description": "Set the REST API configuration.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"General"
],
"summary": "Set the REST API configuration.",
"parameters": [
{
"description": "Configuration",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.Configuration"
}
}
],
"responses": {
"201": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"/v1/groups/{number}": { "/v1/groups/{number}": {
"get": { "get": {
"description": "List all Signal Groups.", "description": "List all Signal Groups.",
@@ -647,6 +714,15 @@
} }
} }
}, },
"api.Configuration": {
"type": "object",
"properties": {
"logging": {
"type": "object",
"$ref": "#/definitions/api.LoggingConfiguration"
}
}
},
"api.CreateGroup": { "api.CreateGroup": {
"type": "object", "type": "object",
"properties": { "properties": {
@@ -666,9 +742,6 @@
"api.GroupEntry": { "api.GroupEntry": {
"type": "object", "type": "object",
"properties": { "properties": {
"active": {
"type": "boolean"
},
"blocked": { "blocked": {
"type": "boolean" "type": "boolean"
}, },
@@ -678,6 +751,9 @@
"internal_id": { "internal_id": {
"type": "string" "type": "string"
}, },
"invite_link": {
"type": "string"
},
"members": { "members": {
"type": "array", "type": "array",
"items": { "items": {
@@ -686,6 +762,18 @@
}, },
"name": { "name": {
"type": "string" "type": "string"
},
"pending_invites": {
"type": "array",
"items": {
"type": "string"
}
},
"pending_requests": {
"type": "array",
"items": {
"type": "string"
}
} }
} }
}, },
@@ -709,6 +797,14 @@
} }
} }
}, },
"api.LoggingConfiguration": {
"type": "object",
"properties": {
"Level": {
"type": "string"
}
}
},
"api.SendMessageV1": { "api.SendMessageV1": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -9,6 +9,12 @@ definitions:
type: string type: string
type: array type: array
type: object type: object
api.Configuration:
properties:
logging:
$ref: '#/definitions/api.LoggingConfiguration'
type: object
type: object
api.CreateGroup: api.CreateGroup:
properties: properties:
id: id:
@@ -21,20 +27,28 @@ definitions:
type: object type: object
api.GroupEntry: api.GroupEntry:
properties: properties:
active:
type: boolean
blocked: blocked:
type: boolean type: boolean
id: id:
type: string type: string
internal_id: internal_id:
type: string type: string
invite_link:
type: string
members: members:
items: items:
type: string type: string
type: array type: array
name: name:
type: string type: string
pending_invites:
items:
type: string
type: array
pending_requests:
items:
type: string
type: array
type: object type: object
api.IdentityEntry: api.IdentityEntry:
properties: properties:
@@ -49,6 +63,11 @@ definitions:
status: status:
type: string type: string
type: object type: object
api.LoggingConfiguration:
properties:
Level:
type: string
type: object
api.SendMessageV1: api.SendMessageV1:
properties: properties:
base64_attachment: base64_attachment:
@@ -181,6 +200,50 @@ paths:
summary: Serve Attachment. summary: Serve Attachment.
tags: tags:
- Attachments - Attachments
/v1/configuration:
get:
consumes:
- application/json
description: List the REST API configuration.
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/api.Configuration'
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
summary: List the REST API configuration.
tags:
- General
post:
consumes:
- application/json
description: Set the REST API configuration.
parameters:
- description: Configuration
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.Configuration'
produces:
- application/json
responses:
"201":
description: OK
schema:
type: string
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
summary: Set the REST API configuration.
tags:
- General
/v1/groups/{number}: /v1/groups/{number}:
get: get:
consumes: consumes:

View File

@@ -20,7 +20,7 @@ import (
// @description This is the Signal Cli REST API documentation. // @description This is the Signal Cli REST API documentation.
// @tag.name General // @tag.name General
// @tag.description List general information. // @tag.description Some general endpoints.
// @tag.name Devices // @tag.name Devices
// @tag.description Register and link Devices. // @tag.description Register and link Devices.
@@ -65,6 +65,12 @@ func main() {
about.GET("", api.About) about.GET("", api.About)
} }
configuration := v1.Group("/configuration")
{
configuration.GET("", api.GetConfiguration)
configuration.POST("", api.SetConfiguration)
}
health := v1.Group("/health") health := v1.Group("/health")
{ {
health.GET("", api.Health) health.GET("", api.Health)