From 880b0fdc75570be20c9e73e8a481e759f0f0fc52 Mon Sep 17 00:00:00 2001 From: Jhonata Menezes Date: Tue, 30 May 2017 19:38:33 -0300 Subject: [PATCH] recebendo requisicoes de notificacao --- cache.go | 21 ++++++++++++++++++++- controller.go | 4 +--- main/kartola.go | 35 +++++++++++++++++++++++++++++++++++ notification/message.go | 2 +- notification/push.go | 33 ++++++++++++++++++++++++++------- 5 files changed, 83 insertions(+), 12 deletions(-) diff --git a/cache.go b/cache.go index e686cd6..885bcae 100755 --- a/cache.go +++ b/cache.go @@ -62,6 +62,26 @@ func UpdatePontuados() { func UpdatePartidas() { CachePartidas = make([]api.Partidas, 21) + + // se pegou todas rodadas anteriores, atualiza apenas a rodada atual + if CachePartidas[0].Rodada > 0 { + tmp := api.Partidas{} + tmp.Get(0) + + // atualiza o cache da rodada 0 e da rodada retornada + if tmp.Rodada > 0 { + CachePartidas[0] = tmp + CachePartidas[tmp.Rodada] = tmp + } else { + log.Println("rodada atual retornada como 0") + } + CachePartidas[0] = tmp + + SleepCacheSecond(10) + UpdatePartidas() + } + + for i:=0; i<=20; i++ { tmp := api.Partidas{} if i == 0 { @@ -71,7 +91,6 @@ func UpdatePartidas() { } tmp.Get(i) CachePartidas[i] = tmp - log.Println(i, CachePartidas[i].Rodada) } SleepCacheSecond(3600) diff --git a/controller.go b/controller.go index 33a108d..fd41682 100755 --- a/controller.go +++ b/controller.go @@ -6,8 +6,6 @@ import ( "github.com/pressly/chi" "strconv" "github.com/pressly/chi/render" - "github.com/SherClockHolmes/webpush-go" - "log" "github.com/jhonata-menezes/kartolafc-backend/notification" ) @@ -110,7 +108,7 @@ func GetPartida(response http.ResponseWriter, request *http.Request) { func AddNotificacao(response http.ResponseWriter, request *http.Request) { responseDefault(response) - client := webpush.Subscription{} + client := notification.Subscription{} render.DecodeJSON(request.Body, &client) notification.ChannelSubscribe <- client render.JSON(response, request, DefaultMessage{"ok", "Inscrito com sucesso"}) diff --git a/main/kartola.go b/main/kartola.go index feb59f3..0fc1050 100755 --- a/main/kartola.go +++ b/main/kartola.go @@ -11,6 +11,9 @@ import ( "io/ioutil" "encoding/json" "github.com/jhonata-menezes/kartolafc-backend/api" + "github.com/jhonata-menezes/kartolafc-backend/notification" + "time" + "github.com/goware/cors" ) func main() { @@ -26,6 +29,27 @@ func main() { } //------------------------------------------------------------------------------------- + // temporario para teste com web push + go notification.AddSubscribe(¬ification.ChannelSubscribe) + channelMessageNotification := make(chan notification.MessageNotification, 1000) + go notification.Notify(&channelMessageNotification) + go func (){ + for { + m := notification.MessageNotification{} + m.Body = "teste 001" + m.Badge = "http://images.terra.com/2015/05/20/corinthians.png" + m.Icon = "http://images.terra.com/2015/05/20/corinthians.png" + m.Link = "http://kartolafc.com.br/#/ligas" + m.Title = "Title Teste" + m.Vibrate = []int{200, 100, 200} + channelMessageNotification <- m + + log.Println("Enviando notificacao") + time.Sleep(30 * time.Second) + } + }() + // ---------------------------------- + router := chi.NewRouter() router.Use(middleware.DefaultCompress) router.Use(render.SetContentType(render.ContentTypeJSON)) @@ -34,6 +58,17 @@ func main() { router.Use(middleware.RealIP) router.Use(middleware.RequestID) + myCors := cors.New(cors.Options{ + // AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts + AllowedOrigins: []string{"*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: true, + MaxAge: 300, // Maximum value not ignored by any of major browsers + }) + router.Use(myCors.Handler) + kartolafc.BuildRoutes(router) log.Println("Bora Cumpade.") log.Println("listen", cmd.ServerBind) diff --git a/notification/message.go b/notification/message.go index 46b610a..7488bc7 100644 --- a/notification/message.go +++ b/notification/message.go @@ -5,7 +5,7 @@ type MessageNotification struct { Body string `json:"body"` Icon string `json:"icon"` Badge string `json:"badge"` - Vibrate string `json:"vibrate"` + Vibrate []int `json:"vibrate"` Link string `json:"link"` } diff --git a/notification/push.go b/notification/push.go index c463259..5f5f0c0 100644 --- a/notification/push.go +++ b/notification/push.go @@ -6,24 +6,39 @@ import ( "encoding/json" ) +// Keys are the base64 encoded values from PushSubscription.getKey() +type Keys struct { + Auth string `json:"auth"` + P256dh string `json:"p256dh"` +} + +// Subscription represents a PushSubscription object from the Push API +type Subscription struct { + Endpoint string `json:"endpoint"` + Keys Keys `json:"keys"` +} + const ( vapidPrivateKey = "W1HAZf9KEso6B9PgCM0xqF_d4KBFe88qKGu-KtAkuvA" ) -var usersKartolafc []webpush.Subscription +var usersKartolafc map[string]Subscription -var ChannelSubscribe = make(chan webpush.Subscription, 1000) +var ChannelSubscribe = make(chan Subscription, 1000) // adiciona os inscritos no array [temporario] -func addSubscribe(subscription *chan webpush.Subscription) { - usersKartolafc = make([]webpush.Subscription, 10000) +func AddSubscribe(subscription *chan Subscription) { + usersKartolafc = make(map[string]Subscription, 10000) for s := range *subscription { - usersKartolafc = append(usersKartolafc, s) + if _, v := usersKartolafc[s.Endpoint]; v { + continue + } + usersKartolafc[s.Endpoint] = s } } -func notify(channelNotifcation *chan MessageNotification,) { +func Notify(channelNotifcation *chan MessageNotification) { for m := range *channelNotifcation { // Send Notification @@ -33,7 +48,11 @@ func notify(channelNotifcation *chan MessageNotification,) { } for _, s := range usersKartolafc { - _, err := webpush.SendNotification(mByte, &s, &webpush.Options{ + adapter := webpush.Subscription{} + adapter.Endpoint = s.Endpoint + adapter.Keys.Auth = s.Keys.Auth + adapter.Keys.P256dh = s.Keys.P256dh + _, err := webpush.SendNotification(mByte, &adapter, &webpush.Options{ Subscriber: "mailto:jhonatamenezes10@gmail.com", TTL: 60, VAPIDPrivateKey: vapidPrivateKey,