initial commit
This commit is contained in:
commit
35cf6f3559
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module github.com/hook-lord/confetti-trigger
|
||||
|
||||
go 1.23.1
|
51
main.go
Normal file
51
main.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var eventChan = make(chan string)
|
||||
|
||||
func SSEHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||
|
||||
clientChan := make(chan string)
|
||||
|
||||
go func() {
|
||||
for event := range clientChan {
|
||||
fmt.Fprintf(w, "data: %s\n\n", event)
|
||||
w.(http.Flusher).Flush()
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case event := <-eventChan:
|
||||
clientChan <- event
|
||||
case <-r.Context().Done():
|
||||
close(clientChan)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TriggerHandler(w http.ResponseWriter, r *http.Request) {
|
||||
eventChan <- "Confetti Triggered!"
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Confetti Triggered"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/events", SSEHandler)
|
||||
http.HandleFunc("/trigger", TriggerHandler)
|
||||
|
||||
log.Println("Server running on port 8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
Loading…
Reference in New Issue
Block a user