{"action":"created","comment":{"url":"https://api.github.com/repos/flynn/flynn/pulls/comments/22838256","id":22838256,"diff_hunk":"@@ -0,0 +1,134 @@\n+package main\n+\n+import (\n+\t\"encoding/json\"\n+\t\"errors\"\n+\t\"fmt\"\n+\t\"net\"\n+\t\"net/http\"\n+\t\"strings\"\n+\t\"time\"\n+\n+\t\"github.com/flynn/flynn/Godeps/_workspace/src/github.com/julienschmidt/httprouter\"\n+\t\"github.com/flynn/flynn/host/types\"\n+\t\"github.com/flynn/flynn/pkg/httphelper\"\n+\t\"github.com/flynn/flynn/pkg/shutdown\"\n+\t\"github.com/flynn/flynn/pkg/sse\"\n+)\n+\n+type Host struct {\n+\tstate   *State\n+\tbackend Backend\n+}\n+\n+func (h *Host) StopJob(id string) error {\n+\tjob := h.state.GetJob(id)\n+\tif job == nil {\n+\t\treturn errors.New(\"host: unknown job\")\n+\t}\n+\tswitch job.Status {\n+\tcase host.StatusStarting:\n+\t\th.state.SetForceStop(id)\n+\t\treturn nil\n+\tcase host.StatusRunning:\n+\t\treturn h.backend.Stop(id)\n+\tdefault:\n+\t\treturn errors.New(\"host: job is already stopped\")\n+\t}\n+}\n+\n+func (h *Host) streamEvents(id string, w http.ResponseWriter) error {\n+\tch := h.state.AddListener(id)\n+\tgo func() {\n+\t\t<-w.(http.CloseNotifier).CloseNotify()\n+\t\th.state.RemoveListener(id, ch)\n+\t}()\n+\tenc := json.NewEncoder(sse.NewSSEWriter(w))\n+\tw.Header().Set(\"Content-Type\", \"text/event-stream; charset=utf-8\")\n+\tw.WriteHeader(200)\n+\tw.(http.Flusher).Flush()\n+\tfor {\n+\t\tselect {\n+\t\tcase data := <-ch:\n+\t\t\tif err := enc.Encode(data); err != nil {\n+\t\t\t\treturn err\n+\t\t\t}\n+\t\t\tw.(http.Flusher).Flush()\n+\t\tcase <-time.After(10 * time.Second):\n+\t\t\t// if the job still doesn't exist after a reasonable timeout, then\n+\t\t\t// break out of this stream (closing the connection will be handled outside)\n+\t\t\tif id != \"all\" && h.state.GetJob(id) == nil {\n+\t\t\t\treturn fmt.Errorf(\"no such job\")\n+\t\t\t}\n+\t\t}\n+\t}\n+\treturn nil\n+}\n+\n+type httpAPI struct {\n+\thost *Host\n+}\n+\n+func (h *httpAPI) listJobs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {\n+\tif strings.Contains(r.Header.Get(\"Accept\"), \"text/event-stream\") {\n+\t\tif err := h.host.streamEvents(\"all\", w); err != nil {\n+\t\t\thttphelper.Error(w, err)\n+\t\t}\n+\t\treturn\n+\t}\n+\tres := h.host.state.Get()\n+\n+\thttphelper.JSON(w, 200, res)\n+}\n+\n+func (h *httpAPI) getJob(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {\n+\tid := ps.ByName(\"id\")\n+\n+\tif strings.Contains(r.Header.Get(\"Accept\"), \"text/event-stream\") {\n+\t\tif err := h.host.streamEvents(id, w); err != nil {\n+\t\t\thttphelper.Error(w, err)\n+\t\t}\n+\t\treturn\n+\t}\n+\tjob := h.host.state.GetJob(id)\n+\thttphelper.JSON(w, 200, job)\n+}\n+\n+func (h *httpAPI) stopJob(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {","path":"host/http.go","position":97,"original_position":97,"commit_id":"9566ec29247a27e7047228f695fb33a01b6c28f4","original_commit_id":"9566ec29247a27e7047228f695fb33a01b6c28f4","user":{"login":"titanous","id":13026,"avatar_url":"https://avatars.githubusercontent.com/u/13026?v=3","gravatar_id":"","url":"https://api.github.com/users/titanous","html_url":"https://github.com/titanous","followers_url":"https://api.github.com/users/titanous/followers","following_url":"https://api.github.com/users/titanous/following{/other_user}","gists_url":"https://api.github.com/users/titanous/gists{/gist_id}","starred_url":"https://api.github.com/users/titanous/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/titanous/subscriptions","organizations_url":"https://api.github.com/users/titanous/orgs","repos_url":"https://api.github.com/users/titanous/repos","events_url":"https://api.github.com/users/titanous/events{/privacy}","received_events_url":"https://api.github.com/users/titanous/received_events","type":"User","site_admin":false},"body":"I like using capitalized (\"exported\") methods to distinguish from any helper methods.","created_at":"2015-01-13T01:39:03Z","updated_at":"2015-01-13T01:39:03Z","html_url":"https://github.com/flynn/flynn/pull/663#discussion_r22838256","pull_request_url":"https://api.github.com/repos/flynn/flynn/pulls/663","_links":{"self":{"href":"https://api.github.com/repos/flynn/flynn/pulls/comments/22838256"},"html":{"href":"https://github.com/flynn/flynn/pull/663#discussion_r22838256"},"pull_request":{"href":"https://api.github.com/repos/flynn/flynn/pulls/663"}}},"pull_request":{"url":"https://api.github.com/repos/flynn/flynn/pulls/663","id":26590759,"html_url":"https://github.com/flynn/flynn/pull/663","diff_url":"https://github.com/flynn/flynn/pull/663.diff","patch_url":"https://github.com/flynn/flynn/pull/663.patch","issue_url":"https://api.github.com/repos/flynn/flynn/issues/663","number":663,"state":"open","locked":false,"title":"Convert host RPC to HTTP API","user":{"login":"archSeer","id":1372918,"avatar_url":"https://avatars.githubusercontent.com/u/1372918?v=3","gravatar_id":"","url":"https://api.github.com/users/archSeer","html_url":"https://github.com/archSeer","followers_url":"https://api.github.com/users/archSeer/followers","following_url":"https://api.github.com/users/archSeer/following{/other_user}","gists_url":"https://api.github.com/users/archSeer/gists{/gist_id}","starred_url":"https://api.github.com/users/archSeer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/archSeer/subscriptions","organizations_url":"https://api.github.com/users/archSeer/orgs","repos_url":"https://api.github.com/users/archSeer/repos","events_url":"https://api.github.com/users/archSeer/events{/privacy}","received_events_url":"https://api.github.com/users/archSeer/received_events","type":"User","site_admin":false},"body":"Opening this up so we can track progress and related discussions here. Prep work should be almost done, now we just need to get this passing on the CI.","created_at":"2014-12-26T05:30:42Z","updated_at":"2015-01-13T01:39:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d48e601b3d0c5edf7b947c573a137644e0c46837","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/flynn/flynn/pulls/663/commits","review_comments_url":"https://api.github.com/repos/flynn/flynn/pulls/663/comments","review_comment_url":"https://api.github.com/repos/flynn/flynn/pulls/comments/{number}","comments_url":"https://api.github.com/repos/flynn/flynn/issues/663/comments","statuses_url":"https://api.github.com/repos/flynn/flynn/statuses/9566ec29247a27e7047228f695fb33a01b6c28f4","head":{"label":"IceDragon200:host-rpc-to-http","ref":"host-rpc-to-http","sha":"9566ec29247a27e7047228f695fb33a01b6c28f4","user":{"login":"IceDragon200","id":1649873,"avatar_url":"https://avatars.githubusercontent.com/u/1649873?v=3","gravatar_id":"","url":"https://api.github.com/users/IceDragon200","html_url":"https://github.com/IceDragon200","followers_url":"https://api.github.com/users/IceDragon200/followers","following_url":"https://api.github.com/users/IceDragon200/following{/other_user}","gists_url":"https://api.github.com/users/IceDragon200/gists{/gist_id}","starred_url":"https://api.github.com/users/IceDragon200/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/IceDragon200/subscriptions","organizations_url":"https://api.github.com/users/IceDragon200/orgs","repos_url":"https://api.github.com/users/IceDragon200/repos","events_url":"https://api.github.com/users/IceDragon200/events{/privacy}","received_events_url":"https://api.github.com/users/IceDragon200/received_events","type":"User","site_admin":false},"repo":{"id":23473126,"name":"flynn","full_name":"IceDragon200/flynn","owner":{"login":"IceDragon200","id":1649873,"avatar_url":"https://avatars.githubusercontent.com/u/1649873?v=3","gravatar_id":"","url":"https://api.github.com/users/IceDragon200","html_url":"https://github.com/IceDragon200","followers_url":"https://api.github.com/users/IceDragon200/followers","following_url":"https://api.github.com/users/IceDragon200/following{/other_user}","gists_url":"https://api.github.com/users/IceDragon200/gists{/gist_id}","starred_url":"https://api.github.com/users/IceDragon200/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/IceDragon200/subscriptions","organizations_url":"https://api.github.com/users/IceDragon200/orgs","repos_url":"https://api.github.com/users/IceDragon200/repos","events_url":"https://api.github.com/users/IceDragon200/events{/privacy}","received_events_url":"https://api.github.com/users/IceDragon200/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/IceDragon200/flynn","description":"A next generation open source platform as a service (PaaS)","fork":true,"url":"https://api.github.com/repos/IceDragon200/flynn","forks_url":"https://api.github.com/repos/IceDragon200/flynn/forks","keys_url":"https://api.github.com/repos/IceDragon200/flynn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/IceDragon200/flynn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/IceDragon200/flynn/teams","hooks_url":"https://api.github.com/repos/IceDragon200/flynn/hooks","issue_events_url":"https://api.github.com/repos/IceDragon200/flynn/issues/events{/number}","events_url":"https://api.github.com/repos/IceDragon200/flynn/events","assignees_url":"https://api.github.com/repos/IceDragon200/flynn/assignees{/user}","branches_url":"https://api.github.com/repos/IceDragon200/flynn/branches{/branch}","tags_url":"https://api.github.com/repos/IceDragon200/flynn/tags","blobs_url":"https://api.github.com/repos/IceDragon200/flynn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/IceDragon200/flynn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/IceDragon200/flynn/git/refs{/sha}","trees_url":"https://api.github.com/repos/IceDragon200/flynn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/IceDragon200/flynn/statuses/{sha}","languages_url":"https://api.github.com/repos/IceDragon200/flynn/languages","stargazers_url":"https://api.github.com/repos/IceDragon200/flynn/stargazers","contributors_url":"https://api.github.com/repos/IceDragon200/flynn/contributors","subscribers_url":"https://api.github.com/repos/IceDragon200/flynn/subscribers","subscription_url":"https://api.github.com/repos/IceDragon200/flynn/subscription","commits_url":"https://api.github.com/repos/IceDragon200/flynn/commits{/sha}","git_commits_url":"https://api.github.com/repos/IceDragon200/flynn/git/commits{/sha}","comments_url":"https://api.github.com/repos/IceDragon200/flynn/comments{/number}","issue_comment_url":"https://api.github.com/repos/IceDragon200/flynn/issues/comments/{number}","contents_url":"https://api.github.com/repos/IceDragon200/flynn/contents/{+path}","compare_url":"https://api.github.com/repos/IceDragon200/flynn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/IceDragon200/flynn/merges","archive_url":"https://api.github.com/repos/IceDragon200/flynn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/IceDragon200/flynn/downloads","issues_url":"https://api.github.com/repos/IceDragon200/flynn/issues{/number}","pulls_url":"https://api.github.com/repos/IceDragon200/flynn/pulls{/number}","milestones_url":"https://api.github.com/repos/IceDragon200/flynn/milestones{/number}","notifications_url":"https://api.github.com/repos/IceDragon200/flynn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/IceDragon200/flynn/labels{/name}","releases_url":"https://api.github.com/repos/IceDragon200/flynn/releases{/id}","created_at":"2014-08-29T18:11:37Z","updated_at":"2014-12-04T17:27:54Z","pushed_at":"2015-01-13T01:32:50Z","git_url":"git://github.com/IceDragon200/flynn.git","ssh_url":"git@github.com:IceDragon200/flynn.git","clone_url":"https://github.com/IceDragon200/flynn.git","svn_url":"https://github.com/IceDragon200/flynn","homepage":"https://flynn.io","size":10283,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"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":"flynn:master","ref":"master","sha":"40b1a276eec4c1650309cdb438460e0bc7b3eee7","user":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"repo":{"id":11290232,"name":"flynn","full_name":"flynn/flynn","owner":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/flynn/flynn","description":"A next generation open source platform as a service (PaaS)","fork":false,"url":"https://api.github.com/repos/flynn/flynn","forks_url":"https://api.github.com/repos/flynn/flynn/forks","keys_url":"https://api.github.com/repos/flynn/flynn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/flynn/flynn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/flynn/flynn/teams","hooks_url":"https://api.github.com/repos/flynn/flynn/hooks","issue_events_url":"https://api.github.com/repos/flynn/flynn/issues/events{/number}","events_url":"https://api.github.com/repos/flynn/flynn/events","assignees_url":"https://api.github.com/repos/flynn/flynn/assignees{/user}","branches_url":"https://api.github.com/repos/flynn/flynn/branches{/branch}","tags_url":"https://api.github.com/repos/flynn/flynn/tags","blobs_url":"https://api.github.com/repos/flynn/flynn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/flynn/flynn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/flynn/flynn/git/refs{/sha}","trees_url":"https://api.github.com/repos/flynn/flynn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/flynn/flynn/statuses/{sha}","languages_url":"https://api.github.com/repos/flynn/flynn/languages","stargazers_url":"https://api.github.com/repos/flynn/flynn/stargazers","contributors_url":"https://api.github.com/repos/flynn/flynn/contributors","subscribers_url":"https://api.github.com/repos/flynn/flynn/subscribers","subscription_url":"https://api.github.com/repos/flynn/flynn/subscription","commits_url":"https://api.github.com/repos/flynn/flynn/commits{/sha}","git_commits_url":"https://api.github.com/repos/flynn/flynn/git/commits{/sha}","comments_url":"https://api.github.com/repos/flynn/flynn/comments{/number}","issue_comment_url":"https://api.github.com/repos/flynn/flynn/issues/comments/{number}","contents_url":"https://api.github.com/repos/flynn/flynn/contents/{+path}","compare_url":"https://api.github.com/repos/flynn/flynn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/flynn/flynn/merges","archive_url":"https://api.github.com/repos/flynn/flynn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/flynn/flynn/downloads","issues_url":"https://api.github.com/repos/flynn/flynn/issues{/number}","pulls_url":"https://api.github.com/repos/flynn/flynn/pulls{/number}","milestones_url":"https://api.github.com/repos/flynn/flynn/milestones{/number}","notifications_url":"https://api.github.com/repos/flynn/flynn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/flynn/flynn/labels{/name}","releases_url":"https://api.github.com/repos/flynn/flynn/releases{/id}","created_at":"2013-07-09T18:22:06Z","updated_at":"2015-01-13T01:19:15Z","pushed_at":"2015-01-13T01:14:20Z","git_url":"git://github.com/flynn/flynn.git","ssh_url":"git@github.com:flynn/flynn.git","clone_url":"https://github.com/flynn/flynn.git","svn_url":"https://github.com/flynn/flynn","homepage":"https://flynn.io","size":29037,"stargazers_count":3112,"watchers_count":3112,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":190,"mirror_url":null,"open_issues_count":108,"forks":190,"open_issues":108,"watchers":3112,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/flynn/flynn/pulls/663"},"html":{"href":"https://github.com/flynn/flynn/pull/663"},"issue":{"href":"https://api.github.com/repos/flynn/flynn/issues/663"},"comments":{"href":"https://api.github.com/repos/flynn/flynn/issues/663/comments"},"review_comments":{"href":"https://api.github.com/repos/flynn/flynn/pulls/663/comments"},"review_comment":{"href":"https://api.github.com/repos/flynn/flynn/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/flynn/flynn/pulls/663/commits"},"statuses":{"href":"https://api.github.com/repos/flynn/flynn/statuses/9566ec29247a27e7047228f695fb33a01b6c28f4"}}}}