Remove some unused trash
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
package main
|
||||
|
||||
// import (
|
||||
// "bytes"
|
||||
// "context"
|
||||
// "encoding/json"
|
||||
// "fmt"
|
||||
// "io"
|
||||
// "log"
|
||||
// "net/http"
|
||||
// )
|
||||
|
||||
// type APIError struct {
|
||||
// Code int `json:"code"`
|
||||
// Message string `json:"message"`
|
||||
// Data APIErrorData `json:"data"`
|
||||
// }
|
||||
|
||||
// type APIErrorData struct {
|
||||
// Link APIErrorLink `json:"link"`
|
||||
// }
|
||||
|
||||
// type APIErrorLink struct {
|
||||
// Code string `json:"code"`
|
||||
// Message string `json:"message"`
|
||||
// }
|
||||
|
||||
// func SetDownloaded(item PBEvent) (err error) {
|
||||
// req, err := http.NewRequestWithContext(context.Background(), "PATCH", FULL_URL+"/"+item.Record.Id, nil)
|
||||
// if err != nil {
|
||||
// log.Printf("Error creating PATCH request: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// partialItem := new(PBEvent)
|
||||
// partialItem.Record = item.Record
|
||||
// partialItem.Record.Downloaded = true
|
||||
|
||||
// body, err := json.Marshal(partialItem.Record)
|
||||
// if err != nil {
|
||||
// log.Printf("Error marshalling subscription body: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
// req.Body = io.NopCloser(bytes.NewReader(body))
|
||||
|
||||
// client := http.Client{}
|
||||
// res, err := client.Do(req)
|
||||
// if err != nil {
|
||||
// log.Printf("Error sending PATCH request: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
// defer res.Body.Close()
|
||||
|
||||
// if res.StatusCode != http.StatusOK {
|
||||
// log.Printf("Non-OK HTTP status: %d", res.StatusCode)
|
||||
|
||||
// body, err = io.ReadAll(res.Body)
|
||||
// if err != nil {
|
||||
// log.Printf("Error reading response body: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
// var data APIError
|
||||
// err = json.Unmarshal(body, &data)
|
||||
// if err != nil {
|
||||
// log.Printf("Error unmarshaling JSON: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
|
||||
// log.Printf("API error: %++v", data)
|
||||
// return fmt.Errorf("Non-OK HTTP status, err: %++v", data)
|
||||
// }
|
||||
|
||||
// return nil
|
||||
// }
|
@@ -1,51 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func NotifyDiscordErrorless(message string) {
|
||||
err := NotifyDiscord(message)
|
||||
if err != nil {
|
||||
log.Printf("Error notifying discord: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func NotifyDiscord(message string) error {
|
||||
webhookURL := os.Getenv("YTDL_DISCORD_WEBHOOK_URL")
|
||||
if webhookURL == "" {
|
||||
return fmt.Errorf("error notifying discord: webhook URL is not set in environment variables")
|
||||
}
|
||||
|
||||
jsonData := map[string]string{"content": message}
|
||||
jsonBytes, err := json.Marshal(jsonData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error notifying discord: error marshalling JSON: %v", err)
|
||||
}
|
||||
req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(jsonBytes))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error notifying discord: error creating request: %v", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error notifying discord: error sending request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error notifying discord: error reading response body: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Response from Discord: %s", string(body))
|
||||
return nil
|
||||
}
|
@@ -1,89 +0,0 @@
|
||||
package main
|
||||
|
||||
// import (
|
||||
// "bytes"
|
||||
// "encoding/json"
|
||||
// "log"
|
||||
// "net/http"
|
||||
|
||||
// "github.com/r3labs/sse"
|
||||
// )
|
||||
|
||||
// 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"`
|
||||
// }
|
||||
|
||||
// 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.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) 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
|
||||
// }
|
||||
|
||||
// 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)
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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)
|
||||
// }
|
@@ -1,143 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
const TIMEOUT = 6
|
||||
const IDLE_TIMEOUT = TIMEOUT * time.Second
|
||||
const PING_INTERVAL = (TIMEOUT / 2) * time.Second
|
||||
|
||||
type WSConnection struct {
|
||||
alive bool
|
||||
url string
|
||||
conn *websocket.Conn
|
||||
errChan chan error
|
||||
writeLock sync.Mutex
|
||||
ReadChan chan string
|
||||
WriteChan chan string
|
||||
Dead chan error
|
||||
}
|
||||
|
||||
func (ws *WSConnection) messageReader() {
|
||||
log.Printf("Starting reader")
|
||||
for {
|
||||
if !ws.alive {
|
||||
break
|
||||
}
|
||||
|
||||
_, message, err := ws.conn.ReadMessage()
|
||||
ws.conn.SetReadDeadline(time.Now().Add(IDLE_TIMEOUT))
|
||||
if err != nil {
|
||||
ws.errChan <- err
|
||||
break
|
||||
}
|
||||
|
||||
log.Printf("Received: %s, %d in output channel", message, len(ws.ReadChan))
|
||||
ws.ReadChan <- string(message)
|
||||
}
|
||||
log.Printf("Reader done")
|
||||
}
|
||||
|
||||
func (ws *WSConnection) messageSender() {
|
||||
log.Printf("Starting sender")
|
||||
for {
|
||||
msg, ok := <-ws.WriteChan
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
ws.doSend(msg)
|
||||
}
|
||||
log.Printf("Sender done")
|
||||
}
|
||||
|
||||
func (ws *WSConnection) doSend(msg string) {
|
||||
ws.writeLock.Lock()
|
||||
defer ws.writeLock.Unlock()
|
||||
|
||||
ws.conn.SetWriteDeadline(time.Now().Add(IDLE_TIMEOUT))
|
||||
log.Printf("Sending: %s, %d in input channel", msg, len(ws.WriteChan))
|
||||
err := ws.conn.WriteMessage(websocket.TextMessage, []byte(msg))
|
||||
if err != nil {
|
||||
log.Printf("Error during message writing: %v", err)
|
||||
ws.errChan <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *WSConnection) pinger() {
|
||||
log.Printf("Starting pinger, sleeping for %v", PING_INTERVAL)
|
||||
for {
|
||||
if !ws.alive {
|
||||
break
|
||||
}
|
||||
ws.doPing()
|
||||
time.Sleep(PING_INTERVAL)
|
||||
}
|
||||
log.Printf("Pinger done")
|
||||
}
|
||||
|
||||
func (ws *WSConnection) doPing() {
|
||||
ws.writeLock.Lock()
|
||||
defer ws.writeLock.Unlock()
|
||||
|
||||
// log.Printf("Ping")
|
||||
err := ws.conn.WriteMessage(websocket.PingMessage, nil)
|
||||
if err != nil {
|
||||
log.Println("Error during ping:", err)
|
||||
ws.errChan <- err
|
||||
return
|
||||
}
|
||||
ws.conn.SetWriteDeadline(time.Now().Add(IDLE_TIMEOUT))
|
||||
// log.Printf("Ping OK")
|
||||
}
|
||||
|
||||
func (ws *WSConnection) handleError() {
|
||||
for {
|
||||
err := <-ws.errChan
|
||||
log.Printf("Client error: %+v", err)
|
||||
ws.alive = false
|
||||
ws.conn.Close()
|
||||
close(ws.ReadChan)
|
||||
close(ws.WriteChan)
|
||||
close(ws.errChan)
|
||||
ws.Dead <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *WSConnection) Open() {
|
||||
log.Printf("Connecting to %s", ws.url)
|
||||
ws.Dead = make(chan error, 1)
|
||||
|
||||
conn, _, err := websocket.DefaultDialer.Dial(ws.url, nil)
|
||||
if err != nil {
|
||||
log.Println("Error during connection:", err)
|
||||
ws.Dead <- err
|
||||
return
|
||||
}
|
||||
log.Printf("Connected")
|
||||
ws.conn = conn
|
||||
ws.alive = true
|
||||
|
||||
ws.errChan = make(chan error, 1)
|
||||
ws.ReadChan = make(chan string, 128)
|
||||
ws.WriteChan = make(chan string, 128)
|
||||
|
||||
ws.conn.SetReadDeadline(time.Now().Add(IDLE_TIMEOUT))
|
||||
ws.conn.SetWriteDeadline(time.Now().Add(IDLE_TIMEOUT))
|
||||
ws.conn.SetPongHandler(func(string) error {
|
||||
// log.Println("Pong")
|
||||
ws.conn.SetReadDeadline(time.Now().Add(IDLE_TIMEOUT))
|
||||
return nil
|
||||
})
|
||||
|
||||
go ws.handleError()
|
||||
go ws.messageReader()
|
||||
go ws.messageSender()
|
||||
go ws.pinger()
|
||||
}
|
Reference in New Issue
Block a user