Skip to content

Commit

Permalink
Merge pull request #2 from gaurigshankar/rest-service
Browse files Browse the repository at this point in the history
Added rest service endpoint for getting all messages
  • Loading branch information
gaurigshankar authored Aug 16, 2017
2 parents a99e85f + 03b7b0c commit 1b61405
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
6 changes: 1 addition & 5 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ This app is deployed to Heroku and can be accessed via https://gauri-golang-chat
### Dependencies
* Using https://github.com/tools/godep for managing Dependencies.
* Whenever new Dependencies are added / used, run `godep save ./...` . This will update Godeps/Godeps.json with new dependencies and copy the dependencies to vendor directory.

### Rest Services
* Get all the current messages from `/getAllMessages`
9 changes: 5 additions & 4 deletions chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ <h1 class="text-center">Welcome to chat powered by Golang</h1>
var message = document.getElementById("userMessage")
var userName = document.getElementById("userName")
var output = document.getElementById("output")
//alert("PORT ..s. "+location.hostname)
var hostName = location.hostname;
var socket;
if(hostName.indexOf("herokuapp") !== -1){
Expand All @@ -88,8 +87,9 @@ <h1 class="text-center">Welcome to chat powered by Golang</h1>
var userSpanNode = document.createElement("span");
var boldNode = document.createElement("strong");
var messageSpanNode = document.createElement("span");
userSpanNode.setAttribute("class","col-2");
messageSpanNode.setAttribute("class", "col-10");
userSpanNode.setAttribute("class","col col-md-4");
messageSpanNode.setAttribute("class", "col col-md-8");
divNode.setAttribute("class", "row");
var userTextnode = document.createTextNode(messageDetails.userName+"-"+messageDetails.timestamp+" : ");
var messageTextnode = document.createTextNode(messageDetails.body+ "\n");
boldNode.appendChild(userTextnode);
Expand All @@ -104,7 +104,8 @@ <h1 class="text-center">Welcome to chat powered by Golang</h1>
if(userName.value === "") {
userName.value = "Anonymous"
}
var currenTimeStamp = new Date().toLocaleString()
var time = new Date();
var currenTimeStamp = time.toLocaleString('en-US', { hour: 'numeric',minute:'numeric', hour12: true });
var messageDetails = {
userName: userName.value,
body: message.value,
Expand Down
19 changes: 14 additions & 5 deletions chat/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chat
import (
"log"
"net/http"
"encoding/json"
"github.com/gorilla/websocket"
)

Expand All @@ -13,7 +14,7 @@ var upgrader = websocket.Upgrader{

type Server struct {
connectedUsers map[int] *User
messages []*Message
Messages []*Message `json: messages`
addUser chan *User
removeUser chan *User
newIncomingMessage chan *Message
Expand All @@ -23,7 +24,7 @@ type Server struct {
}

func NewServer() *Server {
messages := []*Message{}
Messages := []*Message{}
connectedUsers := make(map[int]*User)
addUser := make(chan *User)
removeUser := make(chan *User)
Expand All @@ -33,7 +34,7 @@ func NewServer() *Server {

return &Server {
connectedUsers,
messages,
Messages,
addUser,
removeUser,
newIncomingMessage,
Expand Down Expand Up @@ -63,7 +64,7 @@ func (server *Server) Done() {
}

func (server *Server) sendPastMessages(user *User) {
for _, msg := range server.messages {
for _, msg := range server.Messages {
// log.Println("In sendPastMessages writing ",msg)
user.Write(msg)
}
Expand All @@ -84,6 +85,7 @@ func (server *Server) Listen() {
log.Println("Server Listening .....")

http.HandleFunc("/chat", server.handleChat)
http.HandleFunc("/getAllMessages", server.handleGetAllMessages)

for {
select {
Expand All @@ -99,7 +101,7 @@ func (server *Server) Listen() {
delete(server.connectedUsers, user.id)

case msg := <-server.newIncomingMessage:
server.messages = append(server.messages, msg)
server.Messages = append(server.Messages, msg)
server.sendAll(msg)
case err := <-server.errorChannel:
log.Println("Error : ",err)
Expand Down Expand Up @@ -132,3 +134,10 @@ func (server *Server) handleChat(responseWriter http.ResponseWriter, request *ht


}


func (server *Server) handleGetAllMessages(responseWriter http.ResponseWriter, request *http.Request) {


json.NewEncoder(responseWriter).Encode(server)
}

0 comments on commit 1b61405

Please sign in to comment.