initial
This commit is contained in:
commit
53e0f4374d
22
LICENSE
Normal file
22
LICENSE
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
The MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2018 Darko Luketic. https://icod.de
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
106
README.md
Normal file
106
README.md
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
# What
|
||||||
|
|
||||||
|
Database changes notification library.
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.icod.de/dalu/notifier"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gopkg.in/olahol/melody.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
m := melody.New()
|
||||||
|
n := notifier.NewNotifier(m)
|
||||||
|
h := NewHandler(n)
|
||||||
|
|
||||||
|
r.GET("/", h.Index)
|
||||||
|
r.GET("/ws", func(cx *gin.Context) {
|
||||||
|
m.HandleRequest(cx.Writer, cx.Request)
|
||||||
|
})
|
||||||
|
r.GET("/acme/ins", h.InsertDB)
|
||||||
|
|
||||||
|
go n.Listen(func(notification *notifier.Notification) {
|
||||||
|
b, e := json.Marshal(notification)
|
||||||
|
if e != nil {
|
||||||
|
log.Println(e.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.Broadcast(b)
|
||||||
|
})
|
||||||
|
r.Run(":5000")
|
||||||
|
}
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
notifier *notifier.Notifier
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(n *notifier.Notifier) *Handler {
|
||||||
|
h := new(Handler)
|
||||||
|
h.notifier = n
|
||||||
|
return h
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) Index(cx *gin.Context) {
|
||||||
|
http.ServeFile(cx.Writer, cx.Request, "index.html")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) InsertDB(cx *gin.Context) {
|
||||||
|
// Do database insert stuff
|
||||||
|
h.notifier.Notify("acme", "INSERT", []byte(`{"id": 1, "name": acme}`))
|
||||||
|
cx.JSON(201, map[string]interface{}{"status": "ok"})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Melody example: chatting</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#chat {
|
||||||
|
text-align: left;
|
||||||
|
background: #f1f1f1;
|
||||||
|
width: 500px;
|
||||||
|
min-height: 300px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div align="center">
|
||||||
|
<h3>Chat</h3>
|
||||||
|
<pre id="chat"></pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
let url = "ws://localhost:5000/ws";
|
||||||
|
let ws = new WebSocket(url);
|
||||||
|
|
||||||
|
let chat = document.getElementById("chat");
|
||||||
|
|
||||||
|
let now = function () {
|
||||||
|
let iso = new Date().toISOString();
|
||||||
|
return iso.split("T")[1].split(".")[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = function (msg) {
|
||||||
|
let line = now() + " " + msg.data + "\n";
|
||||||
|
chat.innerText += line;
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
41
notifier.go
Normal file
41
notifier.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package notifier
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gopkg.in/olahol/melody.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Notifier struct {
|
||||||
|
channel chan *Notification
|
||||||
|
Melody *melody.Melody
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNotifier(m *melody.Melody) *Notifier {
|
||||||
|
n := new(Notifier)
|
||||||
|
n.channel = make(chan *Notification)
|
||||||
|
n.Melody = m
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Notifier) Listen(then func(notification *Notification)) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case noti := <-n.channel:
|
||||||
|
then(noti)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Notifier) Notify(collection, type_ string, data []byte) {
|
||||||
|
n.channel <- &Notification{
|
||||||
|
Collection: collection,
|
||||||
|
Type: type_,
|
||||||
|
Data:data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Notification struct {
|
||||||
|
Collection string `json:"collection"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Data []byte `json:"data,omitempty"`
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user