forked from mailhog/MailHog-Server
-
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.
New emails are broadcast through both the API v1 event stream and API v2 websocket.
- Loading branch information
Showing
7 changed files
with
210 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package api | ||
|
||
import ( | ||
gohttp "net/http" | ||
|
||
"github.com/gorilla/pat" | ||
"github.com/mailhog/MailHog-Server/config" | ||
) | ||
|
||
func CreateAPI(conf *config.Config, r gohttp.Handler) { | ||
apiv1 := createAPIv1(conf, r.(*pat.Router)) | ||
apiv2 := createAPIv2(conf, r.(*pat.Router)) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case msg := <-conf.MessageChan: | ||
apiv1.messageChan <- msg | ||
apiv2.messageChan <- msg | ||
} | ||
} | ||
}() | ||
} |
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,73 @@ | ||
package websockets | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
const ( | ||
// Time allowed to write a message to the peer. | ||
writeWait = 10 * time.Second | ||
// Time allowed to read the next pong message from the peer. | ||
pongWait = 60 * time.Second | ||
// Send pings to peer with this period. Must be less than pongWait. | ||
pingPeriod = (pongWait * 9) / 10 | ||
// Maximum message size allowed from peer. | ||
maxMessageSize = 256 | ||
) | ||
|
||
type connection struct { | ||
hub *Hub | ||
ws *websocket.Conn | ||
send chan interface{} | ||
} | ||
|
||
func (c *connection) readLoop() { | ||
defer func() { | ||
c.hub.unregisterChan <- c | ||
c.ws.Close() | ||
}() | ||
c.ws.SetReadLimit(256) | ||
c.ws.SetReadDeadline(time.Now().Add(pongWait)) | ||
c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil }) | ||
for { | ||
if _, _, err := c.ws.NextReader(); err != nil { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (c *connection) writeLoop() { | ||
ticker := time.NewTicker(pingPeriod) | ||
defer func() { | ||
ticker.Stop() | ||
c.ws.Close() | ||
}() | ||
for { | ||
select { | ||
case message, ok := <-c.send: | ||
if !ok { | ||
c.writeControl(websocket.CloseMessage) | ||
return | ||
} | ||
if err := c.writeJSON(message); err != nil { | ||
return | ||
} | ||
case <-ticker.C: | ||
if err := c.writeControl(websocket.PingMessage); err != nil { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *connection) writeJSON(message interface{}) error { | ||
c.ws.SetWriteDeadline(time.Now().Add(writeWait)) | ||
return c.ws.WriteJSON(message) | ||
} | ||
|
||
func (c *connection) writeControl(messageType int) error { | ||
c.ws.SetWriteDeadline(time.Now().Add(writeWait)) | ||
return c.ws.WriteMessage(messageType, []byte{}) | ||
} |
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,73 @@ | ||
package websockets | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/ian-kent/go-log/log" | ||
) | ||
|
||
type Hub struct { | ||
upgrader websocket.Upgrader | ||
connections map[*connection]bool | ||
messages chan interface{} | ||
registerChan chan *connection | ||
unregisterChan chan *connection | ||
} | ||
|
||
func NewHub() *Hub { | ||
hub := &Hub{ | ||
upgrader: websocket.Upgrader{ | ||
ReadBufferSize: 256, | ||
WriteBufferSize: 4096, | ||
}, | ||
connections: make(map[*connection]bool), | ||
messages: make(chan interface{}), | ||
registerChan: make(chan *connection), | ||
unregisterChan: make(chan *connection), | ||
} | ||
go hub.run() | ||
return hub | ||
} | ||
|
||
func (h *Hub) run() { | ||
for { | ||
select { | ||
case c := <-h.registerChan: | ||
h.connections[c] = true | ||
case c := <-h.unregisterChan: | ||
h.unregister(c) | ||
case m := <-h.messages: | ||
for c := range h.connections { | ||
select { | ||
case c.send <- m: | ||
default: | ||
h.unregister(c) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (h *Hub) unregister(c *connection) { | ||
if _, ok := h.connections[c]; ok { | ||
close(c.send) | ||
delete(h.connections, c) | ||
} | ||
} | ||
|
||
func (h *Hub) Serve(w http.ResponseWriter, r *http.Request) { | ||
ws, err := h.upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
c := &connection{hub: h, ws: ws, send: make(chan interface{}, 256)} | ||
h.registerChan <- c | ||
go c.writeLoop() | ||
go c.readLoop() | ||
} | ||
|
||
func (h *Hub) Broadcast(data interface{}) { | ||
h.messages <- data | ||
} |