Remove unused api and realtime
This commit is contained in:
@@ -1,89 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
// import (
|
||||
// "bytes"
|
||||
// "encoding/json"
|
||||
// "log"
|
||||
// "net/http"
|
||||
|
||||
"github.com/r3labs/sse"
|
||||
)
|
||||
// "github.com/r3labs/sse"
|
||||
// )
|
||||
|
||||
type RealtimeListener struct {
|
||||
Url string
|
||||
Collections []string
|
||||
Create chan PBEvent
|
||||
Update chan PBEvent
|
||||
Delete chan PBEvent
|
||||
client *sse.Client
|
||||
}
|
||||
// type RealtimeListener struct {
|
||||
// Url string
|
||||
// Collections []string
|
||||
// Create chan PBEvent
|
||||
// Update chan PBEvent
|
||||
// Delete chan PBEvent
|
||||
// client *sse.Client
|
||||
// }
|
||||
|
||||
type Subscription struct {
|
||||
ClientId string `json:"clientId"`
|
||||
Subscriptions []string `json:"subscriptions"`
|
||||
}
|
||||
// type Subscription struct {
|
||||
// ClientId string `json:"clientId"`
|
||||
// Subscriptions []string `json:"subscriptions"`
|
||||
// }
|
||||
|
||||
func (listener RealtimeListener) handlePbEvent(msg *sse.Event) {
|
||||
pbEvent := new(PBEvent)
|
||||
err := json.Unmarshal(msg.Data, &pbEvent)
|
||||
if err != nil {
|
||||
log.Printf("Error unmarshalling event: %v\n", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Received event: %++v", pbEvent)
|
||||
// func (listener RealtimeListener) handlePbEvent(msg *sse.Event) {
|
||||
// pbEvent := new(PBEvent)
|
||||
// err := json.Unmarshal(msg.Data, &pbEvent)
|
||||
// if err != nil {
|
||||
// log.Printf("Error unmarshalling event: %v\n", err)
|
||||
// return
|
||||
// }
|
||||
// log.Printf("Received event: %++v", pbEvent)
|
||||
|
||||
if pbEvent.ClientId != "" {
|
||||
listener.doSubscribe(pbEvent.ClientId)
|
||||
}
|
||||
// if pbEvent.ClientId != "" {
|
||||
// listener.doSubscribe(pbEvent.ClientId)
|
||||
// }
|
||||
|
||||
if pbEvent.Action != "" {
|
||||
go listener.shipEvent(*pbEvent)
|
||||
}
|
||||
}
|
||||
// if pbEvent.Action != "" {
|
||||
// go listener.shipEvent(*pbEvent)
|
||||
// }
|
||||
// }
|
||||
|
||||
func (listener RealtimeListener) shipEvent(event PBEvent) {
|
||||
switch event.Action {
|
||||
case "create":
|
||||
listener.Create <- event
|
||||
case "update":
|
||||
listener.Update <- event
|
||||
case "delete":
|
||||
listener.Delete <- event
|
||||
default:
|
||||
log.Printf("Unknown action: %v\n", event.Action)
|
||||
}
|
||||
}
|
||||
// func (listener RealtimeListener) shipEvent(event PBEvent) {
|
||||
// switch event.Action {
|
||||
// case "create":
|
||||
// listener.Create <- event
|
||||
// case "update":
|
||||
// listener.Update <- event
|
||||
// case "delete":
|
||||
// listener.Delete <- event
|
||||
// default:
|
||||
// log.Printf("Unknown action: %v\n", event.Action)
|
||||
// }
|
||||
// }
|
||||
|
||||
func (listener RealtimeListener) doSubscribe(clientId string) {
|
||||
subscription := Subscription{
|
||||
ClientId: clientId,
|
||||
Subscriptions: listener.Collections,
|
||||
}
|
||||
log.Printf("Subscribing client: %v to %++v", clientId, subscription)
|
||||
// func (listener RealtimeListener) doSubscribe(clientId string) {
|
||||
// subscription := Subscription{
|
||||
// ClientId: clientId,
|
||||
// Subscriptions: listener.Collections,
|
||||
// }
|
||||
// log.Printf("Subscribing client: %v to %++v", clientId, subscription)
|
||||
|
||||
body, err := json.Marshal(subscription)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling subscription body: %v\n", err)
|
||||
return
|
||||
}
|
||||
// body, err := json.Marshal(subscription)
|
||||
// if err != nil {
|
||||
// log.Printf("Error marshalling subscription body: %v\n", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
resp, err := http.Post(POCKETBASE_REALTIME, "application/json", bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
log.Printf("Error posting subscription: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// resp, err := http.Post(POCKETBASE_REALTIME, "application/json", bytes.NewBuffer(body))
|
||||
// if err != nil {
|
||||
// log.Printf("Error posting subscription: %v\n", err)
|
||||
// return
|
||||
// }
|
||||
// defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
log.Printf("Subscription request failed with status: %v\n", resp.Status)
|
||||
}
|
||||
}
|
||||
// if resp.StatusCode != http.StatusNoContent {
|
||||
// log.Printf("Subscription request failed with status: %v\n", resp.Status)
|
||||
// }
|
||||
// }
|
||||
|
||||
func (listener *RealtimeListener) initialize() {
|
||||
listener.Update = make(chan PBEvent, 32)
|
||||
listener.Create = make(chan PBEvent, 32)
|
||||
listener.Delete = make(chan PBEvent, 32)
|
||||
log.Print("Initialized")
|
||||
listener.client = sse.NewClient(listener.Url)
|
||||
go listener.client.Subscribe("", listener.handlePbEvent)
|
||||
}
|
||||
// func (listener *RealtimeListener) initialize() {
|
||||
// listener.Update = make(chan PBEvent, 32)
|
||||
// listener.Create = make(chan PBEvent, 32)
|
||||
// listener.Delete = make(chan PBEvent, 32)
|
||||
// log.Print("Initialized")
|
||||
// listener.client = sse.NewClient(listener.Url)
|
||||
// go listener.client.Subscribe("", listener.handlePbEvent)
|
||||
// }
|
||||
|
Reference in New Issue
Block a user