Login technically works

This commit is contained in:
2022-03-04 01:56:15 +01:00
parent b5aecea322
commit 3aade31bd6
13 changed files with 769 additions and 130 deletions
+17
View File
@@ -0,0 +1,17 @@
package httputil
import (
"encoding/json"
"net/http"
)
type H map[string]interface{}
func WriteJSON(w http.ResponseWriter, data interface{}) error {
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(data)
}
func ReadJSON(r *http.Request, data interface{}) error {
return json.NewDecoder(r.Body).Decode(data)
}
+87 -23
View File
@@ -2,37 +2,101 @@ package httputil
import (
"fmt"
"log"
"net/http"
)
func HandleSSE(handler func(broadcast, single chan<- string)) http.Handler {
broadcast := make(chan string)
type SSEHandler struct {
clients map[chan string]bool
return http.HandlerFunc(func(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", "*")
Connected func(chan string)
Disconnected func(chan string)
}
client := make(chan string)
defer func() {
close(client)
}()
func (sse *SSEHandler) init() {
if sse.clients == nil {
sse.clients = make(map[chan string]bool)
}
}
go handler(broadcast, client)
func (sse *SSEHandler) Broadcast(message string) {
sse.init()
flusher, _ := w.(http.Flusher)
for {
select {
case message := <-broadcast:
fmt.Fprintf(w, "%s\n", message)
flusher.Flush()
case message := <-client:
fmt.Fprintf(w, "%s\n", message)
flusher.Flush()
case <-r.Context().Done():
for client := range sse.clients {
client <- message
}
}
func (sse *SSEHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sse.init()
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", "*")
client := make(chan string)
sse.clients[client] = true
log.Printf(`New connection`)
if sse.Connected != nil {
go sse.Connected(client)
}
defer func() {
log.Printf(`Connection closed`)
close(client)
delete(sse.clients, client)
if sse.Disconnected != nil {
go sse.Disconnected(client)
}
}()
flusher, _ := w.(http.Flusher)
for {
select {
case message, ok := <-client:
if !ok {
return
}
fmt.Fprintf(w, "data: %s\n\n", message)
flusher.Flush()
case <-r.Context().Done():
return
}
})
}
}
// func HandleSSE(handler func(broadcast, single chan<- string)) http.Handler {
// broadcast := make(chan string)
// return http.HandlerFunc(func(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", "*")
// client := make(chan string)
// defer func() {
// close(client)
// }()
// go handler(broadcast, client)
// flusher, _ := w.(http.Flusher)
// for {
// select {
// case message := <-broadcast:
// fmt.Fprintf(w, "data: %s\n\n", message)
// flusher.Flush()
// case message := <-client:
// fmt.Fprintf(w, "data: %s\n\n", message)
// flusher.Flush()
// case <-r.Context().Done():
// return
// }
// }
// })
// }
+12 -11
View File
@@ -1,6 +1,7 @@
package httputil_test
import (
"log"
"net/http"
"testing"
"time"
@@ -9,23 +10,23 @@ import (
)
func TestSSE(t *testing.T) {
var broadcastChannel chan<- string
sse := &httputil.SSEHandler{
Connected: func(client chan string) {
log.Printf(`New client connected callback`)
client <- "Messaggio 1 per questo client"
time.Sleep(1 * time.Second)
client <- "Messaggio 2 per questo client"
},
}
go func() {
for {
if broadcastChannel != nil {
broadcastChannel <- "Messaggio per tutti"
}
log.Printf(`Broadcasting message`)
sse.Broadcast("Messaggio per tutti")
time.Sleep(1 * time.Second)
}
}()
http.Handle("/sse", httputil.HandleSSE(func(broadcast, single chan<- string) {
broadcastChannel = broadcast
time.Sleep(1 * time.Second)
single <- "Messaggio 1 per questo client"
single <- "Messaggio 2 per questo client"
}))
http.Handle("/sse", sse)
http.ListenAndServe(":8000", nil)
}