Skip to content

Commit

Permalink
Merge pull request #41 from OJ-lab/zztrans/main
Browse files Browse the repository at this point in the history
perf README.md & Support problem package import
  • Loading branch information
slhmy authored Mar 13, 2024
2 parents 952db6e + 6bc2540 commit e326c1e
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 83 deletions.
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ help:
@echo " run - Run the application"
@echo " setup-db - Setup the database"
@echo " clear-db - Clear the database"

@echo " pkg - Setup the database with problem packages"
@echo " all - Get front dist, Setup-db, read pkg and Run"

.PHONY: build
build: gen-swagger gen-proto
@echo "Building on $(OS)"
go mod tidy
go build -o artifacts/bin/migrate_db src/application/migrate_db/main.go
go build -o artifacts/bin/init_db src/application/init_db/main.go
go build -o artifacts/bin/service src/application/server/main.go
go build -o artifacts/bin/schedule src/application/schedule/main.go
go build -o artifacts/bin/read_pkg src/application/read_pkg/main.go

.PHONY: build-image
build-image:
Expand All @@ -34,7 +37,7 @@ setup-db: clear-db build
docker-compose -f environment/docker-compose.yml -p oj-lab-platform up -d
@echo "Wait 10 seconds for db setup"
sleep 10s
./artifacts/bin/migrate_db
./artifacts/bin/init_db

.PHONY: gen-swagger
gen-swagger: install-swaggo
Expand All @@ -49,6 +52,10 @@ check: gen-proto
test: gen-swagger check setup-db
go test -cover -v -count=1 ./...

.PHONY: pkg
pkg: setup-db
./artifacts/bin/read_pkg

.PHONY: run-schedule
run-schedule: build check
./artifacts/bin/schedule
Expand All @@ -61,6 +68,10 @@ run-server: build check
run: build check
make -j run-server run-schedule

.PHONY: all
all: get-front pkg
./artifacts/bin/service

