Skip to content

Commit 577cb6c

Browse files
authored
Merge pull request #14 from VersusControl/feature/add-query-params
(feature): add query parameters overwrite config
2 parents 685b3b3 + cb9ccf2 commit 577cb6c

File tree

6 files changed

+147
-4
lines changed

6 files changed

+147
-4
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,13 @@ curl -X POST http://localhost:3000/api/incidents \
390390

391391
![Slack Alert](src/docs/images/versus-result.png)
392392

393+
## Advanced API Usage
394+
We provide a way to overwrite configuration values using query parameters, allowing you to send alerts to different channel IDs based on the service.
395+
396+
| Query | Description |
397+
|------------------|-------------|
398+
| `slack_channel_id` | The ID of the Slack channel where alerts will be sent. Use: `/api/incidents?slack_channel_id=<your_vaule>` |
399+
393400
## Roadmap
394401

395402
- [x] Add Telegram support

pkg/common/clone_config.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package common
2+
3+
func cloneConfig(src *Config) *Config {
4+
if src == nil {
5+
return nil
6+
}
7+
8+
// Create a new Config and copy all fields
9+
cloned := &Config{
10+
Name: src.Name,
11+
Host: src.Host,
12+
Port: src.Port,
13+
Alert: cloneAlertConfig(src.Alert),
14+
Queue: cloneQueueConfig(src.Queue),
15+
}
16+
17+
return cloned
18+
}
19+
20+
// Helper function to deep clone the AlertConfig struct
21+
func cloneAlertConfig(src AlertConfig) AlertConfig {
22+
return AlertConfig{
23+
DebugBody: src.DebugBody,
24+
Slack: cloneSlackConfig(src.Slack),
25+
Telegram: cloneTelegramConfig(src.Telegram),
26+
Email: cloneEmailConfig(src.Email),
27+
}
28+
}
29+
30+
// Helper function to deep clone the SlackConfig struct
31+
func cloneSlackConfig(src SlackConfig) SlackConfig {
32+
return SlackConfig{
33+
Enable: src.Enable,
34+
Token: src.Token,
35+
ChannelID: src.ChannelID,
36+
TemplatePath: src.TemplatePath,
37+
}
38+
}
39+
40+
// Helper function to deep clone the TelegramConfig struct
41+
func cloneTelegramConfig(src TelegramConfig) TelegramConfig {
42+
return TelegramConfig{
43+
Enable: src.Enable,
44+
BotToken: src.BotToken,
45+
ChatID: src.ChatID,
46+
TemplatePath: src.TemplatePath,
47+
}
48+
}
49+
50+
// Helper function to deep clone the EmailConfig struct
51+
func cloneEmailConfig(src EmailConfig) EmailConfig {
52+
return EmailConfig{
53+
Enable: src.Enable,
54+
SMTPHost: src.SMTPHost,
55+
SMTPPort: src.SMTPPort,
56+
Username: src.Username,
57+
Password: src.Password,
58+
To: src.To,
59+
Subject: src.Subject,
60+
TemplatePath: src.TemplatePath,
61+
}
62+
}
63+
64+
// Helper function to deep clone the QueueConfig struct
65+
func cloneQueueConfig(src QueueConfig) QueueConfig {
66+
return QueueConfig{
67+
Enable: src.Enable,
68+
SNS: cloneSNSConfig(src.SNS),
69+
SQS: cloneSQSConfig(src.SQS),
70+
PubSub: clonePubSubConfig(src.PubSub),
71+
AzBus: cloneAzBusConfig(src.AzBus),
72+
}
73+
}
74+
75+
// Helper function to deep clone the SNSConfig struct
76+
func cloneSNSConfig(src SNSConfig) SNSConfig {
77+
return SNSConfig{
78+
Enable: src.Enable,
79+
}
80+
}
81+
82+
// Helper function to deep clone the SQSConfig struct
83+
func cloneSQSConfig(src SQSConfig) SQSConfig {
84+
return SQSConfig{
85+
Enable: src.Enable,
86+
QueueURL: src.QueueURL,
87+
}
88+
}
89+
90+
// Helper function to deep clone the PubSubConfig struct
91+
func clonePubSubConfig(src PubSubConfig) PubSubConfig {
92+
return PubSubConfig{
93+
Enable: src.Enable,
94+
}
95+
}
96+
97+
// Helper function to deep clone the AzBusConfig struct
98+
func cloneAzBusConfig(src AzBusConfig) AzBusConfig {
99+
return AzBusConfig{
100+
Enable: src.Enable,
101+
}
102+
}

pkg/common/config.go

+11
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,14 @@ func GetConfig() *Config {
132132
}
133133
return cfg
134134
}
135+
136+
func GetConfigWitParamsOverwrite(paramsOverwrite *map[string]string) *Config {
137+
// Clone the global cfg
138+
clonedCfg := cloneConfig(cfg)
139+
140+
if v := (*paramsOverwrite)["slack_channel_id"]; v != "" {
141+
clonedCfg.Alert.Slack.ChannelID = v
142+
}
143+
144+
return clonedCfg
145+
}

pkg/controllers/incident.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ func CreateIncident(c *fiber.Ctx) error {
2424
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid input"})
2525
}
2626

27-
err := services.CreateIncident("", body)
27+
var err error
28+
29+
// If query parameters exist, get the value to overwrite the default configuration
30+
if len(c.Queries()) > 0 {
31+
overwriteVaule := c.Queries()
32+
err = services.CreateIncident("", body, &overwriteVaule)
33+
} else {
34+
err = services.CreateIncident("", body)
35+
}
36+
2837
if err != nil {
2938
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
3039
}

pkg/services/incident.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ import (
77
m "versus-incident/pkg/models"
88
)
99

10-
func CreateIncident(teamID string, content interface{}) error {
10+
func CreateIncident(teamID string, content interface{}, params ...*map[string]string) error {
11+
var cfg *common.Config
12+
13+
if len(params) > 0 {
14+
cfg = common.GetConfigWitParamsOverwrite(params[0])
15+
} else {
16+
cfg = common.GetConfig()
17+
}
18+
1119
incident := m.NewIncident(teamID, content)
12-
cfg := common.GetConfig()
1320

1421
factory := common.NewProviderFactory(cfg)
1522
providers, err := factory.CreateProviders()

src/getting_started.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,11 @@ curl -X POST http://localhost:3000/api/incidents \
355355

356356
## Result
357357

358-
![Slack Alert](docs/images/versus-result.png)
358+
![Slack Alert](docs/images/versus-result.png)
359+
360+
## Advanced API Usage
361+
We provide a way to overwrite configuration values using query parameters, allowing you to send alerts to different channel IDs based on the service.
362+
363+
| Query | Description |
364+
|------------------|-------------|
365+
| `slack_channel_id` | The ID of the Slack channel where alerts will be sent. Use: `/api/incidents?slack_channel_id=<your_vaule>` |

0 commit comments

Comments
 (0)