62 lines
1.2 KiB
Go
Raw Normal View History

2018-11-06 20:40:03 +00:00
package sse
import (
"net/http"
"github.com/animenotifier/notify.moe/components/css"
"github.com/animenotifier/notify.moe/components/js"
2018-11-06 20:40:03 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/utils"
)
var (
scriptsETag = aero.ETagString(js.Bundle())
stylesETag = aero.ETagString(css.Bundle())
)
2018-11-06 20:40:03 +00:00
// Events streams server events to the client.
func Events(ctx *aero.Context) string {
user := utils.GetUser(ctx)
if user == nil {
return ctx.Error(http.StatusUnauthorized, "Not logged in")
}
stream := aero.NewEventStream()
user.AddEventStream(stream)
2018-11-06 20:40:03 +00:00
go func() {
defer user.RemoveEventStream(stream)
2018-11-06 20:40:03 +00:00
2019-03-31 07:46:44 +00:00
// Send the ETag for the scripts
stream.Events <- &aero.Event{
Name: "etag",
Data: struct {
URL string `json:"url"`
ETag string `json:"etag"`
}{
URL: "/scripts",
ETag: scriptsETag,
},
}
2019-03-31 07:46:44 +00:00
// Send the ETag for the styles
stream.Events <- &aero.Event{
Name: "etag",
Data: struct {
URL string `json:"url"`
ETag string `json:"etag"`
}{
URL: "/styles",
ETag: stylesETag,
},
}
2019-03-31 07:46:44 +00:00
// Wait until the user closes the tab or disconnects
<-stream.Closed
2018-11-06 20:40:03 +00:00
}()
return ctx.EventStream(stream)
2018-11-06 20:40:03 +00:00
}