Skip to content

Commit

Permalink
feat: Added sync and task endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
bahattincinic committed May 20, 2024
1 parent 45a6944 commit ebca3a5
Show file tree
Hide file tree
Showing 19 changed files with 576 additions and 82 deletions.
6 changes: 1 addition & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
ENV=local

# Database
DATABASE_DSB="database.db"
DATABASE_DSN="database.db"
DATABASE_TYPE="sqlite"
DATABASE_AUTO_MIGRATE=true

# Strava
STRAVA_ACCESS_TOKEN=""
STRAVA_ATHLETE_ID=

# API
API_PORT=9000
5 changes: 4 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bahattincinic/fitwave/config"
"github.com/bahattincinic/fitwave/database"
"github.com/bahattincinic/fitwave/importer"
"github.com/bahattincinic/fitwave/queue"
"github.com/bahattincinic/fitwave/strava"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
Expand All @@ -23,9 +24,10 @@ type API struct {
log *zap.Logger
st *strava.Strava
im *importer.Importer
q *queue.Queue
}

func RunAPI(ctx context.Context, wg *sync.WaitGroup, log *zap.Logger, db *database.Database, cfg *config.Config, st *strava.Strava, im *importer.Importer) {
func RunAPI(ctx context.Context, wg *sync.WaitGroup, log *zap.Logger, db *database.Database, cfg *config.Config, st *strava.Strava, im *importer.Importer, q *queue.Queue) {
srv := &API{
ec: echo.New(),
ctx: ctx,
Expand All @@ -34,6 +36,7 @@ func RunAPI(ctx context.Context, wg *sync.WaitGroup, log *zap.Logger, db *databa
log: log,
st: st,
im: im,
q: q,
}
srv.ec.Server.IdleTimeout = 120 * time.Second

Expand Down
46 changes: 46 additions & 0 deletions api/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package api

import (
"net/http"

"github.com/bahattincinic/fitwave/strava"
"github.com/labstack/echo/v4"
)

// syncData godoc
//
// @Summary Sync Strava data
// @Tags data
// @Accept json
// @Param Authorization header string true "Strava Access Token"
// @Success 200 {object} queue.TaskResult
// @Router /data/sync [post]
func (a *API) syncData(c echo.Context) error {
user := c.Get(userContextKey).(*strava.User)

task := a.q.AddTask(func() (interface{}, error) {
if err := a.im.Import(user); err != nil {
return nil, err
}
return nil, nil
})

return c.JSON(http.StatusOK, task)
}

// getTask godoc
//
// @Summary Get Task Detail
// @Tags data
// @Accept json
// @Param id path string true "Task ID"
// @Success 200 {object} queue.TaskResult
// @Router /data/task/{id} [get]
func (a *API) getTask(c echo.Context) error {
task, err := a.q.GetResult(c.Param("id"))

if err != nil {
return c.JSON(http.StatusNotFound, err)
}
return c.JSON(http.StatusOK, task)
}
93 changes: 87 additions & 6 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,62 @@ const docTemplate = `{
}
}
},
"/data/sync": {
"post": {
"consumes": [
"application/json"
],
"tags": [
"data"
],
"summary": "Sync Strava data",
"parameters": [
{
"type": "string",
"description": "Strava Access Token",
"name": "Authorization",
"in": "header",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/queue.TaskResult"
}
}
}
}
},
"/data/task/{id}": {
"get": {
"consumes": [
"application/json"
],
"tags": [
"data"
],
"summary": "Get Task Detail",
"parameters": [
{
"type": "string",
"description": "Task ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/queue.TaskResult"
}
}
}
}
},
"/gears/": {
"get": {
"consumes": [
Expand Down Expand Up @@ -525,12 +581,6 @@ const docTemplate = `{
"models.Config": {
"type": "object",
"properties": {
"access_token": {
"type": "string"
},
"athlete_id": {
"type": "integer"
},
"client_id": {
"type": "string"
},
Expand Down Expand Up @@ -573,6 +623,37 @@ const docTemplate = `{
"type": "boolean"
}
}
},
"queue.TaskResult": {
"type": "object",
"properties": {
"completionTime": {
"type": "string"
},
"error": {},
"id": {
"type": "string"
},
"result": {},
"status": {
"$ref": "#/definitions/queue.TaskStatus"
}
}
},
"queue.TaskStatus": {
"type": "string",
"enum": [
"pending",
"success",
"error",
"archived"
],
"x-enum-varnames": [
"Pending",
"Success",
"Error",
"Archived"
]
}
}
}`
Expand Down
93 changes: 87 additions & 6 deletions api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,62 @@
}
}
},
"/data/sync": {
"post": {
"consumes": [
"application/json"
],
"tags": [
"data"
],
"summary": "Sync Strava data",
"parameters": [
{
"type": "string",
"description": "Strava Access Token",
"name": "Authorization",
"in": "header",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/queue.TaskResult"
}
}
}
}
},
"/data/task/{id}": {
"get": {
"consumes": [
"application/json"
],
"tags": [
"data"
],
"summary": "Get Task Detail",
"parameters": [
{
"type": "string",
"description": "Task ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/queue.TaskResult"
}
}
}
}
},
"/gears/": {
"get": {
"consumes": [
Expand Down Expand Up @@ -514,12 +570,6 @@
"models.Config": {
"type": "object",
"properties": {
"access_token": {
"type": "string"
},
"athlete_id": {
"type": "integer"
},
"client_id": {
"type": "string"
},
Expand Down Expand Up @@ -562,6 +612,37 @@
"type": "boolean"
}
}
},
"queue.TaskResult": {
"type": "object",
"properties": {
"completionTime": {
"type": "string"
},
"error": {},
"id": {
"type": "string"
},
"result": {},
"status": {
"$ref": "#/definitions/queue.TaskStatus"
}
}
},
"queue.TaskStatus": {
"type": "string",
"enum": [
"pending",
"success",
"error",
"archived"
],
"x-enum-varnames": [
"Pending",
"Success",
"Error",
"Archived"
]
}
}
}
Loading

0 comments on commit ebca3a5

Please sign in to comment.