{"action":"created","comment":{"url":"https://api.github.com/repos/asm-products/landline-api/pulls/comments/25889891","id":25889891,"diff_hunk":"@@ -0,0 +1,115 @@\n+package handlers\n+\n+import (\n+\t\"fmt\"\n+\t\"os\"\n+\t\"github.com/gin-gonic/gin\"\n+\t\"github.com/googollee/go-socket.io\"\n+\t\"github.com/asm-products/landline-api/models\"\n+)\n+\n+var Socketio_Server *socketio.Server\n+\n+type sioMessage struct{\n+\tBody string\n+\tRoom string\n+}\n+\n+\n+func SetupSocketIOServer(){\n+\tvar err error\n+\tSocketio_Server, err = socketio.NewServer(nil)\n+    if  err != nil  {\n+        panic ( err )\n+    }\n+}\n+\n+func SocketHandler ( c  * gin.Context ) {\n+    Socketio_Server.On(\"connection\", func(so socketio.Socket) {\n+\t\t// Since this function is called for every connection,\n+\t\t// so the user variable is caught in this closure, and is only accessible\n+\t\t// to this connection.\n+\t\tvar user *models.User\n+\n+\t\t// The socket.io client doesn't support sending headers, so we can't use\n+\t\t// the standard auth mechanism. To solve this, before doing anything else,\n+\t\t// the client should emit an \"auth\" event, with their JWT as the message.\n+        so.On(\"auth\", func(token string) string {\n+\t\t\tfmt.Println(\"auth: \", token)\n+\t\t\tres, err := getUserFromJwt(token, os.Getenv(\"SECRET\"))\n+\t\t\tuser = res\n+\t\t\tif (err != nil){\n+\t\t\t\treturn \"error: \" + err.Error()\n+\t\t\t}\n+\t\t\treturn \"success\"\n+        })\n+\n+\t\t// To receive notifications for a room, the client emits the 'join' event,\n+\t\t// with the room slug as the message.\n+\t\tso.On(\"join\", func(roomSlug string) string {\n+\t\t\tif (user == nil){\n+\t\t\t\treturn \"error: not authenticated\"\n+\t\t\t}\n+\t\t\troom, err := models.FindRoom(roomSlug, user.TeamId)\n+  \t\t\tif (err != nil) {\n+    \t\t\treturn \"error: \" + err.Error()\n+  \t\t\t}\n+\t\t\terr = so.Join(room.Id)\n+\t\t\tif (err != nil) {\n+\t\t\t\treturn \"error: \" + err.Error()\n+\t\t\t}\n+\t\t\treturn \"success\"\n+\t\t})\n+\n+\t\t// Works like the 'join' event.\n+\t\tso.On(\"leave\", func(roomSlug string) string {\n+\t\t\tif (user == nil){\n+\t\t\t\treturn \"error: not authenticated\"\n+\t\t\t}\n+\t\t\troom, err := models.FindRoom(roomSlug, user.TeamId)\n+  \t\t\tif (err != nil) {\n+    \t\t\treturn \"error: \" + err.Error()\n+  \t\t\t}\n+\t\t\terr = so.Leave(room.Id)\n+\t\t\tif (err != nil) {\n+\t\t\t\treturn \"error: \" + err.Error()\n+\t\t\t}\n+\t\t\treturn \"success\"\n+\t\t})\n+\n+\t\tso.On(\"message\", func(m *sioMessage) string{\n+\t\t\tif(user == nil){\n+\t\t\t\treturn \"error: not authenticated\"\n+\t\t\t}\n+\t\t\troom, err := models.FindRoom(m.Room, user.TeamId)\n+  \t\t\tif (err != nil) {\n+    \t\t\treturn \"error: \" + err.Error()\n+  \t\t\t}\n+\t\t\t_, err = SendMessage(user, room, m.Body)\n+\t\t\tif (err != nil){\n+\t\t\t\treturn \"error: \" + err.Error()\n+\t\t\t}\n+\t\t\treturn \"success\"\n+\t\t})\n+\n+        so.On(\"disconnection\", func() {\n+            fmt.Println(\"on disconnect\")\n+        })\n+    })\n+\n+    Socketio_Server.On ( \"error\", func( so socketio.Socket, err error) {\n+        fmt.Printf ( \"[ WebSocket ] Error : %v\", err.Error () )\n+    })\n+\n+    Socketio_Server.ServeHTTP ( c.Writer, c.Request )\n+}\n+\n+// Browsers complain when the allowed origin is *, and there are cookies being set, which socket.io requires.\n+func SocketIOCors(c *gin.Context) {\n+\torigin := c.Request.Header.Get(\"Origin\")\n+\tif (origin != \"\"){\n+\t\tc.Writer.Header().Set(\"Access-Control-Allow-Origin\", origin)\n+\t}\n+\tc.Next()","path":"handlers/socketio_handler.go","position":null,"original_position":113,"commit_id":"8eb9230fd0e673d6171f094e10d2f461130f7977","original_commit_id":"3ef41749143895edc046db7604bec53e7a270fef","user":{"login":"bigblind","id":737493,"avatar_url":"https://avatars.githubusercontent.com/u/737493?v=3","gravatar_id":"","url":"https://api.github.com/users/bigblind","html_url":"https://github.com/bigblind","followers_url":"https://api.github.com/users/bigblind/followers","following_url":"https://api.github.com/users/bigblind/following{/other_user}","gists_url":"https://api.github.com/users/bigblind/gists{/gist_id}","starred_url":"https://api.github.com/users/bigblind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bigblind/subscriptions","organizations_url":"https://api.github.com/users/bigblind/orgs","repos_url":"https://api.github.com/users/bigblind/repos","events_url":"https://api.github.com/users/bigblind/events{/privacy}","received_events_url":"https://api.github.com/users/bigblind/received_events","type":"User","site_admin":false},"body":"done.","created_at":"2015-03-05T18:50:06Z","updated_at":"2015-03-05T18:50:06Z","html_url":"https://github.com/asm-products/landline-api/pull/1#discussion_r25889891","pull_request_url":"https://api.github.com/repos/asm-products/landline-api/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/asm-products/landline-api/pulls/comments/25889891"},"html":{"href":"https://github.com/asm-products/landline-api/pull/1#discussion_r25889891"},"pull_request":{"href":"https://api.github.com/repos/asm-products/landline-api/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/asm-products/landline-api/pulls/1","id":30494369,"html_url":"https://github.com/asm-products/landline-api/pull/1","diff_url":"https://github.com/asm-products/landline-api/pull/1.diff","patch_url":"https://github.com/asm-products/landline-api/pull/1.patch","issue_url":"https://api.github.com/repos/asm-products/landline-api/issues/1","number":1,"state":"open","locked":false,"title":"added socket.io handler","user":{"login":"bigblind","id":737493,"avatar_url":"https://avatars.githubusercontent.com/u/737493?v=3","gravatar_id":"","url":"https://api.github.com/users/bigblind","html_url":"https://github.com/bigblind","followers_url":"https://api.github.com/users/bigblind/followers","following_url":"https://api.github.com/users/bigblind/following{/other_user}","gists_url":"https://api.github.com/users/bigblind/gists{/gist_id}","starred_url":"https://api.github.com/users/bigblind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bigblind/subscriptions","organizations_url":"https://api.github.com/users/bigblind/orgs","repos_url":"https://api.github.com/users/bigblind/repos","events_url":"https://api.github.com/users/bigblind/events{/privacy}","received_events_url":"https://api.github.com/users/bigblind/received_events","type":"User","site_admin":false},"body":"Not done yet, but this way you can track progress, and give feedback if you want to.","created_at":"2015-03-04T19:09:34Z","updated_at":"2015-03-05T18:50:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0e6e97a64d08dc726539e6988004c739f9d33402","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/asm-products/landline-api/pulls/1/commits","review_comments_url":"https://api.github.com/repos/asm-products/landline-api/pulls/1/comments","review_comment_url":"https://api.github.com/repos/asm-products/landline-api/pulls/comments{/number}","comments_url":"https://api.github.com/repos/asm-products/landline-api/issues/1/comments","statuses_url":"https://api.github.com/repos/asm-products/landline-api/statuses/8eb9230fd0e673d6171f094e10d2f461130f7977","head":{"label":"bigblind:socketio","ref":"socketio","sha":"8eb9230fd0e673d6171f094e10d2f461130f7977","user":{"login":"bigblind","id":737493,"avatar_url":"https://avatars.githubusercontent.com/u/737493?v=3","gravatar_id":"","url":"https://api.github.com/users/bigblind","html_url":"https://github.com/bigblind","followers_url":"https://api.github.com/users/bigblind/followers","following_url":"https://api.github.com/users/bigblind/following{/other_user}","gists_url":"https://api.github.com/users/bigblind/gists{/gist_id}","starred_url":"https://api.github.com/users/bigblind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bigblind/subscriptions","organizations_url":"https://api.github.com/users/bigblind/orgs","repos_url":"https://api.github.com/users/bigblind/repos","events_url":"https://api.github.com/users/bigblind/events{/privacy}","received_events_url":"https://api.github.com/users/bigblind/received_events","type":"User","site_admin":false},"repo":{"id":31673781,"name":"landline-api","full_name":"bigblind/landline-api","owner":{"login":"bigblind","id":737493,"avatar_url":"https://avatars.githubusercontent.com/u/737493?v=3","gravatar_id":"","url":"https://api.github.com/users/bigblind","html_url":"https://github.com/bigblind","followers_url":"https://api.github.com/users/bigblind/followers","following_url":"https://api.github.com/users/bigblind/following{/other_user}","gists_url":"https://api.github.com/users/bigblind/gists{/gist_id}","starred_url":"https://api.github.com/users/bigblind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bigblind/subscriptions","organizations_url":"https://api.github.com/users/bigblind/orgs","repos_url":"https://api.github.com/users/bigblind/repos","events_url":"https://api.github.com/users/bigblind/events{/privacy}","received_events_url":"https://api.github.com/users/bigblind/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bigblind/landline-api","description":"Hosted, flexible chat","fork":true,"url":"https://api.github.com/repos/bigblind/landline-api","forks_url":"https://api.github.com/repos/bigblind/landline-api/forks","keys_url":"https://api.github.com/repos/bigblind/landline-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bigblind/landline-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bigblind/landline-api/teams","hooks_url":"https://api.github.com/repos/bigblind/landline-api/hooks","issue_events_url":"https://api.github.com/repos/bigblind/landline-api/issues/events{/number}","events_url":"https://api.github.com/repos/bigblind/landline-api/events","assignees_url":"https://api.github.com/repos/bigblind/landline-api/assignees{/user}","branches_url":"https://api.github.com/repos/bigblind/landline-api/branches{/branch}","tags_url":"https://api.github.com/repos/bigblind/landline-api/tags","blobs_url":"https://api.github.com/repos/bigblind/landline-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bigblind/landline-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bigblind/landline-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/bigblind/landline-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bigblind/landline-api/statuses/{sha}","languages_url":"https://api.github.com/repos/bigblind/landline-api/languages","stargazers_url":"https://api.github.com/repos/bigblind/landline-api/stargazers","contributors_url":"https://api.github.com/repos/bigblind/landline-api/contributors","subscribers_url":"https://api.github.com/repos/bigblind/landline-api/subscribers","subscription_url":"https://api.github.com/repos/bigblind/landline-api/subscription","commits_url":"https://api.github.com/repos/bigblind/landline-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/bigblind/landline-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/bigblind/landline-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/bigblind/landline-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/bigblind/landline-api/contents/{+path}","compare_url":"https://api.github.com/repos/bigblind/landline-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bigblind/landline-api/merges","archive_url":"https://api.github.com/repos/bigblind/landline-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bigblind/landline-api/downloads","issues_url":"https://api.github.com/repos/bigblind/landline-api/issues{/number}","pulls_url":"https://api.github.com/repos/bigblind/landline-api/pulls{/number}","milestones_url":"https://api.github.com/repos/bigblind/landline-api/milestones{/number}","notifications_url":"https://api.github.com/repos/bigblind/landline-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bigblind/landline-api/labels{/name}","releases_url":"https://api.github.com/repos/bigblind/landline-api/releases{/id}","created_at":"2015-03-04T18:50:04Z","updated_at":"2015-03-04T18:50:04Z","pushed_at":"2015-03-05T18:49:55Z","git_url":"git://github.com/bigblind/landline-api.git","ssh_url":"git@github.com:bigblind/landline-api.git","clone_url":"https://github.com/bigblind/landline-api.git","svn_url":"https://github.com/bigblind/landline-api","homepage":"https://assembly.com/landline","size":494,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"asm-products:master","ref":"master","sha":"d186ed067d7ae5a440a20708176d11a33925de25","user":{"login":"asm-products","id":6548361,"avatar_url":"https://avatars.githubusercontent.com/u/6548361?v=3","gravatar_id":"","url":"https://api.github.com/users/asm-products","html_url":"https://github.com/asm-products","followers_url":"https://api.github.com/users/asm-products/followers","following_url":"https://api.github.com/users/asm-products/following{/other_user}","gists_url":"https://api.github.com/users/asm-products/gists{/gist_id}","starred_url":"https://api.github.com/users/asm-products/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asm-products/subscriptions","organizations_url":"https://api.github.com/users/asm-products/orgs","repos_url":"https://api.github.com/users/asm-products/repos","events_url":"https://api.github.com/users/asm-products/events{/privacy}","received_events_url":"https://api.github.com/users/asm-products/received_events","type":"Organization","site_admin":false},"repo":{"id":30945558,"name":"landline-api","full_name":"asm-products/landline-api","owner":{"login":"asm-products","id":6548361,"avatar_url":"https://avatars.githubusercontent.com/u/6548361?v=3","gravatar_id":"","url":"https://api.github.com/users/asm-products","html_url":"https://github.com/asm-products","followers_url":"https://api.github.com/users/asm-products/followers","following_url":"https://api.github.com/users/asm-products/following{/other_user}","gists_url":"https://api.github.com/users/asm-products/gists{/gist_id}","starred_url":"https://api.github.com/users/asm-products/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asm-products/subscriptions","organizations_url":"https://api.github.com/users/asm-products/orgs","repos_url":"https://api.github.com/users/asm-products/repos","events_url":"https://api.github.com/users/asm-products/events{/privacy}","received_events_url":"https://api.github.com/users/asm-products/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/asm-products/landline-api","description":"Hosted, flexible chat","fork":false,"url":"https://api.github.com/repos/asm-products/landline-api","forks_url":"https://api.github.com/repos/asm-products/landline-api/forks","keys_url":"https://api.github.com/repos/asm-products/landline-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/asm-products/landline-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/asm-products/landline-api/teams","hooks_url":"https://api.github.com/repos/asm-products/landline-api/hooks","issue_events_url":"https://api.github.com/repos/asm-products/landline-api/issues/events{/number}","events_url":"https://api.github.com/repos/asm-products/landline-api/events","assignees_url":"https://api.github.com/repos/asm-products/landline-api/assignees{/user}","branches_url":"https://api.github.com/repos/asm-products/landline-api/branches{/branch}","tags_url":"https://api.github.com/repos/asm-products/landline-api/tags","blobs_url":"https://api.github.com/repos/asm-products/landline-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/asm-products/landline-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/asm-products/landline-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/asm-products/landline-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/asm-products/landline-api/statuses/{sha}","languages_url":"https://api.github.com/repos/asm-products/landline-api/languages","stargazers_url":"https://api.github.com/repos/asm-products/landline-api/stargazers","contributors_url":"https://api.github.com/repos/asm-products/landline-api/contributors","subscribers_url":"https://api.github.com/repos/asm-products/landline-api/subscribers","subscription_url":"https://api.github.com/repos/asm-products/landline-api/subscription","commits_url":"https://api.github.com/repos/asm-products/landline-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/asm-products/landline-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/asm-products/landline-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/asm-products/landline-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/asm-products/landline-api/contents/{+path}","compare_url":"https://api.github.com/repos/asm-products/landline-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/asm-products/landline-api/merges","archive_url":"https://api.github.com/repos/asm-products/landline-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/asm-products/landline-api/downloads","issues_url":"https://api.github.com/repos/asm-products/landline-api/issues{/number}","pulls_url":"https://api.github.com/repos/asm-products/landline-api/pulls{/number}","milestones_url":"https://api.github.com/repos/asm-products/landline-api/milestones{/number}","notifications_url":"https://api.github.com/repos/asm-products/landline-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/asm-products/landline-api/labels{/name}","releases_url":"https://api.github.com/repos/asm-products/landline-api/releases{/id}","created_at":"2015-02-18T00:34:09Z","updated_at":"2015-03-04T23:10:44Z","pushed_at":"2015-03-04T23:10:44Z","git_url":"git://github.com/asm-products/landline-api.git","ssh_url":"git@github.com:asm-products/landline-api.git","clone_url":"https://github.com/asm-products/landline-api.git","svn_url":"https://github.com/asm-products/landline-api","homepage":"https://assembly.com/landline","size":494,"stargazers_count":2,"watchers_count":2,"language":"Go","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/asm-products/landline-api/pulls/1"},"html":{"href":"https://github.com/asm-products/landline-api/pull/1"},"issue":{"href":"https://api.github.com/repos/asm-products/landline-api/issues/1"},"comments":{"href":"https://api.github.com/repos/asm-products/landline-api/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/asm-products/landline-api/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/asm-products/landline-api/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/asm-products/landline-api/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/asm-products/landline-api/statuses/8eb9230fd0e673d6171f094e10d2f461130f7977"}}}}