Skip to content

Commit

Permalink
sort live
Browse files Browse the repository at this point in the history
  • Loading branch information
chigusa committed Nov 5, 2019
1 parent f53ff00 commit 678eb3f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/servers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"sort"

"github.com/gorilla/mux"
"github.com/tidwall/gjson"
Expand All @@ -33,12 +34,25 @@ func parseInfo(ctx context.Context, l live.Live) *live.Info {
return info
}

type liveSlice []*live.Info

func (c liveSlice) Len() int {
return len(c)
}
func (c liveSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c liveSlice) Less(i, j int) bool {
return c[i].Live.GetLiveId() < c[j].Live.GetLiveId()
}

func getAllLives(writer http.ResponseWriter, r *http.Request) {
inst := instance.GetInstance(r.Context())
info := make([]*live.Info, 0)
info := liveSlice(make([]*live.Info, 0))
for _, v := range inst.Lives {
info = append(info, parseInfo(r.Context(), v))
}
sort.Sort(info)
resp := CommonResp{
Data: info,
}
Expand Down Expand Up @@ -107,7 +121,7 @@ func parseLiveAction(writer http.ResponseWriter, r *http.Request) {
*/
func addLives(writer http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)
info := make([]*live.Info, 0)
info := liveSlice(make([]*live.Info, 0))
gjson.GetBytes(b, "lives").ForEach(func(key, value gjson.Result) bool {
isListen := value.Get("listen").Bool()
u, _ := url.Parse(value.Get("url").String())
Expand All @@ -123,6 +137,7 @@ func addLives(writer http.ResponseWriter, r *http.Request) {
}
return true
})
sort.Sort(info)
resp := CommonResp{
Data: info,
}
Expand Down

0 comments on commit 678eb3f

Please sign in to comment.