All checks were successful
Build Pogdark API / Build Pogdark API (pull_request) Successful in 37s
386 lines
9.9 KiB
Go
386 lines
9.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/gorilla/mux"
|
|
"github.com/gorilla/websocket"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
var (
|
|
upgrader = websocket.Upgrader{
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
return true
|
|
},
|
|
}
|
|
//redisClient *redis.Client
|
|
//ctx = context.Background()
|
|
)
|
|
|
|
type Message struct {
|
|
Id string `json:"Id"`
|
|
Name string `json:"Name"`
|
|
Image string `json:"Image"`
|
|
Status string `json:"Status"`
|
|
Timestamp string `json:"Timestamp"`
|
|
}
|
|
|
|
type Config struct {
|
|
Redis struct {
|
|
Host string `yaml:"host"`
|
|
Port string `yaml:"port"`
|
|
}
|
|
}
|
|
|
|
type App struct {
|
|
redisClient *redis.Client
|
|
sseClients map[chan string]bool
|
|
sseChannel chan string
|
|
mu sync.Mutex
|
|
ctx context.Context
|
|
flusher http.Flusher
|
|
ticker *time.Ticker
|
|
}
|
|
|
|
func getConfig(configPath string) (*Config, error) {
|
|
// Create config structure
|
|
config := &Config{}
|
|
|
|
// Open config file
|
|
file, err := os.Open(configPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func(file *os.File) {
|
|
err := file.Close()
|
|
if err != nil {
|
|
|
|
}
|
|
}(file)
|
|
|
|
// Init new YAML decode
|
|
d := yaml.NewDecoder(file)
|
|
|
|
// Start YAML decoding from file
|
|
if err := d.Decode(&config); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return config, nil
|
|
}
|
|
|
|
func loadConfig() *Config {
|
|
cfg, err := getConfig("config.yaml")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
func initRedis(cfg Config, ctx context.Context) *redis.Client {
|
|
redisClient := redis.NewClient(&redis.Options{
|
|
Addr: cfg.Redis.Host + ":" + cfg.Redis.Port,
|
|
})
|
|
_, err := redisClient.Do(context.Background(), "CONFIG", "SET", "notify-keyspace-events", "KEA").Result()
|
|
if err != nil {
|
|
fmt.Printf("unable to set keyspace events %v", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
redisClient.ConfigSet(ctx, "notify-keyspace-events", "Ex")
|
|
return redisClient
|
|
}
|
|
|
|
func newApp(cfg Config) *App {
|
|
return &App{
|
|
sseClients: make(map[chan string]bool),
|
|
sseChannel: make(chan string),
|
|
redisClient: initRedis(cfg, context.Background()),
|
|
ctx: context.Background(),
|
|
ticker: time.NewTicker(5 * time.Second),
|
|
}
|
|
}
|
|
|
|
func (app *App) handleSSE(w http.ResponseWriter, r *http.Request) {
|
|
// Create a new SSE client channel
|
|
clientChan := make(chan string)
|
|
log.Printf("Clientchan: %v %v", clientChan, len(app.sseClients))
|
|
// Register the client channel
|
|
app.mu.Lock()
|
|
app.sseClients[clientChan] = true
|
|
app.mu.Unlock()
|
|
|
|
// Send headers for SSE
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
// Stream messages to the client
|
|
for msg := range clientChan {
|
|
_, err := fmt.Fprintf(w, "data: %s\n\n", msg)
|
|
log.Printf("SSE: %v", msg)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if flusher, ok := w.(http.Flusher); ok {
|
|
app.flusher = flusher
|
|
app.flusher.Flush()
|
|
}
|
|
}
|
|
|
|
// Clean up when done
|
|
app.mu.Lock()
|
|
log.Println("Deleting %v", clientChan)
|
|
delete(app.sseClients, clientChan)
|
|
close(clientChan)
|
|
app.mu.Unlock()
|
|
}
|
|
|
|
func (app *App) broadcastSSE() {
|
|
for msg := range app.sseChannel {
|
|
app.mu.Lock()
|
|
for clientChan := range app.sseClients {
|
|
clientChan <- msg
|
|
}
|
|
app.mu.Unlock()
|
|
}
|
|
}
|
|
|
|
func broadcastRemovedRecords(app *App, message Message) {
|
|
var msgJSON, err = json.Marshal(message)
|
|
if err != nil {
|
|
log.Println("Error marshalling json:", err)
|
|
return
|
|
}
|
|
fmt.Printf("Broadcasting removal: %s,%s,%s,%s to %d clients\n", message.Id, message.Status, message.Name, message.Timestamp, len(app.sseClients))
|
|
app.sseChannel <- string(msgJSON)
|
|
}
|
|
|
|
func broadcastExpiredRecords(app *App, expired string) {
|
|
var message = Message{Id: expired, Name: "", Image: "", Status: "removed", Timestamp: ""}
|
|
var msgJSON, err = json.Marshal(message)
|
|
if err != nil {
|
|
log.Println("Error marshalling json:", err)
|
|
return
|
|
}
|
|
fmt.Printf("Broadcasting expiration: %s to %s clients\n", string(msgJSON), len(app.sseClients))
|
|
app.sseChannel <- string(msgJSON)
|
|
}
|
|
|
|
func (app *App) listenForExpirationEvents() {
|
|
ps := app.redisClient.PSubscribe(app.ctx, "__keyevent@0__:expired")
|
|
defer func(ps *redis.PubSub) {
|
|
err := ps.Close()
|
|
if err != nil {
|
|
|
|
}
|
|
}(ps)
|
|
|
|
for msg := range ps.Channel() {
|
|
//Print expired payload ID
|
|
fmt.Println(msg.Payload)
|
|
// Broadcast expiration event
|
|
broadcastExpiredRecords(app, msg.Payload)
|
|
fmt.Printf("Done Broadcasting Expiration\n")
|
|
}
|
|
}
|
|
|
|
func (app *App) fetchAllRecords() (map[string]string, error) {
|
|
allKeys, err := app.redisClient.Keys(app.ctx, "*").Result()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not fetch keys: %v", err)
|
|
}
|
|
|
|
records := make(map[string]string)
|
|
for _, key := range allKeys {
|
|
value, err := app.redisClient.Get(app.ctx, key).Result()
|
|
if errors.Is(err, redis.Nil) {
|
|
continue // skip if key does not exist
|
|
} else if err != nil {
|
|
return nil, fmt.Errorf("could not fetch value for key %s: %v", key, err)
|
|
}
|
|
records[key] = value
|
|
}
|
|
|
|
return records, nil
|
|
}
|
|
|
|
func getStatus(w http.ResponseWriter, r *http.Request, app *App) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
fmt.Println("Checking Status")
|
|
var request struct {
|
|
Id string `json:"Id"`
|
|
}
|
|
err := json.NewDecoder(r.Body).Decode(&request)
|
|
if err != nil {
|
|
fmt.Println("Invalid JSON format")
|
|
fmt.Println(r.Body)
|
|
http.Error(w, "Invalid JSON format", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if request.Id == "" {
|
|
fmt.Println("Missing or empty Id field")
|
|
http.Error(w, "Missing or empty Id field", http.StatusBadRequest)
|
|
return
|
|
}
|
|
fmt.Println(request.Id)
|
|
value, err := app.redisClient.Get(app.ctx, request.Id).Result()
|
|
if errors.Is(err, redis.Nil) {
|
|
message := Message{Id: request.Id, Status: "none", Timestamp: time.Now().Format(time.RFC3339)}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
err := json.NewEncoder(w).Encode(message)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return
|
|
} else if err != nil {
|
|
log.Printf("Redis error: %v", err)
|
|
http.Error(w, "Error connecting to Redis", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var message Message
|
|
err = json.Unmarshal([]byte(value), &message)
|
|
if err != nil {
|
|
log.Printf("Failed to decode Redis data for Id: %s - %v", request.Id, err)
|
|
http.Error(w, "Failed to parse data", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
err = json.NewEncoder(w).Encode(message)
|
|
if err != nil {
|
|
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func setStatus(w http.ResponseWriter, r *http.Request, app *App) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var message Message
|
|
err := json.NewDecoder(r.Body).Decode(&message)
|
|
if err != nil || message.Id == "" || message.Status == "" {
|
|
http.Error(w, "Invalid request format", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if message.Status == "none" {
|
|
if err := app.redisClient.Del(app.ctx, message.Id).Err(); err != nil {
|
|
log.Printf("Error deleting key from Redis: %v", err)
|
|
http.Error(w, "Failed to delete key from Redis", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
broadcastRemovedRecords(app, message)
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
msgJSON, err := json.Marshal(message)
|
|
if err != nil {
|
|
http.Error(w, "Failed to encode message", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
timeout := 20 * time.Second
|
|
err = app.redisClient.Set(app.ctx, message.Id, msgJSON, timeout).Err()
|
|
if err != nil {
|
|
log.Printf("Failed to set key in Redis: %v", err)
|
|
http.Error(w, "Failed to store data", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
app.sseChannel <- string(msgJSON)
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func updateMessages(w http.ResponseWriter, r *http.Request, app *App) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Only GET method is allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
allRecords, err := app.fetchAllRecords()
|
|
if err != nil {
|
|
log.Printf("Error fetching records: %v", err)
|
|
http.Error(w, "Failed to fetch records", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var messages []Message
|
|
for _, record := range allRecords {
|
|
var message Message
|
|
err := json.Unmarshal([]byte(record), &message)
|
|
if err != nil {
|
|
log.Printf("Error unmarshalling record: %v", err)
|
|
http.Error(w, "Failed to parse records", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
messages = append(messages, message)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
err = json.NewEncoder(w).Encode(messages)
|
|
if err != nil {
|
|
log.Printf("Error encoding response: %v", err)
|
|
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func enableCORS(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // Allow all origins
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
|
|
|
// Allow preflight requests for the OPTIONS method
|
|
if r.Method == http.MethodOptions {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func main() {
|
|
app := newApp(*loadConfig())
|
|
router := mux.NewRouter()
|
|
|
|
// Register routes on the mux router
|
|
router.HandleFunc("/events", app.handleSSE).Methods("GET")
|
|
router.HandleFunc("/getStatus", func(w http.ResponseWriter, r *http.Request) {
|
|
getStatus(w, r, app)
|
|
}).Methods("POST")
|
|
router.HandleFunc("/setStatus", func(w http.ResponseWriter, r *http.Request) {
|
|
setStatus(w, r, app)
|
|
}).Methods("POST")
|
|
router.HandleFunc("/updateMessages", func(w http.ResponseWriter, r *http.Request) {
|
|
updateMessages(w, r, app)
|
|
}).Methods("GET")
|
|
|
|
// Start server and other necessary goroutines
|
|
go app.listenForExpirationEvents()
|
|
go app.broadcastSSE()
|
|
corsRouter := enableCORS(router)
|
|
// Pass the mux router to ListenAndServe
|
|
fmt.Println("Server started on :8080")
|
|
err := http.ListenAndServe(":8080", corsRouter)
|
|
if err != nil {
|
|
fmt.Printf("Server error: %v\n", err)
|
|
}
|
|
}
|