Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gen-swagger: install-swaggo
gen-proto: install-proto
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
proto/*.proto
proto/*.proto


# Targets for development
Expand All @@ -70,7 +70,7 @@ setup-dependencies: build get-front get-problem-packages
./bin/clean; \
else \
docker compose up -d postgres redis minio; \
@echo "Wait 10 seconds for db setup"; \
echo "Wait 10 seconds for db setup"; \
sleep 10s; \
fi
./bin/init;
Expand Down
17 changes: 8 additions & 9 deletions cmd/clean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"log/slog"

"github.com/minio/minio-go/v7"
judge_model "github.com/oj-lab/platform/models/judge"
Expand All @@ -11,14 +12,12 @@ import (
gorm_agent "github.com/oj-lab/platform/modules/agent/gorm"
minio_agent "github.com/oj-lab/platform/modules/agent/minio"
redis_agent "github.com/oj-lab/platform/modules/agent/redis"

log_module "github.com/oj-lab/platform/modules/log"
)

func clearCasbin() {
enforcer := casbin_agent.GetDefaultCasbinEnforcer()
enforcer.ClearPolicy() // no err return
log_module.AppLogger().Info("Clear Casbin success")
slog.Info("Clear Casbin success")
}

func removeMinioObjects() {
Expand All @@ -30,29 +29,29 @@ func removeMinioObjects() {
opts := minio.ListObjectsOptions{Recursive: true}
for object := range minioClient.ListObjects(context.Background(), minio_agent.GetBucketName(), opts) {
if object.Err != nil {
log_module.AppLogger().WithError(object.Err).Error("Get object error")
slog.With("err", object.Err).Error("Get object error")
}
objectsCh <- object
}
}()

errorCh := minioClient.RemoveObjects(context.Background(), minio_agent.GetBucketName(), objectsCh, minio.RemoveObjectsOptions{})
for e := range errorCh {
log_module.AppLogger().WithError(e.Err).Error("Failed to remove " + e.ObjectName)
slog.With("err", e.Err).Error("Failed to remove " + e.ObjectName)
}

log_module.AppLogger().Info("Remove Minio Objects success")
slog.Info("Remove Minio Objects success")
}

func clearRedis() {
ctx := context.Background()
redis_agent := redis_agent.GetDefaultRedisClient()
err := redis_agent.FlushDB(ctx).Err()
if err != nil {
log_module.AppLogger().WithError(err).Error("Failed to clear redis")
slog.With("err", err).Error("Failed to clear redis")
}

log_module.AppLogger().Info("Clear Redis success")
slog.Info("Clear Redis success")
}
func clearDB() {
db := gorm_agent.GetDefaultDB()
Expand All @@ -73,7 +72,7 @@ func clearDB() {
panic("failed to drop tables")
}

log_module.AppLogger().Info("Clear DB success")
slog.Info("Clear DB success")
}

func main() {
Expand Down
10 changes: 5 additions & 5 deletions cmd/init/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package main

import (
"fmt"
"log/slog"

judge_model "github.com/oj-lab/platform/models/judge"
problem_model "github.com/oj-lab/platform/models/problem"
user_model "github.com/oj-lab/platform/models/user"
gorm_agent "github.com/oj-lab/platform/modules/agent/gorm"
config_module "github.com/oj-lab/platform/modules/config"
log_module "github.com/oj-lab/platform/modules/log"
core_module "github.com/oj-lab/platform/modules/core"
)

const rootPasswordProp = "auth.root_password"
const rootPasswordConfigKey = "auth.root_password"

func initDB() {
rootPassword := config_module.AppConfig().GetString(rootPasswordProp)
rootPassword := core_module.Config.GetString(rootPasswordConfigKey)
db := gorm_agent.GetDefaultDB()
err := db.AutoMigrate(
&user_model.User{},
Expand Down Expand Up @@ -47,5 +47,5 @@ func initDB() {
panic(fmt.Sprintf("failed to create anonymous user: %v", err))
}

log_module.AppLogger().Info("migrate tables ans users success")
slog.Info("migrate tables ans users success")
}
48 changes: 24 additions & 24 deletions cmd/init/problem_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/fs"
"log/slog"
"os"
"path"
"path/filepath"
Expand All @@ -15,21 +16,20 @@ import (
problem_model "github.com/oj-lab/platform/models/problem"
gorm_agent "github.com/oj-lab/platform/modules/agent/gorm"
minio_agent "github.com/oj-lab/platform/modules/agent/minio"
config_module "github.com/oj-lab/platform/modules/config"
log_module "github.com/oj-lab/platform/modules/log"
core_module "github.com/oj-lab/platform/modules/core"
"gopkg.in/yaml.v2"
)

func loadProblemPackages(ctx context.Context) {
db := gorm_agent.GetDefaultDB()
minioClient := minio_agent.GetMinioClient()

packagePath := path.Join(config_module.ProjectRoot(), "problem-packages/icpc")
packagePath := path.Join(core_module.ProjectRoot(), "problem-packages/icpc")

// Load Dirs under `packagePath`
problemPackageDirs, err := os.ReadDir(packagePath)
if err != nil {
log_module.AppLogger().WithError(err).Error("Read package path failed")
slog.With("err", err).Error("Read package path failed")
panic(err)
}
for _, problemPackageDir := range problemPackageDirs {
Expand All @@ -51,7 +51,7 @@ func loadProblemPackages(ctx context.Context) {
problemPackagePath := path.Join(packagePath, problemPackageDir.Name())
err := filepath.Walk(problemPackagePath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
log_module.AppLogger().WithError(err).Error("Walk package path failed")
slog.With("err", err).Error("Walk package path failed")
return err
}
if info == nil {
Expand All @@ -61,33 +61,33 @@ func loadProblemPackages(ctx context.Context) {
return nil
}
relativePath := strings.Replace(path, packagePath, "", 1)
log_module.AppLogger().
WithField("relativePath", relativePath).
WithField("Ext", filepath.Ext(relativePath)).
WithField("Dir", filepath.Dir(relativePath)).
slog.
With("relativePath", relativePath).
With("Ext", filepath.Ext(relativePath)).
With("Dir", filepath.Dir(relativePath)).
Debug("Read file from package")

if filepath.Base(relativePath) == "problem.yaml" {
resultMap := make(map[string]interface{})
yamlFile, err := os.ReadFile(path)
if err != nil {
log_module.AppLogger().WithError(err).Error("Read problem.yaml failed")
slog.With("err", err).Error("Read problem.yaml failed")
}
err = yaml.Unmarshal(yamlFile, &resultMap)
if err != nil {
log_module.AppLogger().WithError(err).Error("Unmarshal problem.yaml failed")
slog.With("err", err).Error("Unmarshal problem.yaml failed")
}
log_module.AppLogger().WithField("resultMap", reflect.TypeOf(resultMap["limits"])).Debug("Read problem.yaml")
slog.With("resultMap", reflect.TypeOf(resultMap["limits"])).Debug("Read problem.yaml")
if resultMap["name"] == nil {
log_module.AppLogger().Error("Problem name is nil")
slog.Error("Problem name is nil")
return nil
}
title = resultMap["name"].(string)
if title == "" {
log_module.AppLogger().Error("Problem title is empty")
slog.Error("Problem title is empty")
}
slug = strings.Split(relativePath, "/")[1]
log_module.AppLogger().WithField("title", title).WithField("slug", slug).Debug("Read problem.yaml")
slog.With("title", title).With("slug", slug).Debug("Read problem.yaml")
if limits, ok := resultMap["limits"].(map[interface{}]interface{}); ok {
if memoryLimit, ok := limits["memory"].(int); ok {
limitDescription += fmt.Sprintf("<center>Memory Limit: %d MB</center>\n", memoryLimit)
Expand All @@ -112,39 +112,39 @@ func loadProblemPackages(ctx context.Context) {
if filepath.Base(relativePath) == "problem.md" {
content, err := os.ReadFile(path)
if err != nil {
log_module.AppLogger().WithError(err).Error("Read problem.md failed")
slog.With("err", err).Error("Read problem.md failed")
}
description = string(content)
log_module.AppLogger().WithField("description", description).Debug("Read problem.md")
slog.With("description", description).Debug("Read problem.md")
}
if filepath.Base(relativePath) == ".timelimit" {
timeLimitStr, err := os.ReadFile(path)
if err != nil {
log_module.AppLogger().WithError(err).Error("Read time limit file failed")
slog.With("err", err).Error("Read time limit file failed")
return nil
}
timeLimit, err := strconv.Atoi(strings.Trim(string(timeLimitStr), "\n"))
if err != nil {
log_module.AppLogger().WithError(err).Error("Parse time limit failed")
slog.With("err", err).Error("Parse time limit failed")
return nil
}
limitDescription += fmt.Sprintf("<center>Time Limit: %d s</center>\n", timeLimit)
}
if filepath.Ext(relativePath) == ".in" && strings.HasSuffix(filepath.Dir(relativePath), "sample") {
ansPath := strings.Replace(path, ".in", ".ans", 1)
if _, err := os.Stat(ansPath); err != nil {
log_module.AppLogger().WithField("path", ansPath).Error("Answer file not found")
slog.With("path", ansPath).Error("Answer file not found")
return nil
}
input, err := os.ReadFile(path)
if err != nil {
log_module.AppLogger().WithError(err).Error("Read input file failed")
slog.With("err", err).Error("Read input file failed")
return nil
}
inputStr := strings.Trim(string(input), "\n")
output, err := os.ReadFile(ansPath)
if err != nil {
log_module.AppLogger().WithError(err).Error("Read output file failed")
slog.With("err", err).Error("Read output file failed")
return nil
}
outputStr := strings.Trim(string(output), "\n")
Expand All @@ -159,7 +159,7 @@ func loadProblemPackages(ctx context.Context) {
path,
minio.PutObjectOptions{})
if err != nil {
log_module.AppLogger().WithError(err).Error("Put object to minio failed")
slog.With("err", err).Error("Put object to minio failed")
}
return err
})
Expand Down Expand Up @@ -196,5 +196,5 @@ func loadProblemPackages(ctx context.Context) {
}
}

log_module.AppLogger().Info("Problem loaded")
slog.Info("Problem loaded")
}
6 changes: 3 additions & 3 deletions cmd/rpc_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"net"

"github.com/oj-lab/platform/cmd/rpc_server/impls"
config_module "github.com/oj-lab/platform/modules/config"
core_module "github.com/oj-lab/platform/modules/core"
"github.com/oj-lab/platform/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

const (
portProp = "rpc-server.port"
portConfigKey = "rpc-server.port"
)

var (
port = config_module.AppConfig().GetInt(portProp)
port = core_module.Config.GetInt(portConfigKey)
)

func main() {
Expand Down
12 changes: 6 additions & 6 deletions cmd/web_server/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"fmt"
"log/slog"
"net/http"
"time"

Expand All @@ -11,21 +12,20 @@ import (
user_model "github.com/oj-lab/platform/models/user"
gorm_agent "github.com/oj-lab/platform/modules/agent/gorm"
auth_module "github.com/oj-lab/platform/modules/auth"
config_module "github.com/oj-lab/platform/modules/config"
log_module "github.com/oj-lab/platform/modules/log"
core_module "github.com/oj-lab/platform/modules/core"
gin_utils "github.com/oj-lab/platform/modules/utils/gin"
user_service "github.com/oj-lab/platform/services/user"
)

const (
emailBacklistProp = "auth.email_backlist"
callbackURL = "/auth/github/callback"
emailBacklistConfigKey = "auth.email_backlist"
callbackURL = "/auth/github/callback"
)

var emailBacklist = map[string]bool{}

func init() {
emailBacklistSlice := config_module.AppConfig().GetStringSlice(emailBacklistProp)
emailBacklistSlice := core_module.Config.GetStringSlice(emailBacklistConfigKey)
for _, email := range emailBacklistSlice {
emailBacklist[email] = true
}
Expand All @@ -52,7 +52,7 @@ func githubCallback(ginCtx *gin.Context) {
return
}

log_module.AppLogger().WithField("tokenResponse", tokenResponse).Info("github callback")
slog.With("tokenResponse", tokenResponse).Info("github callback")
githubUser, err := auth_module.GetGithubUser(tokenResponse.AccessToken)
if err != nil {
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get github user: %v", err))
Expand Down
4 changes: 2 additions & 2 deletions cmd/web_server/handler/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package handler
import (
"fmt"
"io"
"log/slog"
"time"

"github.com/gin-gonic/gin"
log_module "github.com/oj-lab/platform/modules/log"
)

func SetupEventRouter(baseRoute *gin.RouterGroup) {
Expand All @@ -33,7 +33,7 @@ func Stream(ginCtx *gin.Context) {
ginCtx.Stream(func(w io.Writer) bool {
// With event type
message := fmt.Sprintf("event: %s\ndata: %s\n\n", "eventType", time.Now().String())
log_module.AppLogger().Infof("Send message:\n%s", message)
slog.Info(fmt.Sprintf("Send message:\n%s", message))
fmt.Fprint(w, message)
time.Sleep(1 * time.Second)
counter++
Expand Down
4 changes: 2 additions & 2 deletions cmd/web_server/handler/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func checkProblemSlug(ginCtx *gin.Context) {
// PostJudgeBody
//
// @Description The body of a judge request, containing the code and the language used for the judge.
// @Property code (string) required "The source code of the judge" minlength(1)
// @Property language (ProgrammingLanguage) required "The programming language used for the judge"
// @ConfigKeyerty code (string) required "The source code of the judge" minlength(1)
// @ConfigKeyerty language (ProgrammingLanguage) required "The programming language used for the judge"
type PostJudgeBody struct {
Code string `json:"code" binding:"required"`
Language judge_model.ProgrammingLanguage `json:"language" binding:"required"`
Expand Down
8 changes: 4 additions & 4 deletions cmd/web_server/handler/swaggo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

const (
servicePortProp = "service.port"
serviceHostProp = "service.host"
servicePortConfigKey = "service.port"
serviceHostConfigKey = "service.host"
)

var (
Expand All @@ -24,8 +24,8 @@ func SetupSwaggoRouter(r *gin.RouterGroup) {
}

func init() {
sevicePort := viper.GetUint(servicePortProp)
seviceHost := viper.GetString(serviceHostProp)
sevicePort := viper.GetUint(servicePortConfigKey)
seviceHost := viper.GetString(serviceHostConfigKey)
swaggerHost = fmt.Sprintf("%s:%d", seviceHost, sevicePort)
println("Swagger host is set to: " + swaggerHost)
// programmatically set swagger info
Expand Down
Loading