Skip to content

Commit

Permalink
Merge pull request #58 from tensorplex-labs/staging
Browse files Browse the repository at this point in the history
staging to main
  • Loading branch information
karootplx authored Dec 23, 2024
2 parents 363a24e + 0f321ec commit e49b190
Show file tree
Hide file tree
Showing 27 changed files with 849 additions and 397 deletions.
19 changes: 10 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ run:
go: "1.22"
timeout: 3m
allow-parallel-runners: true

output:
formats: colored-line-number

linters:
disable-all: true
enable:
- gosimple # Suggest code simplifications
- govet # Reports suspicious constructs
- ineffassign # Detects unused variable assignments
- staticcheck # go vet on steroids
- unused # Detect unused constants, variables, functions, and types
- gocyclo # Detect cyclomatic complexity
- gofmt # Enforce standard formatting
- gosimple # Suggest code simplifications
- govet # Reports suspicious constructs
- ineffassign # Detects unused variable assignments
- staticcheck # go vet on steroids
- unused # Detect unused constants, variables, functions, and types
- gocyclo # Detect cyclomatic complexity
- gofmt
- goimports
- misspell # Fix spelling errors
- bodyclose # Check whether HTTP response bodies are closed
- misspell # Fix spelling errors
- bodyclose # Check whether HTTP response bodies are closed

linters-settings:
gofumpt:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ COPY --from=builder /app/entrypoint.sh /dojo-api/entrypoint.sh

EXPOSE 8080

ENTRYPOINT ["/dojo-api/entrypoint.sh"]
ENTRYPOINT ["/dojo-api/entrypoint.sh"]
3 changes: 2 additions & 1 deletion cmd/aws/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"net/url"

"dojo-api/pkg/orm"
"dojo-api/utils"
"net/url"

