Skip to content

Commit 04e67d0

Browse files
author
Bernhard B
committed
added configuration endpoints
1 parent 7020d6e commit 04e67d0

File tree

5 files changed

+358
-9
lines changed

5 files changed

+358
-9
lines changed

src/api/api.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ type GroupEntry struct {
3939
InviteLink string `json:"invite_link"`
4040
}
4141

42+
type LoggingConfiguration struct {
43+
Level string `json:"Level"`
44+
}
45+
46+
type Configuration struct {
47+
Logging LoggingConfiguration `json:"logging"`
48+
}
49+
4250
type SignalCliGroupEntry struct {
4351
Name string `json:"name"`
4452
Id string `json:"id"`
@@ -127,6 +135,19 @@ func cleanupTmpFiles(paths []string) {
127135
}
128136
}
129137

138+
func getContainerId() (string, error) {
139+
data, err := ioutil.ReadFile("/proc/1/cpuset")
140+
if err != nil {
141+
return "", err
142+
}
143+
lines := strings.Split(string(data), "\n")
144+
if len(lines) == 0 {
145+
return "", errors.New("Couldn't get docker container id (empty)")
146+
}
147+
containerId := strings.Replace(lines[0], "/docker/", "", -1)
148+
return containerId, nil
149+
}
150+
130151
func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string,
131152
recipients []string, base64Attachments []string, isGroup bool) {
132153
cmd := []string{"--config", signalCliConfig, "-u", number, "send"}
@@ -280,6 +301,17 @@ func getGroups(number string, signalCliConfig string) ([]GroupEntry, error) {
280301
}
281302

282303
func runSignalCli(wait bool, args []string, stdin string) (string, error) {
304+
containerId, err := getContainerId()
305+
306+
log.Debug("If you want to run this command manually, run the following steps on your host system:")
307+
if err == nil {
308+
log.Debug("*) docker exec -it ", containerId, " /bin/bash")
309+
} else {
310+
log.Debug("*) docker exec -it <container id> /bin/bash")
311+
}
312+
log.Debug("*) su signal-api")
313+
log.Debug("*) signal-cli ", strings.Join(args, " "))
314+
283315
cmd := exec.Command("signal-cli", args...)
284316
if stdin != "" {
285317
cmd.Stdin = strings.NewReader(stdin)
@@ -311,6 +343,7 @@ func runSignalCli(wait bool, args []string, stdin string) (string, error) {
311343
return "", errors.New(errBuffer.String())
312344
}
313345
}
346+
314347
return outBuffer.String(), nil
315348
} else {
316349
stdout, err := cmd.StdoutPipe()
@@ -1006,3 +1039,58 @@ func (a *Api) TrustIdentity(c *gin.Context) {
10061039
}
10071040
c.Status(http.StatusNoContent)
10081041
}
1042+
1043+
// @Summary Set the REST API configuration.
1044+
// @Tags General
1045+
// @Description Set the REST API configuration.
1046+
// @Accept json
1047+
// @Produce json
1048+
// @Success 201 {string} string "OK"
1049+
// @Failure 400 {object} Error
1050+
// @Param data body Configuration true "Configuration"
1051+
// @Router /v1/configuration [post]
1052+
func (a *Api) SetConfiguration(c *gin.Context) {
1053+
var req Configuration
1054+
err := c.BindJSON(&req)
1055+
if err != nil {
1056+
c.JSON(400, Error{Msg: "Couldn't process request - invalid request"})
1057+
log.Error(err.Error())
1058+
return
1059+
}
1060+
1061+
if req.Logging.Level != "" {
1062+
if req.Logging.Level == "debug" {
1063+
log.SetLevel(log.DebugLevel)
1064+
} else if req.Logging.Level == "info" {
1065+
log.SetLevel(log.InfoLevel)
1066+
} else if req.Logging.Level == "warn" {
1067+
log.SetLevel(log.WarnLevel)
1068+
} else {
1069+
c.JSON(400, Error{Msg: "Couldn't set log level - invalid log level"})
1070+
return
1071+
}
1072+
}
1073+
c.Status(http.StatusNoContent)
1074+
}
1075+
1076+
// @Summary List the REST API configuration.
1077+
// @Tags General
1078+
// @Description List the REST API configuration.
1079+
// @Accept json
1080+
// @Produce json
1081+
// @Success 200 {object} Configuration
1082+
// @Failure 400 {object} Error
1083+
// @Router /v1/configuration [get]
1084+
func (a *Api) GetConfiguration(c *gin.Context) {
1085+
logLevel := ""
1086+
if log.GetLevel() == log.DebugLevel {
1087+
logLevel = "debug"
1088+
} else if log.GetLevel() == log.InfoLevel {
1089+
logLevel = "info"
1090+
} else if log.GetLevel() == log.WarnLevel {
1091+
logLevel = "warn"
1092+
}
1093+
1094+
configuration := Configuration{Logging: LoggingConfiguration{Level: logLevel}}
1095+
c.JSON(200, configuration)
1096+
}

src/docs/docs.go

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,73 @@ var doc = `{
142142
}
143143
}
144144
},
145+
"/v1/configuration": {
146+
"get": {
147+
"description": "List the REST API configuration.",
148+
"consumes": [
149+
"application/json"
150+
],
151+
"produces": [
152+
"application/json"
153+
],
154+
"tags": [
155+
"General"
156+
],
157+
"summary": "List the REST API configuration.",
158+
"responses": {
159+
"200": {
160+
"description": "OK",
161+
"schema": {
162+
"$ref": "#/definitions/api.Configuration"
163+
}
164+
},
165+
"400": {
166+
"description": "Bad Request",
167+
"schema": {
168+
"$ref": "#/definitions/api.Error"
169+
}
170+
}
171+
}
172+
},
173+
"post": {
174+
"description": "Set the REST API configuration.",
175+
"consumes": [
176+
"application/json"
177+
],
178+
"produces": [
179+
"application/json"
180+
],
181+
"tags": [
182+
"General"
183+
],
184+
"summary": "Set the REST API configuration.",
185+
"parameters": [
186+
{
187+
"description": "Configuration",
188+
"name": "data",
189+
"in": "body",
190+
"required": true,
191+
"schema": {
192+
"$ref": "#/definitions/api.Configuration"
193+
}
194+
}
195+
],
196+
"responses": {
197+
"201": {
198+
"description": "OK",
199+
"schema": {
200+
"type": "string"
201+
}
202+
},
203+
"400": {
204+
"description": "Bad Request",
205+
"schema": {
206+
"$ref": "#/definitions/api.Error"
207+
}
208+
}
209+
}
210+
}
211+
},
145212
"/v1/groups/{number}": {
146213
"get": {
147214
"description": "List all Signal Groups.",
@@ -662,6 +729,15 @@ var doc = `{
662729
}
663730
}
664731
},
732+
"api.Configuration": {
733+
"type": "object",
734+
"properties": {
735+
"logging": {
736+
"type": "object",
737+
"$ref": "#/definitions/api.LoggingConfiguration"
738+
}
739+
}
740+
},
665741
"api.CreateGroup": {
666742
"type": "object",
667743
"properties": {
@@ -681,9 +757,6 @@ var doc = `{
681757
"api.GroupEntry": {
682758
"type": "object",
683759
"properties": {
684-
"active": {
685-
"type": "boolean"
686-
},
687760
"blocked": {
688761
"type": "boolean"
689762
},
@@ -693,6 +766,9 @@ var doc = `{
693766
"internal_id": {
694767
"type": "string"
695768
},
769+
"invite_link": {
770+
"type": "string"
771+
},
696772
"members": {
697773
"type": "array",
698774
"items": {
@@ -701,6 +777,18 @@ var doc = `{
701777
},
702778
"name": {
703779
"type": "string"
780+
},
781+
"pending_invites": {
782+
"type": "array",
783+
"items": {
784+
"type": "string"
785+
}
786+
},
787+
"pending_requests": {
788+
"type": "array",
789+
"items": {
790+
"type": "string"
791+
}
704792
}
705793
}
706794
},
@@ -724,6 +812,14 @@ var doc = `{
724812
}
725813
}
726814
},
815+
"api.LoggingConfiguration": {
816+
"type": "object",
817+
"properties": {
818+
"Level": {
819+
"type": "string"
820+
}
821+
}
822+
},
727823
"api.SendMessageV1": {
728824
"type": "object",
729825
"properties": {

0 commit comments

Comments
 (0)