0

Decouple chat from core and add chat rest api (#25)

* Decouple the chat package from the core

* Add rest api endpoints for the chat aspect
This commit is contained in:
Bradley Hilton 2020-06-23 15:11:01 -05:00 committed by GitHub
parent af1e2c5dd0
commit abb2f363af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 244 additions and 78 deletions

38
controllers/chat.go Normal file
View File

@ -0,0 +1,38 @@
package controllers
import (
"encoding/json"
"net/http"
"github.com/gabek/owncast/core"
"github.com/gabek/owncast/models"
"github.com/gabek/owncast/router/middleware"
)
//GetChatMessages gets all of the chat messages
func GetChatMessages(w http.ResponseWriter, r *http.Request) {
middleware.EnableCors(&w)
switch r.Method {
case http.MethodGet:
messages := core.GetAllChatMessages()
json.NewEncoder(w).Encode(messages)
case http.MethodPost:
var message models.ChatMessage
if err := json.NewDecoder(r.Body).Decode(&message); err != nil {
internalErrorHandler(w, err)
return
}
if err := core.SendMessageToChat(message); err != nil {
badRequestHandler(w, err)
return
}
json.NewEncoder(w).Encode(j{"success": true})
default:
w.WriteHeader(http.StatusNotImplemented)
json.NewEncoder(w).Encode(j{"error": "method not implemented (PRs are accepted)"})
}
}

View File

@ -0,0 +1,26 @@
package controllers
import (
"encoding/json"
"net/http"
)
type j map[string]interface{}
func internalErrorHandler(w http.ResponseWriter, err error) {
if err == nil {
return
}
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(j{"error": err.Error()})
}
func badRequestHandler(w http.ResponseWriter, err error) {
if err == nil {
return
}
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(j{"error": err.Error()})
}

80
core/chat/chat.go Normal file
View File

@ -0,0 +1,80 @@
package chat
import (
"errors"
"time"
"github.com/gabek/owncast/models"
)
//Setup sets up the chat server
func Setup(listener models.ChatListener) {
messages := []models.ChatMessage{}
clients := make(map[string]*Client)
addCh := make(chan *Client)
delCh := make(chan *Client)
sendAllCh := make(chan models.ChatMessage)
pingCh := make(chan models.PingMessage)
doneCh := make(chan bool)
errCh := make(chan error)
// Demo messages only. Remove me eventually!!!
messages = append(messages, models.ChatMessage{"", "Tom Nook", "I'll be there with Bells on! Ho ho!", "https://gamepedia.cursecdn.com/animalcrossingpocketcamp_gamepedia_en/thumb/4/4f/Timmy_Icon.png/120px-Timmy_Icon.png?version=87b38d7d6130411d113486c2db151385", "demo-message-1", "ChatMessage"})
messages = append(messages, models.ChatMessage{"", "Redd", "Fool me once, shame on you. Fool me twice, stop foolin' me.", "https://vignette.wikia.nocookie.net/animalcrossing/images/3/3d/Redd2.gif/revision/latest?cb=20100710004252", "demo-message-2", "ChatMessage"})
messages = append(messages, models.ChatMessage{"", "Kevin", "You just caught me before I was about to go work out weeweewee!", "https://vignette.wikia.nocookie.net/animalcrossing/images/2/20/NH-Kevin_poster.png/revision/latest/scale-to-width-down/100?cb=20200410185817", "demo-message-3", "ChatMessage"})
messages = append(messages, models.ChatMessage{"", "Isabelle", " Isabelle is the mayor's highly capable secretary. She can be forgetful sometimes, but you can always count on her for information about the town. She wears her hair up in a bun that makes her look like a shih tzu. Mostly because she is one! She also has a twin brother named Digby.", "https://dodo.ac/np/images/thumb/7/7b/IsabelleTrophyWiiU.png/200px-IsabelleTrophyWiiU.png", "demo-message-4", "ChatMessage"})
messages = append(messages, models.ChatMessage{"", "Judy", "myohmy, I'm dancing my dreams away.", "https://vignette.wikia.nocookie.net/animalcrossing/images/5/50/NH-Judy_poster.png/revision/latest/scale-to-width-down/100?cb=20200522063219", "demo-message-5", "ChatMessage"})
messages = append(messages, models.ChatMessage{"", "Blathers", "Blathers is an owl with brown feathers. His face is white and he has a yellow beak. His arms are wing shaped and he has yellow talons. His eyes are very big with small black irises. He also has big pink cheek circles on his cheeks. His belly appears to be checkered in diamonds with light brown and white squares, similar to an argyle vest, which is traditionally associated with academia. His green bowtie further alludes to his academic nature.", "https://vignette.wikia.nocookie.net/animalcrossing/images/b/b3/NH-character-Blathers.png/revision/latest?cb=20200229053519", "demo-message-6", "ChatMessage"})
_server = &server{
messages,
clients,
"/entry", //hardcoded due to the UI requiring this and it is not configurable
listener,
addCh,
delCh,
sendAllCh,
pingCh,
doneCh,
errCh,
}
}
//Start starts the chat server
func Start() error {
if _server == nil {
return errors.New("chat server is nil")
}
ticker := time.NewTicker(30 * time.Second)
go func() {
for {
select {
case <-ticker.C:
_server.ping()
}
}
}()
_server.Listen()
return errors.New("chat server failed to start")
}
//SendMessage sends a message to all
func SendMessage(message models.ChatMessage) {
if _server == nil {
return
}
_server.SendToAll(message)
}
//GetMessages gets all of the messages
func GetMessages() []models.ChatMessage {
if _server == nil {
return []models.ChatMessage{}
}
return _server.Messages
}

View File

@ -21,7 +21,6 @@ type Client struct {
id string
ws *websocket.Conn
server *Server
ch chan models.ChatMessage
pingch chan models.PingMessage
@ -29,21 +28,17 @@ type Client struct {
}
//NewClient creates a new chat client
func NewClient(ws *websocket.Conn, server *Server) *Client {
func NewClient(ws *websocket.Conn) *Client {
if ws == nil {
log.Panicln("ws cannot be nil")
}
if server == nil {
log.Panicln("server cannot be nil")
}
ch := make(chan models.ChatMessage, channelBufSize)
doneCh := make(chan bool)
pingch := make(chan models.PingMessage)
clientID := utils.GenerateClientIDFromRequest(ws.Request())
return &Client{time.Now(), 0, clientID, ws, server, ch, pingch, doneCh}
return &Client{time.Now(), 0, clientID, ws, ch, pingch, doneCh}
}
//GetConnection gets the connection for the client
@ -55,8 +50,8 @@ func (c *Client) Write(msg models.ChatMessage) {
select {
case c.ch <- msg:
default:
c.server.Remove(c)
c.server.Err(fmt.Errorf("client %s is disconnected", c.id))
_server.remove(c)
_server.err(fmt.Errorf("client %s is disconnected", c.id))
}
}
@ -86,7 +81,7 @@ func (c *Client) listenWrite() {
// receive done request
case <-c.doneCh:
c.server.Remove(c)
_server.remove(c)
c.doneCh <- true // for listenRead method
return
}
@ -100,7 +95,7 @@ func (c *Client) listenRead() {
// receive done request
case <-c.doneCh:
c.server.Remove(c)
_server.remove(c)
c.doneCh <- true // for listenWrite method
return
@ -112,10 +107,12 @@ func (c *Client) listenRead() {
c.doneCh <- true
return
} else if err != nil {
c.server.Err(err)
_server.err(err)
} else {
c.MessageCount++
c.server.SendToAll(msg)
msg.ClientID = c.id
_server.SendToAll(msg)
}
}
}

View File

@ -8,16 +8,21 @@ import (
log "github.com/sirupsen/logrus"
"golang.org/x/net/websocket"
"github.com/gabek/owncast/core"
"github.com/gabek/owncast/models"
)
var (
_server *server
)
//Server represents the server which handles the chat
type Server struct {
type server struct {
Messages []models.ChatMessage
Clients map[string]*Client
pattern string
listener models.ChatListener
addCh chan *Client
delCh chan *Client
sendAllCh chan models.ChatMessage
@ -26,88 +31,44 @@ type Server struct {
errCh chan error
}
//NewServer creates a new chat server
func NewServer() *Server {
messages := []models.ChatMessage{}
clients := make(map[string]*Client)
addCh := make(chan *Client)
delCh := make(chan *Client)
sendAllCh := make(chan models.ChatMessage)
pingCh := make(chan models.PingMessage)
doneCh := make(chan bool)
errCh := make(chan error)
// Demo messages only. Remove me eventually!!!
messages = append(messages, models.ChatMessage{"Tom Nook", "I'll be there with Bells on! Ho ho!", "https://gamepedia.cursecdn.com/animalcrossingpocketcamp_gamepedia_en/thumb/4/4f/Timmy_Icon.png/120px-Timmy_Icon.png?version=87b38d7d6130411d113486c2db151385", "demo-message-1", "ChatMessage"})
messages = append(messages, models.ChatMessage{"Redd", "Fool me once, shame on you. Fool me twice, stop foolin' me.", "https://vignette.wikia.nocookie.net/animalcrossing/images/3/3d/Redd2.gif/revision/latest?cb=20100710004252", "demo-message-2", "ChatMessage"})
messages = append(messages, models.ChatMessage{"Kevin", "You just caught me before I was about to go work out weeweewee!", "https://vignette.wikia.nocookie.net/animalcrossing/images/2/20/NH-Kevin_poster.png/revision/latest/scale-to-width-down/100?cb=20200410185817", "demo-message-3", "ChatMessage"})
messages = append(messages, models.ChatMessage{"Isabelle", " Isabelle is the mayor's highly capable secretary. She can be forgetful sometimes, but you can always count on her for information about the town. She wears her hair up in a bun that makes her look like a shih tzu. Mostly because she is one! She also has a twin brother named Digby.", "https://dodo.ac/np/images/thumb/7/7b/IsabelleTrophyWiiU.png/200px-IsabelleTrophyWiiU.png", "demo-message-4", "ChatMessage"})
messages = append(messages, models.ChatMessage{"Judy", "myohmy, I'm dancing my dreams away.", "https://vignette.wikia.nocookie.net/animalcrossing/images/5/50/NH-Judy_poster.png/revision/latest/scale-to-width-down/100?cb=20200522063219", "demo-message-5", "ChatMessage"})
messages = append(messages, models.ChatMessage{"Blathers", "Blathers is an owl with brown feathers. His face is white and he has a yellow beak. His arms are wing shaped and he has yellow talons. His eyes are very big with small black irises. He also has big pink cheek circles on his cheeks. His belly appears to be checkered in diamonds with light brown and white squares, similar to an argyle vest, which is traditionally associated with academia. His green bowtie further alludes to his academic nature.", "https://vignette.wikia.nocookie.net/animalcrossing/images/b/b3/NH-character-Blathers.png/revision/latest?cb=20200229053519", "demo-message-6", "ChatMessage"})
server := &Server{
messages,
clients,
"/entry", //hardcoded due to the UI requiring this and it is not configurable
addCh,
delCh,
sendAllCh,
pingCh,
doneCh,
errCh,
}
ticker := time.NewTicker(30 * time.Second)
go func() {
for {
select {
case <-ticker.C:
server.ping()
}
}
}()
return server
}
//Add adds a client to the server
func (s *Server) Add(c *Client) {
func (s *server) add(c *Client) {
s.addCh <- c
}
//Remove removes a client from the server
func (s *Server) Remove(c *Client) {
func (s *server) remove(c *Client) {
s.delCh <- c
}
//SendToAll sends a message to all of the connected clients
func (s *Server) SendToAll(msg models.ChatMessage) {
func (s *server) SendToAll(msg models.ChatMessage) {
s.sendAllCh <- msg
}
//Done marks the server as done
func (s *Server) Done() {
func (s *server) done() {
s.doneCh <- true
}
//Err handles an error
func (s *Server) Err(err error) {
func (s *server) err(err error) {
s.errCh <- err
}
func (s *Server) sendPastMessages(c *Client) {
func (s *server) sendPastMessages(c *Client) {
for _, msg := range s.Messages {
c.Write(msg)
}
}
func (s *Server) sendAll(msg models.ChatMessage) {
func (s *server) sendAll(msg models.ChatMessage) {
for _, c := range s.Clients {
c.Write(msg)
}
}
func (s *Server) ping() {
func (s *server) ping() {
// fmt.Println("Start pinging....", len(s.clients))
ping := models.PingMessage{MessageType: "PING"}
@ -116,8 +77,8 @@ func (s *Server) ping() {
}
}
func (s *Server) onConnection(ws *websocket.Conn) {
client := NewClient(ws, s)
func (s *server) onConnection(ws *websocket.Conn) {
client := NewClient(ws)
defer func() {
log.Printf("The client was connected for %s and sent %d messages (%s)", time.Since(client.ConnectedAt), client.MessageCount, client.id)
@ -127,13 +88,13 @@ func (s *Server) onConnection(ws *websocket.Conn) {
}
}()
s.Add(client)
s.add(client)
client.Listen()
}
// Listen and serve.
// It serves client connection and broadcast request.
func (s *Server) Listen() {
func (s *server) Listen() {
http.Handle(s.pattern, websocket.Handler(s.onConnection))
log.Printf("Starting the websocket listener on: %s", s.pattern)
@ -144,18 +105,18 @@ func (s *Server) Listen() {
case c := <-s.addCh:
s.Clients[c.id] = c
core.SetClientActive(c.id)
s.listener.ClientAdded(c.id)
s.sendPastMessages(c)
// remove a client
case c := <-s.delCh:
delete(s.Clients, c.id)
core.RemoveClient(c.id)
s.listener.ClientRemoved(c.id)
// broadcast a message to all clients
case msg := <-s.sendAllCh:
log.Println("Sending a message to all:", msg)
s.Messages = append(s.Messages, msg)
s.listener.MessageSent(msg)
s.sendAll(msg)
case ping := <-s.pingCh:

44
core/chatListener.go Normal file
View File

@ -0,0 +1,44 @@
package core
import (
"errors"
log "github.com/sirupsen/logrus"
"github.com/gabek/owncast/core/chat"
"github.com/gabek/owncast/models"
)
//ChatListenerImpl the implementation of the chat client
type ChatListenerImpl struct{}
//ClientAdded is for when a client is added the system
func (cl ChatListenerImpl) ClientAdded(clientID string) {
SetClientActive(clientID)
}
//ClientRemoved is for when a client disconnects/is removed
func (cl ChatListenerImpl) ClientRemoved(clientID string) {
RemoveClient(clientID)
}
//MessageSent is for when a message is sent
func (cl ChatListenerImpl) MessageSent(message models.ChatMessage) {
log.Printf("Message sent to all: %s", message.String())
}
//SendMessageToChat sends a message to the chat server
func SendMessageToChat(message models.ChatMessage) error {
if !message.Valid() {
return errors.New("invalid chat message; id, author, and body are required")
}
chat.SendMessage(message)
return nil
}
//GetAllChatMessages gets all of the chat messages
func GetAllChatMessages() []models.ChatMessage {
return chat.GetMessages()
}

View File

@ -8,6 +8,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/gabek/owncast/config"
"github.com/gabek/owncast/core/chat"
"github.com/gabek/owncast/core/ffmpeg"
"github.com/gabek/owncast/models"
"github.com/gabek/owncast/utils"
@ -37,6 +38,8 @@ func Start() error {
return err
}
chat.Setup(ChatListenerImpl{})
return nil
}

8
models/chatListener.go Normal file
View File

@ -0,0 +1,8 @@
package models
//ChatListener represents the listener for the chat server
type ChatListener interface {
ClientAdded(clientID string)
ClientRemoved(clientID string)
MessageSent(message ChatMessage)
}

View File

@ -2,6 +2,8 @@ package models
//ChatMessage represents a single chat message
type ChatMessage struct {
ClientID string `json:"-"`
Author string `json:"author"`
Body string `json:"body"`
Image string `json:"image"`
@ -14,3 +16,8 @@ type ChatMessage struct {
func (s ChatMessage) String() string {
return s.Author + " says " + s.Body
}
//Valid checks to ensure the message is valid
func (s ChatMessage) Valid() bool {
return s.Author != "" && s.Body != "" && s.ID != ""
}

View File

@ -14,9 +14,8 @@ import (
//Start starts the router for the http, ws, and rtmp
func Start() error {
// websocket server
chatServer := chat.NewServer()
go chatServer.Listen()
// websocket chat server
go chat.Start()
// start the rtmp server
go rtmp.Start()
@ -27,6 +26,9 @@ func Start() error {
// status of the system
http.HandleFunc("/status", controllers.GetStatus)
// chat rest api
http.HandleFunc("/chat", controllers.GetChatMessages)
port := config.Config.WebServerPort
log.Printf("Starting public web server on port: %d", port)