Compare commits

..

No commits in common. "2d467c710fd0335d8f2814b92b0c4ef8aafe28d8" and "2e5aa77e25ee5f66bd3cf692187612ed7eba33e9" have entirely different histories.

16 changed files with 4 additions and 251 deletions

View File

@ -1,33 +0,0 @@
package api
import (
"github.com/gin-gonic/gin"
"online_code/core"
"online_code/models/param"
"online_code/repository"
"online_code/utils"
)
type problemApi struct{}
func ProblemApi() *problemApi {
return &problemApi{}
}
func (problemApi) GetProbleList(ctx *gin.Context) {
var p param.GetProblemList
if err := ctx.ShouldBind(&p); err != nil {
core.R(ctx).FailWithMessage("参数错误: " + err.Error())
return
}
records, count, err := repository.ProblemService().GetList(p)
if err != nil {
core.R(ctx).FailWithMessage("获取题目列表失败: " + err.Error())
return
}
// 计算总页码
totalPage := utils.GenTotalPage(count, p.Size)
// 返回结果
core.R(ctx).OkWithData(core.PageData{Current: p.Current, Size: p.Size, Total: count, TotalPage: totalPage, Records: records})
}

View File

@ -1,19 +0,0 @@
app:
port: 8083
name: online_code
version: v1
prefix: /api/v1
mysql:
host: 127.0.0.1
port: 3306
user: root
password: root
db: online_code
redis:
host: 127.0.0.1
port: 6379
password: loser123
db: 0

View File

@ -1,6 +0,0 @@
package define
var (
DefaultCurrent = "1"
DefaultSize = "10"
)

5
go.mod
View File

