Compare commits
15 Commits
2d65afdf33
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a83f77b48f | |||
|
|
5ceca47d39 | ||
|
|
f62fd46939 | ||
| c7601ed174 | |||
| 8b04d7b763 | |||
| 2a35f2f816 | |||
| 657dc7c3a6 | |||
| 4cab2f6ff3 | |||
| 731f1d01e6 | |||
| 688177ba06 | |||
| d09d9d9f1a | |||
| 27c89d66f7 | |||
| feb235e11d | |||
| 35a31a6614 | |||
| 1ab62031ea |
19
.gitea/workflows/demo.yaml
Normal file
19
.gitea/workflows/demo.yaml
Normal file
@@ -0,0 +1,19 @@
|
||||
name: Gitea Actions Demo
|
||||
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
Explore-Gitea-Actions:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
|
||||
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
|
||||
- run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
- run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
|
||||
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
|
||||
- name: List files in the repository
|
||||
run: |
|
||||
ls ${{ gitea.workspace }}
|
||||
- run: echo "🍏 This job's status is ${{ job.status }}."
|
||||
Binary file not shown.
@@ -1,30 +1,4 @@
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/common"
|
||||
"fmt"
|
||||
"Crimson-Gatekeeper/internal/database"
|
||||
"Crimson-Gatekeeper/internal/utils"
|
||||
|
||||
"gorm.io/gen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client, clean := common.GetDataBaseClient()
|
||||
defer func() {
|
||||
err := clean()
|
||||
if err != nil {
|
||||
fmt.Println("关闭数据库链接失败")
|
||||
}
|
||||
}()
|
||||
client, err := database.GetDataBaseClient()
|
||||
utils.PaincEro(err, "数据库链接失败")
|
||||
g := gen.NewGenerator(gen.Config{
|
||||
OutPath: "./internal/query",
|
||||
ModelPkgPath: "./internal/model",
|
||||
20
srv/go.mod
20
srv/go.mod
@@ -4,8 +4,11 @@ go 1.25.5
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
github.com/google/wire v0.7.0
|
||||
gorm.io/driver/postgres v1.6.0
|
||||
gorm.io/gen v0.3.27
|
||||
gorm.io/gorm v1.31.1
|
||||
gorm.io/plugin/dbresolver v1.6.2
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -41,17 +44,16 @@ require (
|
||||
github.com/ugorji/go/codec v1.3.0 // indirect
|
||||
go.uber.org/mock v0.5.0 // indirect
|
||||
golang.org/x/arch v0.20.0 // indirect
|
||||
golang.org/x/crypto v0.47.0 // indirect
|
||||
golang.org/x/mod v0.32.0 // indirect
|
||||
golang.org/x/net v0.49.0 // indirect
|
||||
golang.org/x/sync v0.19.0 // indirect
|
||||
golang.org/x/sys v0.40.0 // indirect
|
||||
golang.org/x/text v0.33.0 // indirect
|
||||
golang.org/x/tools v0.41.0 // indirect
|
||||
golang.org/x/crypto v0.51.0 // indirect
|
||||
golang.org/x/mod v0.35.0 // indirect
|
||||
golang.org/x/net v0.53.0 // indirect
|
||||
golang.org/x/sync v0.20.0 // indirect
|
||||
golang.org/x/sys v0.44.0 // indirect
|
||||
golang.org/x/text v0.37.0 // indirect
|
||||
golang.org/x/tools v0.44.0 // indirect
|
||||
google.golang.org/protobuf v1.36.9 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gorm.io/datatypes v1.2.7 // 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/plugin/dbresolver v1.6.2 // indirect
|
||||
)
|
||||
|
||||
38
srv/go.sum
38
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-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||
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/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
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/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/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
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-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.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/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
@@ -86,31 +96,35 @@ go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
||||
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/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/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/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
|
||||
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
|
||||
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/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
||||
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
||||
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
|
||||
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
|
||||
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.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
|
||||
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
|
||||
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.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
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/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
|
||||
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
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/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=
|
||||
golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=
|
||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||
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.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
|
||||
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
|
||||
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
|
||||
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
@@ -124,6 +138,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/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
|
||||
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/go.mod h1:9zquz2xD1f3Eb/eHq4oLn2z6vDVvQlCY5S3uMBLv4EA=
|
||||
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||
|
||||
48
srv/internal/application/appliceation.go
Normal file
48
srv/internal/application/appliceation.go
Normal file
@@ -0,0 +1,48 @@
|
||||
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(cs []Ctrl, p *sql.DB) *Application {
|
||||
route := gin.Default()
|
||||
for _, ctrl := range cs {
|
||||
ctrl.RegisterRoutes(route)
|
||||
}
|
||||
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 (
|
||||
"Crimson-Gatekeeper/internal/query"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
@@ -12,7 +14,7 @@ import (
|
||||
// 返回值
|
||||
// - 一个 gorm.DB 的指针
|
||||
// - 一个清理所有数据库相关数据的函数
|
||||
func GetDataBaseClient() (*gorm.DB, func() error) {
|
||||
func GetDataBaseClient() (*gorm.DB, error) {
|
||||
dsn := "host=localhost " +
|
||||
"user=gatekeeper " +
|
||||
"dbname=crimson " +
|
||||
@@ -24,17 +26,23 @@ func GetDataBaseClient() (*gorm.DB, func() error) {
|
||||
client, ero := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if ero != nil {
|
||||
fmt.Println("数据库链接建立失败")
|
||||
panic(ero)
|
||||
return nil, ero
|
||||
}
|
||||
|
||||
poolCfg, ero := client.DB()
|
||||
if ero != nil {
|
||||
fmt.Println("获取数据库链接池失败")
|
||||
panic(ero)
|
||||
}
|
||||
|
||||
poolCfg.SetConnMaxIdleTime(3)
|
||||
poolCfg.SetMaxOpenConns(10)
|
||||
|
||||
return client, poolCfg.Close
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func GetPoolCfg(g *gorm.DB) (*sql.DB, error) {
|
||||
pool, err := g.DB()
|
||||
if err != nil {
|
||||
fmt.Println("获取数据库链接池失败")
|
||||
return nil, err
|
||||
}
|
||||
pool.SetConnMaxIdleTime(3)
|
||||
pool.SetMaxOpenConns(10)
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func GetQuery(db *gorm.DB) *query.Query {
|
||||
return query.Use(db)
|
||||
}
|
||||
@@ -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
srv/internal/resource/database.go
Normal file
1
srv/internal/resource/database.go
Normal file
@@ -0,0 +1 @@
|
||||
package resource
|
||||
7
srv/internal/resource/init.go
Normal file
7
srv/internal/resource/init.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package resource
|
||||
|
||||
var cfg *Cfg
|
||||
|
||||
func init() {
|
||||
cfg = getCfg()
|
||||
}
|
||||
65
srv/internal/resource/parse.go
Normal file
65
srv/internal/resource/parse.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"Crimson-Gatekeeper/internal/utils"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Cfg struct {
|
||||
server struct {
|
||||
port string
|
||||
database *struct {
|
||||
host string
|
||||
user string
|
||||
dbname string
|
||||
sslmode string
|
||||
port string
|
||||
passwd string
|
||||
connectTimeout int
|
||||
sshPoint string
|
||||
}
|
||||
sshPoint map[string]*struct {
|
||||
host string
|
||||
port int
|
||||
user string
|
||||
passwd string
|
||||
keyPath string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getCfg() *Cfg {
|
||||
pwd, err := os.Getwd()
|
||||
utils.PaincEro(err, "获取当前工作目录失败")
|
||||
dirs, err := os.ReadDir(pwd)
|
||||
utils.PaincEro(err, "获取目录文件失败")
|
||||
var data []byte
|
||||
for _, item := range dirs {
|
||||
isyaml := strings.HasSuffix(item.Name(), ".yaml")
|
||||
if isyaml {
|
||||
filePath := path.Join(pwd, item.Name())
|
||||
data, err = os.ReadFile(filePath)
|
||||
utils.PaincEro(err, "读取配置文件失败")
|
||||
break
|
||||
}
|
||||
}
|
||||
if data == nil {
|
||||
fmt.Println("没有找到配置文件,启动失败")
|
||||
panic("没有找到配置文件,进程终止")
|
||||
}
|
||||
cfg := &Cfg{}
|
||||
err = yaml.Unmarshal(data, cfg)
|
||||
utils.PaincEro(err, "解析配置文件失败")
|
||||
for _, item := range cfg.server.sshPoint {
|
||||
if item.port == 0 {
|
||||
item.port = 22
|
||||
}
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
25
srv/internal/resource/ssh.go
Normal file
25
srv/internal/resource/ssh.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/utils"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func getSshEnv(name string) *ssh.Client {
|
||||
sshCfg := cfg.server.sshPoint[name]
|
||||
if sshCfg == nil {
|
||||
return nil
|
||||
}
|
||||
clientCfg := &ssh.ClientConfig{
|
||||
User: sshCfg.user,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.Password(sshCfg.passwd),
|
||||
},
|
||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||
}
|
||||
client, err := ssh.Dial("tcp", sshCfg.host+strconv.Itoa(sshCfg.port), clientCfg)
|
||||
utils.PaincEro(err, "建立 SSH 隧道失败,程序中止")
|
||||
return client
|
||||
}
|
||||
1
srv/internal/server/server.go
Normal file
1
srv/internal/server/server.go
Normal file
@@ -0,0 +1 @@
|
||||
package server
|
||||
@@ -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 {
|
||||
}
|
||||
10
srv/internal/utils/index.go
Normal file
10
srv/internal/utils/index.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package utils
|
||||
|
||||
import "fmt"
|
||||
|
||||
func PaincEro(err error, msg string) {
|
||||
if err != nil {
|
||||
fmt.Println(msg)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
20
srv/server-env/config.yaml
Normal file
20
srv/server-env/config.yaml
Normal file
@@ -0,0 +1,20 @@
|
||||
server:
|
||||
# 服务运行端口
|
||||
port: 8443
|
||||
# 数据库配置
|
||||
database:
|
||||
host: localhost
|
||||
user: gatekeeper
|
||||
dbname: crimson
|
||||
sslmode: disable
|
||||
port: 5432
|
||||
passwd: crimson
|
||||
connect-timeout: 20
|
||||
ssh-point: ycy
|
||||
ssh-point:
|
||||
- ycy:
|
||||
host: www.ciallo.pub
|
||||
user: root
|
||||
port: 13915
|
||||
# passwd: kgmO6dGkC6hB
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
DROP TABLE "user" ;
|
||||
|
||||
-- サキュバス喚んだら義母が来た!? --
|
||||
Reference in New Issue
Block a user