.PHONY: get-front
get-front:
./scripts/update-frontend-dist.sh artifacts/oj-lab-front/dist
Expand Down
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,32 @@ Currently the backend server for OJ Lab.
For service development, we don't want to make it too complex.
Using VSCode on either Win/*nix System are avaliabe, try using the Makefile/Dockerfile in the repository.

### Run Processes
### Serve Frontend

Use `make get-front` to get the frontend dist codes.
You should also set `service.serve_front` to `true` in config file
(see [override.example.toml](environment/configs/override.example.toml) for more information)

### Init Database

Check config in `environment/configs`.

```bash
make run
# To initialize the minio config
cp environment/rclone-minio.example.conf environment/rclone-minio.conf
```

## Serve Frontend
Use `make pkg` to init database with **package** data.

Use `make get-front` to get the frontend dist codes.
You should also set `service.serve_front` to `true` in config file
(see [override.example.toml](configs/override.example.toml) for more information)
While `make setup-db` for **no data** but just table and user init.

### Run Processes

### WARNING
```bash
make run
```

You should close `remote.autoForwardPorts` if you are using online VSCode
`make all` do previous things except **config** in one line.

### Troubleshooting

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.2
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
2 changes: 1 addition & 1 deletion scripts/update-frontend-dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ else
mkdir -p "$FRONTEND_DIST_PATH"
fi

curl -o dist.zip -L https://github.com/OJ-lab/oj-lab-front/releases/download/v0.0.1/dist.zip
curl -o dist.zip -L https://github.com/OJ-lab/oj-lab-front/releases/download/v0.0.2/dist.zip
unzip -o dist.zip -d "$FRONTEND_DIST_PATH"
rm dist.zip
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,6 @@ func main() {
panic("failed to migrate database")
}

description := `Write a program that prints "Hello World!".`
mapper.CreateProblem(db, model.Problem{
Slug: "hello-world",
Title: "Hello World!",
Description: &description,
Tags: []*model.AlgorithmTag{
{Name: "Primer"},
},
})

description = `Calculate A + B, print the result.`
mapper.CreateProblem(db, model.Problem{
Slug: "a-plus-b",
Title: "A + B",
Description: &description,
Tags: []*model.AlgorithmTag{
{Name: "Primer"},
{Name: "Math"},
},
})

mapper.CreateUser(db, model.User{
Name: "admin",
Account: "admin",
Expand All @@ -53,5 +32,5 @@ func main() {
},
})

core.AppLogger().Info("migrate tables success")
core.AppLogger().Info("migrate tables ans users success")
}
107 changes: 107 additions & 0 deletions src/application/read_pkg/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"context"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"

gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm"
minioAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/minio"
"github.com/OJ-lab/oj-lab-services/src/service/mapper"
"github.com/OJ-lab/oj-lab-services/src/service/model"
yaml "gopkg.in/yaml.v2"

"github.com/minio/minio-go/v7"
)

var ctx = context.Background()

func main() {
db := gormAgent.GetDefaultDB()
minioClient := minioAgent.GetMinioClient()

log.Printf("%#v\n", minioClient) // minioClient is now set up
bucketName := minioAgent.GetBucketName()

err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists == nil && exists {
log.Printf("We already own %s\n", bucketName)
} else {
log.Fatalln(err)
}
} else {
log.Printf("Successfully created %s\n", bucketName)
}

// Read package files
// Search Problem under packagePath
// 1. parse problem path as `slug`,
// parse problem.yaml's name as `title`,
// parse problem.md as description.
// 2. insert object into minio storage.
packagePath := "tests/data/packages/icpc"
title := ""
slug := ""
filepath.Walk(packagePath, func(path string, info fs.FileInfo, err error) error {
if info == nil {
return fmt.Errorf("file info is nil")
}
if info.IsDir() {
return nil
}
relativePath := strings.Replace(path, packagePath, "", 1)
println("relativePath: ", relativePath)
if filepath.Base(relativePath) == "problem.yaml" {
resultMap := make(map[string]interface{})
yamlFile, err := os.ReadFile(path)
if err != nil {
log.Println(err)
}
err = yaml.Unmarshal(yamlFile, &resultMap)
if err != nil {
log.Printf("Unmarshal: %v\n", err)
}
title = resultMap["name"].(string)
if title == "" {
log.Fatal("name key not exist in problem.yaml")
}
slug = strings.Split(relativePath, "/")[1]
log.Println("title: ", title)
log.Println("slug: ", slug)
}
if filepath.Base(relativePath) == "problem.md" {
content, err := os.ReadFile(path)
if err != nil {
log.Println(err)
}
description := string(content)
println("description: ", description)
mapper.CreateProblem(db, model.Problem{
Slug: slug,
Title: title,
Description: &description,
Tags: []*model.AlgorithmTag{
{Name: "to-be-add"},
},
})
}

_, minioErr := minioClient.FPutObject(ctx, bucketName,
relativePath,
path,
minio.PutObjectOptions{})
if minioErr != nil {
log.Fatalln(minioErr)
}
return err
})

log.Println("Read Problem Success!")
}
7 changes: 6 additions & 1 deletion src/service/mapper/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/OJ-lab/oj-lab-services/src/service/model"
"github.com/alexedwards/argon2id"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

// Account, Password, Roles will be used to create a new user.
Expand Down Expand Up @@ -45,7 +46,11 @@ func GetPublicUser(tx *gorm.DB, account string) (*model.User, error) {
}

func DeleteUser(tx *gorm.DB, user model.User) error {
return tx.Delete(&model.User{Account: user.Account}).Error
if err := tx.Where("user_account = ?", user.Account).Delete(&model.JudgeTaskSubmission{}).Error; err != nil {
return err
}

return tx.Select(clause.Associations).Delete(&model.User{Account: user.Account}).Error
}

func UpdateUser(tx *gorm.DB, update model.User) error {
Expand Down
46 changes: 0 additions & 46 deletions tests/core/minio_test.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,9 @@
package core_test

import (
"fmt"
"io/fs"
"log"
"path/filepath"
"strings"
"testing"

minioAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/minio"
"github.com/minio/minio-go/v7"
)

func TestMinio(T *testing.T) {
// Initialize minio client object.
minioClient := minioAgent.GetMinioClient()

log.Printf("%#v\n", minioClient) // minioClient is now set up
bucketName := minioAgent.GetBucketName()

err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists == nil && exists {
log.Printf("We already own %s\n", bucketName)
} else {
log.Fatalln(err)
}
} else {
log.Printf("Successfully created %s\n", bucketName)
}

// Upload package files
packagePath := "../data/packages/icpc/hello-world"
filepath.Walk(packagePath, func(path string, info fs.FileInfo, err error) error {
if info == nil {
return fmt.Errorf("file info is nil")
}
if info.IsDir() {
return nil
}
relativePath := filepath.Join(filepath.Base(packagePath), strings.Replace(path, packagePath, "", 1))
println(relativePath)
_, minioErr := minioClient.FPutObject(ctx, bucketName,
relativePath,
path,
minio.PutObjectOptions{})
if minioErr != nil {
log.Fatalln(minioErr)
}
return minioErr
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write a program that prints "Hello World!".
6 changes: 6 additions & 0 deletions tests/mapper/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ func TestProblemMapper(t *testing.T) {
t.Error(err)
}
fmt.Printf("%+v\n", string(problemListJson))

err = mapper.DeleteProblem(db, problem.Slug)
if err != nil {
t.Error(err)
}

}
8 changes: 7 additions & 1 deletion tests/mapper/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestUserMapper(t *testing.T) {
user := model.User{
Account: "test",
Password: func() *string { s := "test"; return &s }(),
Roles: []*model.Role{{Name: "admin"}},
Roles: []*model.Role{{Name: "test"}},
}
err := mapper.CreateUser(db, user)
if err != nil {
Expand All @@ -41,4 +41,10 @@ func TestUserMapper(t *testing.T) {
t.Error(err)
}
fmt.Printf("%+v\n", string(publicUserJson))

err = mapper.DeleteUser(db, user)
if err != nil {
t.Error(err)
}

}

0 comments on commit e326c1e

Please sign in to comment.