Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sedflix committed Oct 2, 2020
1 parent b710b63 commit 5d72f81
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
30 changes: 27 additions & 3 deletions fitness.go → src/fitness.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"google.golang.org/api/people/v1"
"log"
"net/http"
"sort"
"strings"
"time"
)
Expand All @@ -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) {

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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,
)
}
File renamed without changes.
18 changes: 11 additions & 7 deletions main.go → src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions oauth.go → src/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.

0 comments on commit 5d72f81

Please sign in to comment.