@ -5,14 +5,13 @@ go 1.18
require ( require (
git.echol.cn/loser/logger v1.0.14 git.echol.cn/loser/logger v1.0.14
git.echol.cn/loser/nacos-viper-remote v0.4.1-0.20220525104600-e38430672884 git.echol.cn/loser/nacos-viper-remote v0.4.1-0.20220525104600-e38430672884
gitee.ltd/lxh/logger v1.0.14
github.com/caarlos0/env/v6 v6.9.2 github.com/caarlos0/env/v6 v6.9.2
github.com/fsnotify/fsnotify v1.5.1 github.com/fsnotify/fsnotify v1.5.1
github.com/gin-gonic/gin v1.7.7 github.com/gin-gonic/gin v1.7.7
github.com/go-redis/redis/v8 v8.11.5 github.com/go-redis/redis/v8 v8.11.5
github.com/google/uuid v1.1.2 github.com/google/uuid v1.1.2
github.com/spf13/viper v1.10.1 github.com/spf13/viper v1.10.1
golang.org/x/crypto v0.0.0-20220214200702-86341886e292
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gorm.io/driver/mysql v1.3.2 gorm.io/driver/mysql v1.3.2
gorm.io/gorm v1.23.5 gorm.io/gorm v1.23.5
) )
@ -20,7 +19,6 @@ require (
require ( require (
cloud.google.com/go v0.99.0 // indirect cloud.google.com/go v0.99.0 // indirect
cloud.google.com/go/firestore v1.6.1 // indirect cloud.google.com/go/firestore v1.6.1 // indirect
gitee.ltd/lxh/logger v1.0.14 // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
github.com/armon/go-metrics v0.3.10 // indirect github.com/armon/go-metrics v0.3.10 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
@ -98,6 +96,7 @@ require (
go.uber.org/atomic v1.9.0 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 // indirect
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87 // indirect golang.org/x/net v0.0.0-20220517181318-183a9ca12b87 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect

View File

@ -9,12 +9,7 @@ import (
// 初始化数据库表 // 初始化数据库表
func databaseTable() { func databaseTable() {
dbs := []any{ dbs := []any{
new(entity.User), new(entity.User), // 管理员用户
new(entity.Category),
new(entity.Problem),
new(entity.ProblemCategory),
new(entity.Submit),
new(entity.TestCase),
} }
if err := client.MySQL.AutoMigrate(dbs...); err != nil { if err := client.MySQL.AutoMigrate(dbs...); err != nil {

50
main.go
View File

@ -1,50 +0,0 @@
package main
import (
"fmt"
"git.echol.cn/loser/logger/log"
"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"
"net/http"
"online_code/config"
"online_code/core"
"online_code/route"
"time"
)
var g errgroup.Group
// 启动入口
func main() {
// 强制日志颜色化
gin.ForceConsoleColor()
app := &http.Server{
Addr: fmt.Sprintf(":%d", config.Scd.Api.Port),
Handler: api(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 1 * time.Minute,
}
// 启动项目
g.Go(func() error {
return app.ListenAndServe()
})
if err := g.Wait(); err != nil {
log.Panicf("启动失败,错误信息:%s", err.Error())
}
}
// 生成接口服务
func api() http.Handler {
app := gin.New()
app.Use(gin.Recovery())
// 开启自定义请求方式不允许处理函数
app.HandleMethodNotAllowed = true
// 处理请求方式不对
app.NoMethod(core.NoMethodHandler())
// 404返回数据
app.NoRoute(core.NoRouteHandler())
// 初始化路由
route.InitRoute(app.Group(config.Scd.Api.Prefix))
return app
}

18
models/cache/user.go vendored
View File

@ -1,18 +0,0 @@
package cache
import "encoding/json"
// UserInfo 登录用的用户信息结构体
type UserInfo struct {
UserType string `json:"userType"` // 用户类型
UserId string `json:"userId"` // 用户Id
}
// String 实现Stringer接口
func (i UserInfo) String() (string, error) {
b, err := json.Marshal(i)
if err != nil {
return "", err
}
return string(b), nil
}

View File

@ -4,7 +4,7 @@ import (
"online_code/common/types" "online_code/common/types"
) )
// ProblemCategory 问题分类关联 // ProblemCategory 问题分类
type ProblemCategory struct { type ProblemCategory struct {
types.BaseDbModel types.BaseDbModel
ProblemId uint `json:"problem_id" gorm:"column:problem_id;type:int(11);comment:'问题的ID'" ` // 问题的ID ProblemId uint `json:"problem_id" gorm:"column:problem_id;type:int(11);comment:'问题的ID'" ` // 问题的ID

View File

@ -1,7 +0,0 @@
package param
// 分页通用参数
type page struct {
Current int `json:"current" form:"current" binding:"required"` // 页码
Size int `json:"size" form:"size" binding:"required"` // 每页数量
}

View File

@ -1,6 +0,0 @@
package param
type GetProblemList struct {
page
Keyword string `json:"keyword" form:"keyword"` // 问题关键字
}

View File

@ -1,19 +0,0 @@
package repository
import "gorm.io/gorm"
// 分页组件
func page(current, size int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if current == 0 {
current = 1
}
if size < 1 {
size = 10
}
// 计算偏移量
offset := (current - 1) * size
// 返回组装结果
return db.Offset(offset).Limit(size)
}
}

View File

@ -1,31 +0,0 @@
package repository
import (
"online_code/client"
"online_code/models/entity"
"online_code/models/param"
)
type problemService struct{}
func ProblemService() *problemService {
return &problemService{}
}
// GetList 获取题目列表
func (problemService) GetList(p param.GetProblemList) (records entity.Problem, count int64, err error) {
sel := client.MySQL.Scopes(page(p.Current, p.Size))
if p.Keyword != "" {
sel.Where("title LIKE ? OR content like ?", "%"+p.Keyword+"%", "%"+p.Keyword+"%")
}
err = sel.Order("updated_at DESC").Find(&records).Offset(-1).Limit(-1).Count(&count).Error
return
}
// GetProblemInfo 获取题目详情
func (problemService) GetProblemInfo(id int) (problem entity.Problem, err error) {
err = client.MySQL.Where("id = ?", id).Find(&problem).Error
return
}

View File

@ -1,10 +0,0 @@
package route
import (
"github.com/gin-gonic/gin"
"online_code/api"
)
func problem(g *gin.RouterGroup) {
g.GET("/list", api.ProblemApi().GetProbleList)
}

View File

@ -1,8 +0,0 @@
package route
import "github.com/gin-gonic/gin"
func InitRoute(g *gin.RouterGroup) {
api := g.Group("/api")
problem(api.Group("/problem"))
}

View File

@ -1,14 +0,0 @@
package utils
// GenTotalPage 计算总页数
func GenTotalPage(count int64, size int) int {
totalPage := 0
if count > 0 {
upPage := 0
if int(count)%size > 0 {
upPage = 1
}
totalPage = (int(count) / size) + upPage
}
return totalPage
}

View File

@ -1,20 +0,0 @@
package utils
import (
"golang.org/x/crypto/bcrypt"
)
// 生成Token的密钥写死防止乱改
//var jwtSecret = "qSxw4fCBBBecPsws"
// HashPassword 加密密码
func HashPassword(pass *string) {
bytePass := []byte(*pass)
hPass, _ := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost)
*pass = string(hPass)
}
// ComparePassword 校验密码
func ComparePassword(dbPass, pass string) bool {
return bcrypt.CompareHashAndPassword([]byte(dbPass), []byte(pass)) == nil
}