Raggiunto primo stato presentabile

This commit is contained in:
2022-03-05 04:45:12 +01:00
parent b0732c9c4f
commit bbd6b9eb7e
19 changed files with 1326 additions and 468 deletions
+5 -2
View File
@@ -7,9 +7,12 @@ import (
type H map[string]interface{}
func WriteJSON(w http.ResponseWriter, data interface{}) error {
func WriteJSON(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(data)
err := json.NewEncoder(w).Encode(data)
if err != nil {
panic(err)
}
}
func ReadJSON(r *http.Request, data interface{}) error {
+78
View File
@@ -0,0 +1,78 @@
package serverevents
import (
"fmt"
"log"
"net/http"
)
type Config struct {
Connected func(chan string)
Disconnected func(chan string)
}
type Handler interface {
http.Handler
Broadcast(message string)
}
type handler struct {
clients map[chan string]bool
Connected func(chan string)
Disconnected func(chan string)
}
func New(config *Config) Handler {
return &handler{
make(map[chan string]bool),
config.Connected,
config.Disconnected,
}
}
func (sse *handler) Broadcast(message string) {
for client := range sse.clients {
client <- message
}
}
func (sse *handler) ServeHTTP(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)
sse.clients[client] = true
log.Printf(`New connection`)
if sse.Connected != nil {
sse.Connected(client)
}
defer func() {
log.Printf(`Connection closed`)
close(client)
delete(sse.clients, client)
if sse.Disconnected != nil {
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
}
}
}
+34
View File
@@ -0,0 +1,34 @@
package serverevents_test
import (
"log"
"net/http"
"testing"
"time"
"git.phc.dm.unipi.it/aziis98/posti-dm/server/httputil/serverevents"
)
func TestSSE(t *testing.T) {
sse := serverevents.New(&serverevents.Config{
Connected: func(client chan string) {
go func() {
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 {
log.Printf(`Broadcasting message`)
sse.Broadcast("Messaggio per tutti")
time.Sleep(1 * time.Second)
}
}()
http.Handle("/sse", sse)
http.ListenAndServe(":8000", nil)
}
-102
View File
@@ -1,102 +0,0 @@
package httputil
import (
"fmt"
"log"
"net/http"
)
type SSEHandler struct {
clients map[chan string]bool
Connected func(chan string)
Disconnected func(chan string)
}
func (sse *SSEHandler) init() {
if sse.clients == nil {
sse.clients = make(map[chan string]bool)
}
}
func (sse *SSEHandler) Broadcast(message string) {
sse.init()
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
// }
// }
// })
// }
-32
View File
@@ -1,32 +0,0 @@
package httputil_test
import (
"log"
"net/http"
"testing"
"time"
"git.phc.dm.unipi.it/aziis98/posti-dm/server/httputil"
)
func TestSSE(t *testing.T) {
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 {
log.Printf(`Broadcasting message`)
sse.Broadcast("Messaggio per tutti")
time.Sleep(1 * time.Second)
}
}()
http.Handle("/sse", sse)
http.ListenAndServe(":8000", nil)
}