Compare commits
10 Commits
2d65afdf33
...
2a35f2f816
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a35f2f816 | |||
| 657dc7c3a6 | |||
| 4cab2f6ff3 | |||
| 731f1d01e6 | |||
| 688177ba06 | |||
| d09d9d9f1a | |||
| 27c89d66f7 | |||
| feb235e11d | |||
| 35a31a6614 | |||
| 1ab62031ea |
Binary file not shown.
@@ -1,30 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"Crimson-Gatekeeper/internal/common"
|
|
||||||
"Crimson-Gatekeeper/internal/middle"
|
|
||||||
"Crimson-Gatekeeper/internal/query"
|
|
||||||
"Crimson-Gatekeeper/internal/user"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app, clean := launchApplication()
|
app, err := getApplication()
|
||||||
defer clean()
|
if err != nil {
|
||||||
err := app.Run(":3333")
|
fmt.Println("应用初始化失败")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = app.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("应用启动失败")
|
fmt.Println("应用启动失败")
|
||||||
panic(err)
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
23
srv/cmd/gatekeeper/wire.go
Normal file
23
srv/cmd/gatekeeper/wire.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
//go:build wireinject
|
||||||
|
// +build wireinject
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Crimson-Gatekeeper/internal/application"
|
||||||
|
"Crimson-Gatekeeper/internal/database"
|
||||||
|
"Crimson-Gatekeeper/internal/module/userpak"
|
||||||
|
|
||||||
|
"github.com/google/wire"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getApplication() (*application.Application, error) {
|
||||||
|
wire.Build(
|
||||||
|
application.NewApplication,
|
||||||
|
database.GetDataBaseClient,
|
||||||
|
database.GetQuery,
|
||||||
|
database.GetPoolCfg,
|
||||||
|
userpak.NewUserCtl,
|
||||||
|
)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
39
srv/cmd/gatekeeper/wire_gen.go
Normal file
39
srv/cmd/gatekeeper/wire_gen.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
// Code generated by Wire. DO NOT EDIT.
|
||||||
|
|
||||||
|
//go:generate go run -mod=mod github.com/google/wire/cmd/wire
|
||||||
|
//go:build !wireinject
|
||||||
|
// +build !wireinject
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Crimson-Gatekeeper/internal/application"
|
||||||
|
"Crimson-Gatekeeper/internal/database"
|
||||||
|
"Crimson-Gatekeeper/internal/module/userpak"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Injectors from wire.go:
|
||||||
|
|
||||||
|
func getApplication() (*application.Application, error) {
|
||||||
|
db, err := database.GetDataBaseClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query := database.GetQuery(db)
|
||||||
|
userCtl := userpak.NewUserCtl(query)
|
||||||
|
v := CtrlCollector(userCtl)
|
||||||
|
sqlDB, err := database.GetPoolCfg(db)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
applicationApplication := application.NewApplication(v, sqlDB)
|
||||||
|
return applicationApplication, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// wire.go:
|
||||||
|
|
||||||
|
func CtrlCollector(
|
||||||
|
c1 *userpak.UserCtl,
|
||||||
|
) []application.Ctrl {
|
||||||
|
return []application.Ctrl{c1}
|
||||||
|
}
|
||||||
9
srv/cmd/tool/cfg.go
Normal file
9
srv/cmd/tool/cfg.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package tool
|
||||||
|
|
||||||
|
// Karmaforge 因果熔炉
|
||||||
|
// 这个函数会被引用,并且最终被代码生成所重写
|
||||||
|
func Karmaforge[T any](params ...any) *T {
|
||||||
|
panic("在使用因果熔炉之前,需要执行仪式")
|
||||||
|
// var result T
|
||||||
|
// return &result
|
||||||
|
}
|
||||||
1
srv/cmd/tool/lab/bluePrint.go
Normal file
1
srv/cmd/tool/lab/bluePrint.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package lab
|
||||||
5
srv/cmd/tool/lab/interface.go
Normal file
5
srv/cmd/tool/lab/interface.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package lab
|
||||||
|
|
||||||
|
type Animal interface {
|
||||||
|
sound() string
|
||||||
|
}
|
||||||
3
srv/cmd/tool/lab/main.go
Normal file
3
srv/cmd/tool/lab/main.go
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package lab
|
||||||
|
|
||||||
|
func main() {}
|
||||||
21
srv/cmd/tool/lab/pasture/dog.go
Normal file
21
srv/cmd/tool/lab/pasture/dog.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package pasture
|
||||||
|
|
||||||
|
type Dog struct {
|
||||||
|
lines string
|
||||||
|
weapon string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Dog) Sound() string {
|
||||||
|
return d.lines
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Dog) Dangers() bool {
|
||||||
|
if len(d.weapon) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDog(line string) *Dog {
|
||||||
|
return &Dog{lines: line}
|
||||||
|
}
|
||||||
13
srv/cmd/tool/lab/pasture/sheep.go
Normal file
13
srv/cmd/tool/lab/pasture/sheep.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package pasture
|
||||||
|
|
||||||
|
type Sheep struct {
|
||||||
|
lines string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sheep) Sound() string {
|
||||||
|
return s.lines
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSheep(line string) *Sheep {
|
||||||
|
return &Sheep{lines: line}
|
||||||
|
}
|
||||||
59
srv/cmd/tool/main.go
Normal file
59
srv/cmd/tool/main.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package tool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go/types"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/tools/go/packages"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("无法获取当前工作目录")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
sep := string(filepath.Separator)
|
||||||
|
s := strings.Split(pwd, sep)
|
||||||
|
var project string
|
||||||
|
for i := len(s); i > 0; i-- {
|
||||||
|
base := strings.Join(s[:i], sep)
|
||||||
|
module := filepath.Join(base, "go.mod")
|
||||||
|
_, err := os.Stat(module)
|
||||||
|
if err == nil {
|
||||||
|
project = module
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if project == "" {
|
||||||
|
panic("没有找到 go.mod ,请检查是否在 module 中调用程序")
|
||||||
|
}
|
||||||
|
project = filepath.Dir(project)
|
||||||
|
fmt.Println("当前寻根目录 " + project)
|
||||||
|
pkgs, err := packages.Load(&packages.Config{
|
||||||
|
Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedDeps,
|
||||||
|
Tests: false,
|
||||||
|
}, filepath.Join(project, "..."))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("解析项目失败")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for _, pkg := range pkgs {
|
||||||
|
top := pkg.Types.Scope()
|
||||||
|
funcs := []*types.Func{}
|
||||||
|
for _, name := range top.Names() {
|
||||||
|
obj := top.Lookup(name)
|
||||||
|
if !strings.HasSuffix(obj.Name(), "Provider") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fun, ok := obj.(*types.Func)
|
||||||
|
if ok {
|
||||||
|
funcs = append(funcs, fun)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(pkg.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,11 @@ go 1.25.5
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.11.0
|
github.com/gin-gonic/gin v1.11.0
|
||||||
|
github.com/google/wire v0.7.0
|
||||||
gorm.io/driver/postgres v1.6.0
|
gorm.io/driver/postgres v1.6.0
|
||||||
|
gorm.io/gen v0.3.27
|
||||||
gorm.io/gorm v1.31.1
|
gorm.io/gorm v1.31.1
|
||||||
|
gorm.io/plugin/dbresolver v1.6.2
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -51,7 +54,5 @@ require (
|
|||||||
google.golang.org/protobuf v1.36.9 // indirect
|
google.golang.org/protobuf v1.36.9 // indirect
|
||||||
gorm.io/datatypes v1.2.7 // indirect
|
gorm.io/datatypes v1.2.7 // indirect
|
||||||
gorm.io/driver/mysql v1.6.0 // indirect
|
gorm.io/driver/mysql v1.6.0 // indirect
|
||||||
gorm.io/gen v0.3.27 // indirect
|
|
||||||
gorm.io/hints v1.1.2 // indirect
|
gorm.io/hints v1.1.2 // indirect
|
||||||
gorm.io/plugin/dbresolver v1.6.2 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
24
srv/go.sum
24
srv/go.sum
@@ -29,11 +29,17 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
|||||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||||
|
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
|
||||||
|
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||||
|
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
|
||||||
|
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
|
||||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/google/wire v0.7.0 h1:JxUKI6+CVBgCO2WToKy/nQk0sS+amI9z9EjVmdaocj4=
|
||||||
|
github.com/google/wire v0.7.0/go.mod h1:n6YbUQD9cPKTnHXEBN2DXlOp/mVADhVErcMFb0v3J18=
|
||||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||||
@@ -56,6 +62,10 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
|
|||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
|
github.com/microsoft/go-mssqldb v1.7.2 h1:CHkFJiObW7ItKTJfHo1QX7QBBD1iV+mn1eOyRP3b/PA=
|
||||||
|
github.com/microsoft/go-mssqldb v1.7.2/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
@@ -86,29 +96,19 @@ go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
|||||||
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
|
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
|
||||||
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
|
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
|
||||||
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
|
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
|
||||||
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
|
||||||
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
|
||||||
golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8=
|
golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8=
|
||||||
golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A=
|
golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A=
|
||||||
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
|
|
||||||
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
|
|
||||||
golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=
|
golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=
|
||||||
golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=
|
golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=
|
||||||
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
|
||||||
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
|
||||||
golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o=
|
golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o=
|
||||||
golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8=
|
golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8=
|
||||||
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||||
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
|
||||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
|
||||||
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
|
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
|
||||||
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
|
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
|
||||||
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
|
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
|
||||||
golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=
|
|
||||||
golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=
|
|
||||||
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
|
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
|
||||||
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
|
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
|
||||||
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
|
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
|
||||||
@@ -124,6 +124,10 @@ gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqK
|
|||||||
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
|
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
|
||||||
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
|
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
|
||||||
gorm.io/driver/sqlite v1.5.0/go.mod h1:kDMDfntV9u/vuMmz8APHtHF0b4nyBB7sfCieC6G8k8I=
|
gorm.io/driver/sqlite v1.5.0/go.mod h1:kDMDfntV9u/vuMmz8APHtHF0b4nyBB7sfCieC6G8k8I=
|
||||||
|
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
|
||||||
|
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
|
||||||
|
gorm.io/driver/sqlserver v1.6.0 h1:VZOBQVsVhkHU/NzNhRJKoANt5pZGQAS1Bwc6m6dgfnc=
|
||||||
|
gorm.io/driver/sqlserver v1.6.0/go.mod h1:WQzt4IJo/WHKnckU9jXBLMJIVNMVeTu25dnOzehntWw=
|
||||||
gorm.io/gen v0.3.27 h1:ziocAFLpE7e0g4Rum69pGfB9S6DweTxK8gAun7cU8as=
|
gorm.io/gen v0.3.27 h1:ziocAFLpE7e0g4Rum69pGfB9S6DweTxK8gAun7cU8as=
|
||||||
gorm.io/gen v0.3.27/go.mod h1:9zquz2xD1f3Eb/eHq4oLn2z6vDVvQlCY5S3uMBLv4EA=
|
gorm.io/gen v0.3.27/go.mod h1:9zquz2xD1f3Eb/eHq4oLn2z6vDVvQlCY5S3uMBLv4EA=
|
||||||
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||||
|
|||||||
45
srv/internal/application/appliceation.go
Normal file
45
srv/internal/application/appliceation.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package application
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Ctrl interface {
|
||||||
|
RegisterRoutes(engine *gin.Engine)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Application struct {
|
||||||
|
srv *http.Server
|
||||||
|
pool *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *Application) Start() error {
|
||||||
|
return app.srv.ListenAndServe()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *Application) Stop() {
|
||||||
|
err := app.pool.Close()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("关闭数据库链接遭遇错误")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
err = app.srv.Shutdown(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("优雅关闭服务遭遇错误")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApplication(ct Ctrl, p *sql.DB) *Application {
|
||||||
|
route := gin.Default()
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: ":8443",
|
||||||
|
Handler: route,
|
||||||
|
}
|
||||||
|
return &Application{srv, p}
|
||||||
|
}
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package common
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ctxKey struct {
|
|
||||||
desc string
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
dataKey = ctxKey{"响应体内容"}
|
|
||||||
)
|
|
||||||
|
|
||||||
// SetData 设置响应数据,可以设置为 nil,或者干脆不设置。此时会返回空的响应包装。
|
|
||||||
func SetData(c *gin.Context, data any) {
|
|
||||||
c.Set(dataKey, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetData 获取响应数据
|
|
||||||
// 除了响应包装器,你不应该在任何地方调用该方法。因为响应数据的类型不一。
|
|
||||||
// 如果你真的对响应数据处理。请在中间件向上下文中塞入数据,在 handler 中处理好。
|
|
||||||
func GetData(c *gin.Context) any {
|
|
||||||
data, _ := c.Get(dataKey)
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package common
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"Crimson-Gatekeeper/internal/query"
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
@@ -12,7 +14,7 @@ import (
|
|||||||
// 返回值
|
// 返回值
|
||||||
// - 一个 gorm.DB 的指针
|
// - 一个 gorm.DB 的指针
|
||||||
// - 一个清理所有数据库相关数据的函数
|
// - 一个清理所有数据库相关数据的函数
|
||||||
func GetDataBaseClient() (*gorm.DB, func() error) {
|
func GetDataBaseClient() (*gorm.DB, error) {
|
||||||
dsn := "host=localhost " +
|
dsn := "host=localhost " +
|
||||||
"user=gatekeeper " +
|
"user=gatekeeper " +
|
||||||
"dbname=crimson " +
|
"dbname=crimson " +
|
||||||
@@ -24,17 +26,23 @@ func GetDataBaseClient() (*gorm.DB, func() error) {
|
|||||||
client, ero := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
client, ero := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||||
if ero != nil {
|
if ero != nil {
|
||||||
fmt.Println("数据库链接建立失败")
|
fmt.Println("数据库链接建立失败")
|
||||||
panic(ero)
|
return nil, ero
|
||||||
}
|
}
|
||||||
|
|
||||||
poolCfg, ero := client.DB()
|
return client, nil
|
||||||
if ero != nil {
|
}
|
||||||
|
|
||||||
|
func GetPoolCfg(g *gorm.DB) (*sql.DB, error) {
|
||||||
|
pool, err := g.DB()
|
||||||
|
if err != nil {
|
||||||
fmt.Println("获取数据库链接池失败")
|
fmt.Println("获取数据库链接池失败")
|
||||||
panic(ero)
|
return nil, err
|
||||||
|
}
|
||||||
|
pool.SetConnMaxIdleTime(3)
|
||||||
|
pool.SetMaxOpenConns(10)
|
||||||
|
return pool, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
poolCfg.SetConnMaxIdleTime(3)
|
func GetQuery(db *gorm.DB) *query.Query {
|
||||||
poolCfg.SetMaxOpenConns(10)
|
return query.Use(db)
|
||||||
|
|
||||||
return client, poolCfg.Close
|
|
||||||
}
|
}
|
||||||
@@ -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}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package middle
|
|
||||||
|
|
||||||
import (
|
|
||||||
"Crimson-Gatekeeper/internal/common"
|
|
||||||
"Crimson-Gatekeeper/internal/ero"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GeneralResponse 通用的返回包装结构体。不管错误与否
|
|
||||||
type GeneralResponse struct {
|
|
||||||
success string
|
|
||||||
data any
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResponsePackageMiddle 返回包装中间件
|
|
||||||
func ResponsePackageMiddle(c *gin.Context) {
|
|
||||||
c.Next()
|
|
||||||
data := common.GetData(c)
|
|
||||||
if data, ok := data.(ero.GeneralError); ok {
|
|
||||||
c.JSON(200, &GeneralResponse{"false", data})
|
|
||||||
} else {
|
|
||||||
c.JSON(200, &GeneralResponse{"fail", data})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
31
srv/internal/module/userpak/controller.go
Normal file
31
srv/internal/module/userpak/controller.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package userpak
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Crimson-Gatekeeper/internal/query"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserCtl struct {
|
||||||
|
q *query.Query
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserCtl(q *query.Query) *UserCtl {
|
||||||
|
return &UserCtl{q}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserCtl) login(ctx *gin.Context) {
|
||||||
|
lp := loginParam{}
|
||||||
|
err := ctx.ShouldBindQuery(&lp)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("出错啦")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
ctx.JSON(http.StatusOK, lp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserCtl) RegisterRoutes(eng *gin.Engine) {
|
||||||
|
eng.GET("/login", u.login)
|
||||||
|
}
|
||||||
6
srv/internal/module/userpak/param.go
Normal file
6
srv/internal/module/userpak/param.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package userpak
|
||||||
|
|
||||||
|
type loginParam struct {
|
||||||
|
Account string `json:"account" binding:"required"`
|
||||||
|
Password string `json:"password" binding:"required"`
|
||||||
|
}
|
||||||
@@ -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(¶m); 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}
|
|
||||||
}
|
|
||||||
@@ -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)
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package user
|
|
||||||
|
|
||||||
import (
|
|
||||||
"Crimson-Gatekeeper/internal/model"
|
|
||||||
"Crimson-Gatekeeper/internal/query"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// loginInfo 储存用户的登录状态
|
|
||||||
type loginInfo struct {
|
|
||||||
// user 信息指针
|
|
||||||
user *model.User
|
|
||||||
// invalidation 失效时间,此事件之后,该数据应该失效且被删除
|
|
||||||
invalidation time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
var loginIn = sync.Map{}
|
|
||||||
|
|
||||||
type Service struct {
|
|
||||||
c *query.Query
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) login(user *model.User) error {
|
|
||||||
}
|
|
||||||
@@ -1 +1,3 @@
|
|||||||
DROP TABLE "user" ;
|
DROP TABLE "user" ;
|
||||||
|
|
||||||
|
-- サキュバス喚んだら義母が来た!? --
|
||||||
@@ -2,19 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"Crimson-Gatekeeper/internal/common"
|
"Crimson-Gatekeeper/internal/common"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gorm.io/gen"
|
"gorm.io/gen"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
client, clean := common.GetDataBaseClient()
|
client, clean, _ := common.GetDataBaseClient()
|
||||||
defer func() {
|
defer clean()
|
||||||
err := clean()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("关闭数据库链接失败")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
g := gen.NewGenerator(gen.Config{
|
g := gen.NewGenerator(gen.Config{
|
||||||
OutPath: "./internal/query",
|
OutPath: "./internal/query",
|
||||||
ModelPkgPath: "./internal/model",
|
ModelPkgPath: "./internal/model",
|
||||||
|
|||||||
Reference in New Issue
Block a user