-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
201 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/nats-io/nats" | ||
) | ||
|
||
type Component struct { | ||
ID string `json:"_component_id"` | ||
Type string `json:"_component"` | ||
State string `json:"_state"` | ||
Action string `json:"_action"` | ||
Provider string `json:"_provider"` | ||
Name string `json:"name"` | ||
Error string `json:"error,omitempty"` | ||
Service string `json:"service,omitempty"` | ||
} | ||
|
||
func componentHandler(msg *nats.Msg) { | ||
var c Component | ||
if err := json.Unmarshal(msg.Data, &c); err != nil { | ||
panic(err) | ||
} | ||
|
||
id := c.getID() | ||
|
||
data, err := json.Marshal(c) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
publishEvent(id, data) | ||
} | ||
|
||
func (c *Component) getID() string { | ||
var pieces []string | ||
pieces = strings.Split(c.Service, "-") | ||
|
||
return pieces[len(pieces)-1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/r3labs/sse" | ||
) | ||
|
||
func publishEvent(id string, data []byte) { | ||
|
||
// id := svr.getID() | ||
|
||
// data, err := json.Marshal(svr) | ||
// if err != nil { | ||
// panic(err) | ||
// } | ||
|
||
//fmt.Printf("mydata = %+v\n", string(data)) | ||
|
||
// Create a new stream | ||
log.Println("Creating stream for", id) | ||
ss.CreateStream(id) | ||
|
||
ss.Publish(id, data) | ||
|
||
time.Sleep(10 * time.Millisecond) | ||
// Remove a new stream when the build completes | ||
log.Println("Closing stream for", id) | ||
go func(s *sse.Server) { | ||
ss.RemoveStream(id) | ||
}(ss) | ||
|
||
// publishMessage(notification.getServiceID(), &nm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/nats-io/nats" | ||
) | ||
|
||
type Service struct { | ||
ID string `json:"id"` | ||
Changes []Component `json:"changes"` | ||
} | ||
|
||
func serviceHandler(msg *nats.Msg) { | ||
var s Service | ||
if err := json.Unmarshal(msg.Data, &s); err != nil { | ||
panic(err) | ||
} | ||
|
||
id := s.getID() | ||
|
||
data, err := json.Marshal(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
publishEvent(id, data) | ||
} | ||
|
||
func (s *Service) getID() string { | ||
var pieces []string | ||
pieces = strings.Split(s.ID, "-") | ||
|
||
return pieces[len(pieces)-1] | ||
} |
Oops, something went wrong.