Skip to content

Commit

Permalink
Merge pull request #9 from zrougamed/feature/instant-notification
Browse files Browse the repository at this point in the history
Add Instant Notification Endpoint and Config Fixes ✉️⚙️
  • Loading branch information
zrougamed authored Dec 27, 2024
2 parents b98406e + d761ae0 commit 04a7a9f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ type ScheduledJob struct {
LastRun sql.NullTime `json:"last_run,omitempty"`
}

// InstantJob struct
type InstantJob struct {
NotificationType string `json:"notification_type"`
Recipient string `json:"recipient"`
Message Message `json:"message"`
}

type Notifier interface {
Name() string
Type() string
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"dynamic-notification-system/config"
"dynamic-notification-system/notifier"
"dynamic-notification-system/plugins"
"dynamic-notification-system/scheduler"
"fmt"
Expand All @@ -25,6 +26,9 @@ func main() {
log.Fatalf("Error loading plugins: %v", err)
}

// Pass the loaded notifiers to the notifier package
notifier.SetNotifiers(notifiers)

// Initialize Scheduler if enabled
if cfg.Scheduler {
fmt.Println("Starting scheduled jobs...")
Expand All @@ -39,12 +43,15 @@ func main() {

r := mux.NewRouter()
if cfg.Scheduler {
// Scheduled notification endpoints
r.HandleFunc("/schema/job", scheduler.GetJobSchema())
r.HandleFunc("/jobs", scheduler.HandlePostJob).Methods("POST")
r.HandleFunc("/jobs", scheduler.HandleGetJobs).Methods("GET")
} else {
fmt.Println("Scheduling endpoints are disabled.")
}
// Instant notification endpoint
r.HandleFunc("/notify", notifier.HandlePostJob).Methods("POST")

fmt.Println("Server listening on port 8080")
log.Fatal(http.ListenAndServe(":8080", r))
Expand Down
50 changes: 50 additions & 0 deletions notifier/notifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package notifier

import (
"dynamic-notification-system/config"
"encoding/json"
"fmt"
"log"
"net/http"
)

var notifiers []config.Notifier

// SetNotifiers initializes the notifiers
func SetNotifiers(n []config.Notifier) {
notifiers = n
}

func HandlePostJob(w http.ResponseWriter, r *http.Request) {
var job config.InstantJob

err := json.NewDecoder(r.Body).Decode(&job)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if err := validateJob(&job); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
jobCopy := job
for _, notifier := range notifiers {
if notifier.Type() == jobCopy.NotificationType {
fmt.Printf("Running job: %s \n", jobCopy.Recipient)
err := notifier.Notify(&jobCopy.Message)
if err != nil {
log.Printf("Error sending notification via %s: %v", notifier.Name(), err)
}
}
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(job)
}

func validateJob(job *config.InstantJob) error { // add instant notification
if job.NotificationType == "" {
return fmt.Errorf("NotificationType is required")
}
return nil
}

0 comments on commit 04a7a9f

Please sign in to comment.