-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
267 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package adapter | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// WebhookPayload represents the structure of the data from Redis. | ||
type WebhookPayload struct { | ||
URL string `json:"url"` | ||
WebhookID string `json:"webhookId"` | ||
MessageID string `json:"messageId"` | ||
Data map[string]interface{} `json:"data"` | ||
SecretHash string `json:"secretHash"` | ||
MetaData map[string]interface{} `json:"metaData"` | ||
} | ||
|
||
type RedisConfig struct { | ||
RedisAddress string `json:"redisAddress"` | ||
RedisPassword string `json:"redisPassword"` | ||
RedisDb string `json:"redisDb"` | ||
RedisSsl string `json:"redisSsl"` | ||
RedisCaCert string `json:"redisCaCert"` | ||
RedisClientCert string `json:"redisClientCert"` | ||
RedisClientKey string `json:"redisClientKey"` | ||
RedisStreamName string `json:"redisStreamName"` | ||
RedisStreamStatusName string `json:"redisStreamStatusName"` | ||
} | ||
|
||
type Configuration struct { | ||
Redis RedisConfig `json:"redis"` | ||
SecretHashHeaderName string `json:"secretHashHeaderName"` | ||
Broker string `json:"broker"` | ||
} | ||
|
||
// Adapter defines methods for interacting with different queue systems. | ||
type Adapter interface { | ||
Connect() error | ||
SubscribeToQueue(ctx context.Context, queue chan<- WebhookPayload) error | ||
ProcessWebhooks(ctx context.Context, queue <-chan WebhookPayload) error | ||
PublishStatus(ctx context.Context, webhookID, url, created, delivered, status, deliveryError string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package adapter_manager | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"sendhooks/adapter" | ||
"sync" | ||
|
||
redisadapter "sendhooks/adapter/redis_adapter" | ||
) | ||
|
||
var ( | ||
instance adapter.Adapter | ||
once sync.Once | ||
) | ||
|
||
var ( | ||
config adapter.Configuration | ||
) | ||
|
||
// LoadConfiguration loads the configuration from a file | ||
func LoadConfiguration(filename string) { | ||
once.Do(func() { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
log.Fatalf("Failed to open config file: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
decoder := json.NewDecoder(file) | ||
err = decoder.Decode(&config) | ||
if err != nil { | ||
log.Fatalf("Failed to decode config file: %v", err) | ||
} | ||
}) | ||
} | ||
|
||
// GetConfig returns the loaded configuration | ||
func GetConfig() adapter.Configuration { | ||
return config | ||
} | ||
|
||
// Initialize initializes the appropriate adapter based on the configuration. | ||
func Initialize() { | ||
once.Do(func() { | ||
conf := GetConfig() | ||
switch conf.Broker { | ||
case "redis": | ||
instance = redisadapter.NewRedisAdapter(conf) | ||
default: | ||
log.Fatalf("Unsupported broker type: %v", conf.Broker) | ||
} | ||
|
||
err := instance.Connect() | ||
if err != nil { | ||
log.Fatalf("Failed to connect to broker: %v", err) | ||
} | ||
}) | ||
} | ||
|
||
// GetAdapter returns the singleton instance of the adapter. | ||
func GetAdapter() adapter.Adapter { | ||
return instance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package redisadapter | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"strconv" | ||
"strings" | ||
|
||
"sendhooks/adapter" | ||
"sendhooks/utils" | ||
|
||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
type RedisAdapter struct { | ||
client *redis.Client | ||
config adapter.Configuration | ||
streamName string | ||
statusStream string | ||
} | ||
|
||
// PublishStatus implements adapter.Adapter. | ||
func (r *RedisAdapter) PublishStatus(ctx context.Context, webhookID string, url string, created string, delivered string, status string, deliveryError string) error { | ||
panic("unimplemented") | ||
} | ||
|
||
// SubscribeToQueue implements adapter.Adapter. | ||
func (r *RedisAdapter) SubscribeToQueue(ctx context.Context, queue chan<- adapter.WebhookPayload) error { | ||
panic("unimplemented") | ||
} | ||
|
||
func NewRedisAdapter(config adapter.Configuration) *RedisAdapter { | ||
return &RedisAdapter{ | ||
config: config, | ||
streamName: config.Redis.RedisStreamName, | ||
statusStream: config.Redis.RedisStreamStatusName, | ||
} | ||
} | ||
|
||
func (r *RedisAdapter) Connect() error { | ||
redisAddress := r.config.Redis.RedisAddress | ||
if redisAddress == "" { | ||
redisAddress = "localhost:6379" // Default address | ||
} | ||
|
||
redisDB := r.config.Redis.RedisDb | ||
if redisDB == "" { | ||
redisDB = "0" // Default database | ||
} | ||
|
||
redisDBInt, _ := strconv.Atoi(redisDB) | ||
redisPassword := r.config.Redis.RedisPassword | ||
|
||
useSSL := strings.ToLower(r.config.Redis.RedisSsl) == "true" | ||
var tlsConfig *tls.Config | ||
|
||
if useSSL { | ||
caCertPath := r.config.Redis.RedisCaCert | ||
clientCertPath := r.config.Redis.RedisClientCert | ||
clientKeyPath := r.config.Redis.RedisClientKey | ||
|
||
var err error | ||
tlsConfig, err = utils.CreateTLSConfig(caCertPath, clientCertPath, clientKeyPath) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
r.client = redis.NewClient(&redis.Options{ | ||
Addr: redisAddress, | ||
Password: redisPassword, | ||
DB: redisDBInt, | ||
TLSConfig: tlsConfig, | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func (r *RedisAdapter) SubscribeToStream(ctx context.Context, streamName string, queue chan<- adapter.WebhookPayload) error { | ||
stream := streamName | ||
if stream == "" { | ||
stream = r.streamName | ||
} | ||
// Implement subscription logic here | ||
// Example: | ||
// for { | ||
// streams, err := r.client.XRead(&redis.XReadArgs{ | ||
// Streams: []string{stream, "0"}, | ||
// Count: 1, | ||
// Block: 0, | ||
// }).Result() | ||
// if err != nil { | ||
// return err | ||
// } | ||
// for _, message := range streams[0].Messages { | ||
// queue <- adapter.WebhookPayload{ID: message.ID, Payload: message.Values["data"].(string)} | ||
// } | ||
// } | ||
return nil | ||
} | ||
|
||
func (r *RedisAdapter) ProcessWebhooks(ctx context.Context, queue <-chan adapter.WebhookPayload) error { | ||
// Implement webhook processing logic here | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.