diff --git a/fitness.go b/src/fitness.go similarity index 88% rename from fitness.go rename to src/fitness.go index e39cb32..698050e 100644 --- a/fitness.go +++ b/src/fitness.go @@ -11,6 +11,7 @@ import ( "google.golang.org/api/people/v1" "log" "net/http" + "sort" "strings" "time" ) @@ -23,6 +24,13 @@ type userInfoElement struct { StepsWeek int64 } +// byStepsWeek implements sort.Interface based on the userInfoElement.StepsWeek field. +type allUserInfo []userInfoElement + +func (a allUserInfo) Len() int { return len(a) } +func (a allUserInfo) Less(i, j int) bool { return a[i].StepsWeek > a[j].StepsWeek } +func (a allUserInfo) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + // getFitnessService returns the service object google fitness with apt permission to read steps func getFitnessService(user OAuthUser) (*fitness.Service, error) { @@ -189,7 +197,7 @@ func getDetailsWrapper( return } -func getAll() (map[string]userInfoElement, error) { +func getAll() ([]userInfoElement, error) { // get all users in usersChannels usersChannels := make(chan OAuthUser) @@ -203,10 +211,10 @@ func getAll() (map[string]userInfoElement, error) { go getDetailsWrapper(resultQueue, user, getDetails) } - result := make(map[string]userInfoElement) + result := make([]userInfoElement, 0) for i := 0; i < numbersOfUsers; i++ { element := <-resultQueue - result[element.Email] = element + result = append(result, element) } return result, nil @@ -219,5 +227,21 @@ func list(ctx *gin.Context) { _ = ctx.AbortWithError(http.StatusInternalServerError, err) return } + sort.Sort(allUserInfo(result)) ctx.IndentedJSON(http.StatusOK, result) } + +func index(ctx *gin.Context) { + _ = sessions.Default(ctx) + result, err := getAll() + if err != nil { + _ = ctx.AbortWithError(http.StatusInternalServerError, err) + return + } + sort.Sort(allUserInfo(result)) + ctx.HTML( + http.StatusOK, + "index.html", + result, + ) +} diff --git a/go.mod b/src/go.mod similarity index 100% rename from go.mod rename to src/go.mod diff --git a/main.go b/src/main.go similarity index 69% rename from main.go rename to src/main.go index 31bebfd..f1287d8 100644 --- a/main.go +++ b/src/main.go @@ -52,14 +52,18 @@ func main() { // custom error handling TODO: add ui router.Use(ErrorHandle()) - // index page - //router.Static("/css", "./static/css") - //router.Static("/img", "./static/img") - //router.LoadHTMLGlob("templates/*") + // static + router.Static("/css", "./web/css") + router.Static("/img", "./web/img") + router.Static("/js", "./web/js") + router.StaticFile("/favicon.ico", "./web/favicon.ico") + router.LoadHTMLFiles("web/index.html") - router.GET("/list", list) - router.GET("/login", authoriseUserHandler) - router.GET("/auth", oAuthCallbackHandler) + router.GET("/", index) // index page + router.GET("/list/json", list) // show information in json + + router.GET("/login", authoriseUserHandler) // to register + router.GET("/auth", oAuthCallbackHandler) // oauth callback // Add the pprof routes //pprof.Register(router) diff --git a/mongo.go b/src/mongo.go similarity index 100% rename from mongo.go rename to src/mongo.go diff --git a/oauth.go b/src/oauth.go similarity index 98% rename from oauth.go rename to src/oauth.go index 098d4ec..4c1beea 100644 --- a/oauth.go +++ b/src/oauth.go @@ -103,9 +103,7 @@ func oAuthCallbackHandler(ctx *gin.Context) { return } - ctx.JSON(http.StatusOK, gin.H{ - "user": user.Email, - }) + ctx.Redirect(http.StatusTemporaryRedirect, "/") } // authoriseUserHandler redirects the user to appropriate url for auth diff --git a/utils.go b/src/utils.go similarity index 100% rename from utils.go rename to src/utils.go