"github.com/rs/zerolog/log"
)
Expand Down
56 changes: 0 additions & 56 deletions cmd/blockchain/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/seed/fixtures/fixture_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (o *FixtureService) CreateDefaultTask(ctx context.Context, title string, ex
db.Task.Type.Set(db.TaskTypeCodeGeneration),
db.Task.TaskData.Set(types.JSON(taskDataJSON)),
db.Task.Status.Set(db.TaskStatusInProgress),
db.Task.MaxResults.Set(10),
db.Task.MaxResults.Set(1),
db.Task.NumResults.Set(0),
db.Task.NumCriteria.Set(0),
db.Task.TotalReward.Set(101.0),
Expand Down
15 changes: 5 additions & 10 deletions cmd/seed/task_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
"max": 100,
"min": 1,
"type": "multi-score",
"options": [
"459520f8-b654-41a8-9f94-93e003c4ecb5",
"024ffca6-aac0-40d2-959b-1ab7b165b68e",
"4fa05b97-cd66-4df7-bb3a-298d2b04b148",
"1b26f82c-935f-432b-a60d-f21204e580f2"
]
"options": ["1", "2", "3", "4"]
}
],
"responses": [
{
"model": "459520f8-b654-41a8-9f94-93e003c4ecb5",
"model": "1",
"completion": {
"files": [
{
Expand All @@ -29,7 +24,7 @@
}
},
{
"model": "024ffca6-aac0-40d2-959b-1ab7b165b68e",
"model": "2",
"completion": {
"files": [
{
Expand All @@ -42,7 +37,7 @@
}
},
{
"model": "4fa05b97-cd66-4df7-bb3a-298d2b04b148",
"model": "3",
"completion": {
"files": [
{
Expand All @@ -55,7 +50,7 @@
}
},
{
"model": "1b26f82c-935f-432b-a60d-f21204e580f2",
"model": "4",
"completion": {
"files": [
{
Expand Down
18 changes: 17 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ func main() {
loadEnvVars()
go continuouslyReadEnv()
go orm.NewTaskORM().UpdateExpiredTasks(context.Background())

runtimeEnv := utils.LoadDotEnv("RUNTIME_ENV")
if runtimeEnv == "aws" {
gin.SetMode(gin.ReleaseMode)
} else {
gin.SetMode(gin.DebugMode)
}

port := utils.LoadDotEnv("SERVER_PORT")
router := gin.Default()
// read allowedOrigins from environment variable which is a comma separated string
allowedOrigins := strings.Split(utils.LoadDotEnv("CORS_ALLOWED_ORIGINS"), ",")

Expand All @@ -46,7 +53,16 @@ func main() {
MaxAge: 12 * time.Hour,
AllowWildcard: true,
}

api.InitializeLimiters()
log.Info().Msg("Rate limiters initialized")

router := gin.New() // empty engine
router.Use(gin.Recovery()) // add recovery middleware
router.Use(api.CustomGinLogger(&log.Logger)) // add our custom gin logger

router.Use(cors.New(config))
router.ForwardedByClientIP = true
api.LoginRoutes(router)

if os.Getenv("RUNTIME_ENV") == "local" {
Expand Down
3 changes: 2 additions & 1 deletion cmd/siws/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"dojo-api/pkg/blockchain/siws"
"fmt"

"dojo-api/pkg/blockchain/siws"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

echo "Starting Dojo API"
./service
./service
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/gorilla/securecookie v1.1.2
github.com/joho/godotenv v1.5.1
github.com/redis/rueidis v1.0.36
github.com/redis/go-redis/v9 v9.7.0
github.com/rs/zerolog v1.32.0
github.com/shopspring/decimal v1.3.1
github.com/spruceid/siwe-go v0.2.1
github.com/steebchen/prisma-client-go v0.36.0
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
github.com/ulule/limiter/v3 v3.11.2
gopkg.in/mail.v2 v2.3.1
)

Expand All @@ -47,8 +48,10 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.28.7 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d // indirect
github.com/dchest/uniuri v1.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
Expand All @@ -63,6 +66,8 @@ require (
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/relvacode/iso8601 v1.4.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/sync v0.7.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
)
Expand Down
20 changes: 16 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/bits-and-blooms/bitset v1.10.0 h1:ePXTeiPEazB5+opbv5fr8umg2R/1NlzgDsyepwsSr88=
github.com/bits-and-blooms/bitset v1.10.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0=
github.com/btcsuite/btcd/btcec/v2 v2.3.3/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
Expand All @@ -69,6 +73,8 @@ github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
Expand All @@ -94,6 +100,8 @@ github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5il
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA=
github.com/ethereum/c-kzg-4844 v1.0.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/ethereum/go-ethereum v1.14.0 h1:xRWC5NlB6g1x7vNy4HDBLuqVNbtLrc7v8S6+Uxim1LU=
Expand Down Expand Up @@ -199,16 +207,14 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/rueidis v1.0.36 h1:LfyASusbTvxVQjX9yl4FsafJ8Bl8ZvCHTnqdH/McZQc=
github.com/redis/rueidis v1.0.36/go.mod h1:bnbkk4+CkXZgDPEbUtSos/o55i4RhFYYesJ4DS2zmq0=
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
github.com/relvacode/iso8601 v1.4.0 h1:GsInVSEJfkYuirYFxa80nMLbH2aydgZpIf52gYZXUJs=
github.com/relvacode/iso8601 v1.4.0/go.mod h1:FlNp+jz+TXpyRqgmM7tnzHHzBnz776kmAH2h3sZCn0I=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
Expand Down Expand Up @@ -253,6 +259,12 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/ulule/limiter/v3 v3.11.2 h1:P4yOrxoEMJbOTfRJR2OzjL90oflzYPPmWg+dvwN2tHA=
github.com/ulule/limiter/v3 v3.11.2/go.mod h1:QG5GnFOCV+k7lrL5Y8kgEeeflPH3+Cviqlqa8SVSQxI=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
Expand Down
34 changes: 12 additions & 22 deletions pkg/api/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"github.com/gorilla/securecookie"
"github.com/redis/rueidis"
"github.com/rs/zerolog/log"
"github.com/spruceid/siwe-go"
)
Expand Down Expand Up @@ -115,14 +114,6 @@ func WorkerLoginController(c *gin.Context) {
func CreateTasksController(c *gin.Context) {
log.Info().Msg("Creating Tasks")

// Log the headers of the request
headers := c.Request.Header
log.Info().Interface("headers", headers).Msg("Request headers")

// Log the size of the request
requestSize := c.Request.ContentLength
log.Info().Int64("requestSize", requestSize).Msg("Request size")

log.Debug().Interface("request body", c.Request.Body).Msg("Creating tasks with request body")

minerUserInterface, exists := c.Get("minerUser")
Expand Down Expand Up @@ -158,8 +149,6 @@ func CreateTasksController(c *gin.Context) {
return
}

log.Info().Str("minerUser", fmt.Sprintf("%+v", minerUser)).Msg("Miner user found")

// Here we will handle file upload
// Parse files from the form
form, err := c.MultipartForm()
Expand Down Expand Up @@ -306,6 +295,10 @@ func SubmitTaskResultController(c *gin.Context) {
return
}

// Remove from cache
cache := cache.GetCacheInstance()
cache.DeleteWithSuffix(cache.Keys.TaskResultByWorker, worker.ID)

// Update the metric data with goroutine
handleMetricData(taskData, updatedTask)

Expand Down Expand Up @@ -976,21 +969,18 @@ func GenerateCookieAuth(c *gin.Context) {
},
}

expirationTime := 5 * time.Minute
if err := cache.Redis.Do(
context.Background(),
cache.Redis.B().JsonSet().Key(encoded).Path("$").Value(rueidis.JSON(redisData)).Build(),
).Error(); err != nil {
log.Error().Err(err).Msg("Failed to store session in redis")
jsonData, err := json.Marshal(redisData)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal auth data...")
c.AbortWithStatusJSON(http.StatusInternalServerError, defaultErrorResponse("Failed to generate session"))
return
}

if err := cache.Redis.Do(
context.Background(),
cache.Redis.B().Expire().Key(encoded).Seconds(int64(expirationTime.Seconds())).Build(),
).Error(); err != nil {
log.Error().Err(err).Msg("Failed to set expiration time for session")
expirationTime := 5 * time.Minute
// migrating from rueidis to go-redis

if _, err := cache.Redis.Set(context.Background(), encoded, jsonData, expirationTime).Result(); err != nil {
log.Error().Err(err).Msg("Failed to store session in redis")
c.AbortWithStatusJSON(http.StatusInternalServerError, defaultErrorResponse("Failed to generate session"))
return
}
Expand Down
Loading

0 comments on commit e49b190

Please sign in to comment.