diff --git a/cmd/app/run.go b/cmd/app/run.go index 0da7fbc..96b23bd 100644 --- a/cmd/app/run.go +++ b/cmd/app/run.go @@ -1,8 +1,8 @@ package main import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/app" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/urfave/cli/v2" "math/rand" "time" diff --git a/config.yaml b/config.yaml index 8663f6a..de46fd7 100644 --- a/config.yaml +++ b/config.yaml @@ -6,6 +6,7 @@ WebServer: Redis: Db: 0 + QueueDb: 0 Address: '127.0.0.1:6379' Password: '' diff --git a/internal/global/config.go b/global/config.go similarity index 97% rename from internal/global/config.go rename to global/config.go index aa1083e..e478dee 100644 --- a/internal/global/config.go +++ b/global/config.go @@ -9,6 +9,7 @@ type ConfigWebServer struct { type ConfigRedis struct { Db int `yaml:"Db"` + QueueDb int `yaml:"QueueDb"` Address string `yaml:"Address"` Password string `yaml:"Password"` } diff --git a/internal/global/global.go b/global/global.go similarity index 100% rename from internal/global/global.go rename to global/global.go diff --git a/internal/global/jwt.go b/global/jwt.go similarity index 100% rename from internal/global/jwt.go rename to global/jwt.go diff --git a/internal/global/repo.go b/global/repo.go similarity index 100% rename from internal/global/repo.go rename to global/repo.go diff --git a/internal/global/router.go b/global/router.go similarity index 100% rename from internal/global/router.go rename to global/router.go diff --git a/internal/global/setup.go b/global/setup.go similarity index 100% rename from internal/global/setup.go rename to global/setup.go diff --git a/go.mod b/go.mod index 9b4c2d7..79d3d95 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gin-gonic/gin v1.8.1 github.com/go-redis/redis/v8 v8.11.5 github.com/golang-jwt/jwt/v4 v4.4.2 + github.com/hibiken/asynq v0.23.0 github.com/lib/pq v1.10.2 github.com/prometheus/client_golang v1.13.0 github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a @@ -42,7 +43,6 @@ require ( github.com/goccy/go-json v0.9.7 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/hibiken/asynq v0.23.0 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgconn v1.12.1 // indirect github.com/jackc/pgio v1.0.0 // indirect diff --git a/go.sum b/go.sum index 52a4312..d858d78 100644 --- a/go.sum +++ b/go.sum @@ -80,6 +80,7 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= @@ -439,9 +440,8 @@ go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI= go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= -go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= @@ -616,8 +616,6 @@ golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI= golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= diff --git a/internal/api/debug/handler.go b/internal/api/debug/handler.go index 23ae47e..d5d9e85 100644 --- a/internal/api/debug/handler.go +++ b/internal/api/debug/handler.go @@ -1,7 +1,7 @@ package debug import ( - "github.com/WHUPRJ/woj-server/internal/global" + "github.com/WHUPRJ/woj-server/global" "github.com/gin-gonic/gin" "go.uber.org/zap" ) diff --git a/internal/api/problem/handler.go b/internal/api/problem/handler.go index 12378f8..08fb97f 100644 --- a/internal/api/problem/handler.go +++ b/internal/api/problem/handler.go @@ -1,7 +1,7 @@ package problem import ( - "github.com/WHUPRJ/woj-server/internal/global" + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/service/problem" "github.com/gin-gonic/gin" "go.uber.org/zap" diff --git a/internal/api/problem/update.go b/internal/api/problem/update.go index 11550c1..68e2d85 100644 --- a/internal/api/problem/update.go +++ b/internal/api/problem/update.go @@ -1,8 +1,8 @@ package problem import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/WHUPRJ/woj-server/model" "github.com/gin-gonic/gin" ) diff --git a/internal/api/user/create.go b/internal/api/user/create.go index 8e2dc25..5149ee0 100644 --- a/internal/api/user/create.go +++ b/internal/api/user/create.go @@ -1,8 +1,8 @@ package user import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/WHUPRJ/woj-server/internal/service/user" "github.com/gin-gonic/gin" ) diff --git a/internal/api/user/handler.go b/internal/api/user/handler.go index 1dcc8b5..e05fba1 100644 --- a/internal/api/user/handler.go +++ b/internal/api/user/handler.go @@ -1,7 +1,7 @@ package user import ( - "github.com/WHUPRJ/woj-server/internal/global" + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/service/user" "github.com/gin-gonic/gin" "go.uber.org/zap" diff --git a/internal/api/user/login.go b/internal/api/user/login.go index b922962..40336f6 100644 --- a/internal/api/user/login.go +++ b/internal/api/user/login.go @@ -1,8 +1,8 @@ package user import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/WHUPRJ/woj-server/model" "github.com/gin-gonic/gin" ) diff --git a/internal/api/user/logout.go b/internal/api/user/logout.go index f9a288c..015759a 100644 --- a/internal/api/user/logout.go +++ b/internal/api/user/logout.go @@ -1,8 +1,8 @@ package user import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/gin-gonic/gin" ) diff --git a/internal/api/user/profile.go b/internal/api/user/profile.go index 90e8904..cd14ec9 100644 --- a/internal/api/user/profile.go +++ b/internal/api/user/profile.go @@ -1,8 +1,8 @@ package user import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/WHUPRJ/woj-server/model" "github.com/gin-gonic/gin" ) diff --git a/internal/app/app.go b/internal/app/app.go index 58139f5..9e77a21 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -3,11 +3,11 @@ package app import ( "context" "fmt" - "github.com/WHUPRJ/woj-server/internal/global" - "github.com/WHUPRJ/woj-server/internal/repo/postgresql" - "github.com/WHUPRJ/woj-server/internal/repo/redis" + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/router" "github.com/WHUPRJ/woj-server/internal/service/jwt" + "github.com/WHUPRJ/woj-server/repo/postgresql" + "github.com/WHUPRJ/woj-server/repo/redis" "go.uber.org/zap" "net/http" "os" diff --git a/internal/router/api.go b/internal/router/api.go index 80fab98..1e88076 100644 --- a/internal/router/api.go +++ b/internal/router/api.go @@ -1,10 +1,10 @@ package router import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/api/debug" "github.com/WHUPRJ/woj-server/internal/api/problem" "github.com/WHUPRJ/woj-server/internal/api/user" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/gin-gonic/gin" ) diff --git a/internal/router/router.go b/internal/router/router.go index a5669f2..f03bb88 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -1,7 +1,7 @@ package router import ( - "github.com/WHUPRJ/woj-server/internal/global" + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/pkg/metrics" _ "github.com/WHUPRJ/woj-server/internal/router/docs" "github.com/WHUPRJ/woj-server/pkg/utils" diff --git a/internal/service/jwt/service.go b/internal/service/jwt/service.go index 3cb372d..36b1d99 100644 --- a/internal/service/jwt/service.go +++ b/internal/service/jwt/service.go @@ -1,7 +1,7 @@ package jwt import ( - "github.com/WHUPRJ/woj-server/internal/global" + "github.com/WHUPRJ/woj-server/global" "github.com/go-redis/redis/v8" "go.uber.org/zap" ) diff --git a/internal/service/jwt/token.go b/internal/service/jwt/token.go index 876757e..5deaa17 100644 --- a/internal/service/jwt/token.go +++ b/internal/service/jwt/token.go @@ -3,8 +3,8 @@ package jwt import ( "context" "fmt" + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/WHUPRJ/woj-server/pkg/utils" "github.com/golang-jwt/jwt/v4" "go.uber.org/zap" diff --git a/internal/service/problem/service.go b/internal/service/problem/service.go index 584117a..b9c3e4a 100644 --- a/internal/service/problem/service.go +++ b/internal/service/problem/service.go @@ -1,8 +1,8 @@ package problem import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/WHUPRJ/woj-server/model" "go.uber.org/zap" "gorm.io/gorm" diff --git a/internal/service/task/common.go b/internal/service/task/common.go index 3ceb80a..3629330 100644 --- a/internal/service/task/common.go +++ b/internal/service/task/common.go @@ -21,7 +21,7 @@ func (s *service) submit(typename string, payload []byte) (*asynq.TaskInfo, e.St } func (s *service) GetTaskInfo(id string) (*asynq.TaskInfo, e.Status) { - task, err := s.inspector.GetTaskInfo("", id) + task, err := s.inspector.GetTaskInfo("default", id) if err != nil { s.log.Debug("get task info failed", zap.Error(err), zap.String("id", id)) return nil, e.TaskGetInfoFailed diff --git a/internal/service/task/judge.go b/internal/service/task/judge.go index 2623414..12df05f 100644 --- a/internal/service/task/judge.go +++ b/internal/service/task/judge.go @@ -7,14 +7,14 @@ import ( "go.uber.org/zap" ) -func (s *service) NewJudge(submission model.Submission) e.Status { +func (s *service) NewJudge(submission model.Submission) (string, e.Status) { payload, err := json.Marshal(model.SubmitJudge{Submission: submission}) if err != nil { s.log.Warn("json marshal error", zap.Error(err), zap.Any("payload", submission)) - return e.InternalError + return "", e.InternalError } - _, status := s.submit(model.TypeSubmitJudge, payload) + info, status := s.submit(model.TypeSubmitJudge, payload) - return status + return info.ID, status } diff --git a/internal/service/task/push.go b/internal/service/task/push.go new file mode 100644 index 0000000..b6026af --- /dev/null +++ b/internal/service/task/push.go @@ -0,0 +1,23 @@ +package task + +import ( + "encoding/json" + "github.com/WHUPRJ/woj-server/internal/e" + "github.com/WHUPRJ/woj-server/model" + "go.uber.org/zap" +) + +func (s *service) PushProblem(id uint, file string) (string, e.Status) { + payload, err := json.Marshal(model.ProblemPushPayload{ + ProblemID: id, + ProblemFile: file, + }) + if err != nil { + s.log.Warn("json marshal error", zap.Error(err), zap.Any("id", id), zap.String("file", file)) + return "", e.InternalError + } + + info, status := s.submit(model.TypeSubmitJudge, payload) + + return info.ID, status +} diff --git a/internal/service/task/service.go b/internal/service/task/service.go index 679bb3a..8b104b5 100644 --- a/internal/service/task/service.go +++ b/internal/service/task/service.go @@ -1,8 +1,8 @@ package task import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/WHUPRJ/woj-server/model" "github.com/hibiken/asynq" "go.uber.org/zap" @@ -11,7 +11,9 @@ import ( var _ Service = (*service)(nil) type Service interface { - NewJudge(submission model.Submission) e.Status + NewJudge(submission model.Submission) (string, e.Status) + PushProblem(id uint, file string) (string, e.Status) + GetTaskInfo(id string) (*asynq.TaskInfo, e.Status) submit(typename string, payload []byte) (*asynq.TaskInfo, e.Status) } @@ -25,7 +27,7 @@ func NewService(g *global.Global) Service { redisOpt := asynq.RedisClientOpt{ Addr: g.Conf.Redis.Address, Password: g.Conf.Redis.Password, - DB: g.Conf.Redis.Db, + DB: g.Conf.Redis.QueueDb, } return &service{ log: g.Log, diff --git a/internal/service/user/service.go b/internal/service/user/service.go index 00b83ba..4f38ef0 100644 --- a/internal/service/user/service.go +++ b/internal/service/user/service.go @@ -1,8 +1,8 @@ package user import ( + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/internal/e" - "github.com/WHUPRJ/woj-server/internal/global" "github.com/WHUPRJ/woj-server/model" "github.com/go-redis/redis/v8" "go.uber.org/zap" diff --git a/model/Problem.go b/model/Problem.go index 2e2de22..458daf2 100644 --- a/model/Problem.go +++ b/model/Problem.go @@ -15,5 +15,6 @@ type Problem struct { Provider User `json:"-" gorm:"foreignKey:ProviderID"` Languages pq.Int32Array `json:"languages" gorm:"type:int[]"` Points pq.Int32Array `json:"points" gorm:"type:int[]"` + StorageKey string `json:"storage_key" gorm:"not null"` IsEnabled bool `json:"is_enabled" gorm:"not null;index"` } diff --git a/model/Task.go b/model/Task.go index 35b3694..453ea8a 100644 --- a/model/Task.go +++ b/model/Task.go @@ -1,17 +1,12 @@ package model const ( - TypeProblemResolve = "problem:resolve" - TypeProblemPush = "problem:push" - TypeSubmitJudge = "submit:judge" + TypeProblemPush = "problem:push" + TypeSubmitJudge = "submit:judge" ) -type ProblemResolvePayload struct { - ProblemID uint - ProblemFile string -} - type ProblemPushPayload struct { + ProblemID uint ProblemFile string } diff --git a/internal/repo/postgresql/postgresql.go b/repo/postgresql/postgresql.go similarity index 97% rename from internal/repo/postgresql/postgresql.go rename to repo/postgresql/postgresql.go index a5f8bc4..a99d57d 100644 --- a/internal/repo/postgresql/postgresql.go +++ b/repo/postgresql/postgresql.go @@ -4,7 +4,7 @@ import ( "database/sql" "errors" "fmt" - "github.com/WHUPRJ/woj-server/internal/global" + "github.com/WHUPRJ/woj-server/global" "github.com/WHUPRJ/woj-server/model" "go.uber.org/zap" "gorm.io/driver/postgres" diff --git a/internal/repo/redis/redis.go b/repo/redis/redis.go similarity index 93% rename from internal/repo/redis/redis.go rename to repo/redis/redis.go index d23da71..1058f4d 100644 --- a/internal/repo/redis/redis.go +++ b/repo/redis/redis.go @@ -2,7 +2,7 @@ package redis import ( "context" - "github.com/WHUPRJ/woj-server/internal/global" + "github.com/WHUPRJ/woj-server/global" "github.com/go-redis/redis/v8" "go.uber.org/zap" )