Skip to content
This repository was archived by the owner on Aug 24, 2020. It is now read-only.

Commit

Permalink
recebendo requisicoes de notificacao
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonata-menezes committed May 30, 2017
1 parent 9b2a957 commit 880b0fd
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
21 changes: 20 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -71,7 +91,6 @@ func UpdatePartidas() {
}
tmp.Get(i)
CachePartidas[i] = tmp
log.Println(i, CachePartidas[i].Rodada)
}

SleepCacheSecond(3600)
Expand Down
4 changes: 1 addition & 3 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"})
Expand Down
35 changes: 35 additions & 0 deletions main/kartola.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -26,6 +29,27 @@ func main() {
}
//-------------------------------------------------------------------------------------

// temporario para teste com web push
go notification.AddSubscribe(&notification.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))
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion notification/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

33 changes: 26 additions & 7 deletions notification/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 880b0fd

Please sign in to comment.