重头再来

This commit is contained in:
des
2026-01-21 00:00:49 +08:00
parent 1ab62031ea
commit 35a31a6614
7 changed files with 0 additions and 203 deletions

View File

@@ -1,30 +1 @@
package main package main
import (
"Crimson-Gatekeeper/internal/common"
"Crimson-Gatekeeper/internal/middle"
"Crimson-Gatekeeper/internal/query"
"Crimson-Gatekeeper/internal/user"
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
app, clean := launchApplication()
defer clean()
err := app.Run(":3333")
if err != nil {
fmt.Println("应用启动失败")
panic(err)
}
}
func launchApplication() (*gin.Engine, func() error) {
db, clean := common.GetDataBaseClient()
q := query.Use(db)
app := gin.Default()
app.Use(middle.ResponsePackageMiddle)
user.New(q).RegisterRoute(app)
return app, clean
}

View File

@@ -1,27 +0,0 @@
package ero
import "net/http"
type Type int
const (
// ServerError 通用的,不需要特殊处理暴露给前端的错误
ServerError Type = iota
// FormError 表单校验异常没有执行成功。Data 内会有明确的字段级错误返回给前端处理
FormError
)
// GeneralError 通用的错误数据包装
type GeneralError struct {
Status int
// Type 错误类型,参考自定义类型 Type
Type Type
// Message 当前错误的简短解释
Message string
// Data 如果是复杂错误,此处会返回前端协助前端展示错误的数据
Data any
}
func SimpleError(msg string) *GeneralError {
return &GeneralError{http.StatusInternalServerError, ServerError, msg, nil}
}

View File

@@ -1,61 +0,0 @@
package user
import (
"Crimson-Gatekeeper/internal/common"
"Crimson-Gatekeeper/internal/ero"
"Crimson-Gatekeeper/internal/query"
"context"
"fmt"
"github.com/gin-gonic/gin"
)
const version = "/api/v1"
const group = "/user"
type Controller struct {
query *query.Query
}
type postLoginParam struct {
Account string `json:"account" binding:"required"`
Password string `json:"password" binding:"required"`
}
func (c *Controller) postLogin(ctx *gin.Context) {
param := &postLoginParam{}
if err := ctx.ShouldBindJSON(&param); err != nil {
fmt.Println("参数解析失败,请确认参数是否为 JSON")
fmt.Println(err)
}
u := c.query.User
user, err := u.WithContext(context.Background()).Where(u.Account.Eq(param.Account)).Take()
if err != nil {
fmt.Println("查询错误")
common.SetData(ctx, ero.SimpleError("服务器繁忙,请稍后重试"))
}
if user == nil {
fmt.Println("没有找到用户")
return
}
if user.Passwd != param.Password {
fmt.Println("用户密码错误")
return
}
}
func (c *Controller) RegisterRoute(gin *gin.Engine) {
group := gin.Group(version).Group(group)
group.POST("/login", c.postLogin)
}
// New 获取用户模块的控制器
//
// 参数
// - gorm.DB 该模块所使用的数据库链接
//
// 返回值
// - Controller 控制器的实例指针
func New(param *query.Query) *Controller {
return &Controller{param}
}

View File

@@ -1,39 +0,0 @@
package user_test
import (
"Crimson-Gatekeeper/internal/common"
"Crimson-Gatekeeper/internal/query"
"Crimson-Gatekeeper/internal/user"
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestLogin(t *testing.T) {
client, clean := common.GetDataBaseClient()
defer clean()
gin.SetMode(gin.TestMode)
app := gin.Default()
user.New(query.Use(client)).RegisterRoute(app)
w := httptest.NewRecorder()
param := gin.H{
"account": "admin1",
"password": "123445",
}
data, ero := json.Marshal(param)
if ero != nil {
fmt.Println("序列化失败")
panic(ero)
}
fmt.Println("参数")
fmt.Println(string(data))
req, _ := http.NewRequest("POST", "/api/v1/user/login", bytes.NewReader(data))
app.ServeHTTP(w, req)
fmt.Println("测试结果")
fmt.Println(w)
}

View File

@@ -1,25 +0,0 @@
package user
import (
"Crimson-Gatekeeper/internal/model"
"sync"
"time"
)
type login_info struct {
info model.User
token string
invalidation time.Time
}
type login_manage struct {
data *sync.Map
}
func (l *login_manage) Login(li *login_info) {
l.data.Store(li.token, li)
}
func newLoginManage() *login_manage {
return &login_manage{data: &sync.Map{}}
}

View File

@@ -1,9 +0,0 @@
package user
import (
"Crimson-Gatekeeper/internal/query"
)
type Service struct {
c *query.Query
}

View File

@@ -1,13 +0,0 @@
package wire_generate
import (
"Crimson-Gatekeeper/internal/common"
"Crimson-Gatekeeper/internal/user"
"github.com/google/wire"
)
func TestInit() *user.Controller {
wire.Build(user.New, common.GetQuery, common.GetDataBaseClient)
return &user.Controller{}
}