From 0a502f609e9284f0ecb32d053deb068f65b3ecc5 Mon Sep 17 00:00:00 2001 From: Odboy Date: Sun, 4 Feb 2024 19:14:13 +0800 Subject: [PATCH] init --- application-dev.yml | 17 +++ application.yml | 2 + go.mod | 61 ++++++++ go.sum | 188 +++++++++++++++++++++++++ main.go | 17 +++ src/infra/config/init.go | 67 +++++++++ src/infra/context/init.go | 15 ++ src/infra/database/init.go | 42 ++++++ src/infra/gin.go | 74 ++++++++++ src/infra/jwt/init.go | 73 ++++++++++ src/infra/rediscon/init.go | 41 ++++++ src/infra/rediskey/init.go | 5 + src/infra/redistool/init.go | 31 ++++ src/infra/resp/init.go | 21 +++ src/infra/router.go | 5 + src/modules/init.go | 6 + src/modules/system/dao/user.go | 15 ++ src/modules/system/domain/router.go | 16 +++ src/modules/system/domain/user.go | 13 ++ src/modules/system/rest/auth.go | 71 ++++++++++ src/modules/system/rest/dto/kickout.go | 5 + src/modules/system/rest/dto/login.go | 7 + src/modules/system/rest/user.go | 15 ++ 23 files changed, 807 insertions(+) create mode 100644 application-dev.yml create mode 100644 application.yml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 src/infra/config/init.go create mode 100644 src/infra/context/init.go create mode 100644 src/infra/database/init.go create mode 100644 src/infra/gin.go create mode 100644 src/infra/jwt/init.go create mode 100644 src/infra/rediscon/init.go create mode 100644 src/infra/rediskey/init.go create mode 100644 src/infra/redistool/init.go create mode 100644 src/infra/resp/init.go create mode 100644 src/infra/router.go create mode 100644 src/modules/init.go create mode 100644 src/modules/system/dao/user.go create mode 100644 src/modules/system/domain/router.go create mode 100644 src/modules/system/domain/user.go create mode 100644 src/modules/system/rest/auth.go create mode 100644 src/modules/system/rest/dto/kickout.go create mode 100644 src/modules/system/rest/dto/login.go create mode 100644 src/modules/system/rest/user.go diff --git a/application-dev.yml b/application-dev.yml new file mode 100644 index 0000000..50d8b56 --- /dev/null +++ b/application-dev.yml @@ -0,0 +1,17 @@ +server: + port: 8001 +datasource: + host: 127.0.0.1 + port: 3306 + username: root + password: 123456 + database: d2admin +redis: + host: 127.0.0.1 + port: 6379 + password: 123456 + database: 1 + pool: + size: 1024 +jwt: + secret: 123456 \ No newline at end of file diff --git a/application.yml b/application.yml new file mode 100644 index 0000000..c166b54 --- /dev/null +++ b/application.yml @@ -0,0 +1,2 @@ +active: dev +debug: true \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..99cdd90 --- /dev/null +++ b/go.mod @@ -0,0 +1,61 @@ +module d2-admin-service + +go 1.21 + +require ( + github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 + github.com/fatih/color v1.16.0 + github.com/gin-gonic/gin v1.9.1 + github.com/go-redis/redis/v8 v8.11.5 + github.com/go-sql-driver/mysql v1.7.1 + github.com/golang-jwt/jwt/v4 v4.5.0 + github.com/jinzhu/gorm v1.9.16 + github.com/spf13/viper v1.18.2 +) + +require ( + github.com/bytedance/sonic v1.9.1 // indirect + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.14.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/arch v0.3.0 // indirect + golang.org/x/crypto v0.16.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..736b1ba --- /dev/null +++ b/go.sum @@ -0,0 +1,188 @@ +github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= +github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= +github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= +github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= +github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= +github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= +github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= +github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +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/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= +github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= +github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= +github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= +github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= +github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= +github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= +golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..0472419 --- /dev/null +++ b/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "d2-admin-service/src/infra" + "d2-admin-service/src/infra/config" + "d2-admin-service/src/infra/database" + "d2-admin-service/src/infra/rediscon" + "d2-admin-service/src/util" +) + +func main() { + config.ImportYmlConfig(util.GetPWD()) + database.Connect() + rediscon.Connect() + infra.IntRouters() + infra.RunServer() +} diff --git a/src/infra/config/init.go b/src/infra/config/init.go new file mode 100644 index 0000000..bc91d87 --- /dev/null +++ b/src/infra/config/init.go @@ -0,0 +1,67 @@ +package config + +import ( + "d2-admin-service/src/util" + "fmt" + "github.com/spf13/viper" + "os" +) + +var Config *ApplicationConfig + +type ApplicationConfig struct { + // 激活哪一个配置文件,格式application-{active}.yml + Active string + Debug bool + Server struct { + Port string + } + Datasource struct { + Host string + Port int + Username string + Password string + Database string + } + Jwt struct { + Secret string + } + Redis struct { + Host string + Port int + Password string + Database int + Pool struct { + Size int + } + } +} + +func ImportYmlConfig(currentPath string) { + parentConfig := readYmlConfigFile(currentPath + string(os.PathSeparator) + "application.yml") + active := parentConfig.Active + if util.IsBlank(active) { + panic("application.yml中的active属性不能为空") + } + childrenConfig := readYmlConfigFile(currentPath + string(os.PathSeparator) + "application-" + active + ".yml") + childrenConfig.Active = parentConfig.Active + childrenConfig.Debug = parentConfig.Debug + if util.IsBlank(childrenConfig.Server.Port) { + // 默认端口 + childrenConfig.Server.Port = "8001" + } + Config = childrenConfig +} + +// 读取配置文件 +func readYmlConfigFile(filepath string) *ApplicationConfig { + viper.SetConfigFile(filepath) + err := viper.ReadInConfig() + if err != nil { + panic(fmt.Errorf("Fatal errors config file: %w \n", err)) + } + // 绑定配置到结构体 + var config ApplicationConfig + viper.Unmarshal(&config) + return &config +} diff --git a/src/infra/context/init.go b/src/infra/context/init.go new file mode 100644 index 0000000..b76a812 --- /dev/null +++ b/src/infra/context/init.go @@ -0,0 +1,15 @@ +package context + +import ( + "d2-admin-service/src/modules/system/domain" + "github.com/gin-gonic/gin" +) + +func SetUser(c *gin.Context, user *domain.User) { + c.Set("GenContext_UserInfo", user) +} + +func GetUser(c *gin.Context) *domain.User { + user, _ := c.Get("GenContext_UserInfo") + return user.(*domain.User) +} diff --git a/src/infra/database/init.go b/src/infra/database/init.go new file mode 100644 index 0000000..6838380 --- /dev/null +++ b/src/infra/database/init.go @@ -0,0 +1,42 @@ +package database + +import ( + "d2-admin-service/src/infra/config" + "d2-admin-service/src/modules/system/domain" + "fmt" + "github.com/fatih/color" + _ "github.com/go-sql-driver/mysql" + "github.com/jinzhu/gorm" + "time" +) + +var DB *gorm.DB + +func Connect() { + color.Green("============ 连接数据库 ============") + dsn := fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", + config.Config.Datasource.Username, + config.Config.Datasource.Password, + config.Config.Datasource.Host, + config.Config.Datasource.Port, + config.Config.Datasource.Database, + ) + var err error + DB, err = gorm.Open("mysql", dsn) + if err != nil { + panic("数据库连接失败, " + err.Error()) + } + // 连接池 + sqlDB := DB.DB() + // SetMaxIdleConns sets the maximum number of connections in the idle connection pool. + sqlDB.SetMaxIdleConns(10) + // SetMaxOpenConns sets the maximum number of open connections to the database. + sqlDB.SetMaxOpenConns(100) + // SetConnMaxLifetime sets the maximum amount of time a connection may be reused. + sqlDB.SetConnMaxLifetime(time.Hour) + //defer DB.Close() + + // 自动建表 + DB.AutoMigrate(&domain.User{}) + DB.AutoMigrate(&domain.Router{}) +} diff --git a/src/infra/gin.go b/src/infra/gin.go new file mode 100644 index 0000000..54086bd --- /dev/null +++ b/src/infra/gin.go @@ -0,0 +1,74 @@ +package infra + +import ( + "d2-admin-service/src/infra/config" + "d2-admin-service/src/infra/jwt" + "d2-admin-service/src/modules/system/dao" + "d2-admin-service/src/modules/system/rest" + "github.com/fatih/color" + "github.com/gin-gonic/gin" + "net/http" +) + +// 解决跨域问题 +func corsMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + method := c.Request.Method + c.Header("Access-Control-Allow-Origin", "*") + c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token,Authorization,Token") + c.Header("Access-Control-Allow-Methods", "POST,GET,OPTIONS") + c.Header("Access-Control-Expose-Headers", "Content-Length,Access-Control-Allow-Origin,Access-Control-Allow-Headers,Content-Type") + c.Header("Access-Control-Allow-Credentials", "True") + // 放行options请求 + if method == "OPTIONS" { + c.AbortWithStatus(http.StatusNoContent) + } + // 处理请求 + c.Next() + } +} + +//user := new(rest.UserController) +//Routers["/createUser"] = user.CreateUser + +func healthCheckApi(c *gin.Context) { + c.String(http.StatusOK, "online") +} + +func RunServer() { + if config.Config.Debug { + gin.SetMode(gin.DebugMode) + } else { + gin.SetMode(gin.ReleaseMode) + } + // 默认使用了2个中间件Logger(), Recovery() + r := gin.Default() + r.Use(corsMiddleware()) + // 健康检查接口 + r.GET("/", healthCheckApi) + r.GET("/healthCheck", healthCheckApi) + // 登入登出接口 + authController := rest.AuthController{} + r.POST("/login", authController.Login) + r.POST("/logout", authController.Logout) + // 定义需要鉴权的接口组 + apiGroup := r.Group("/api/v1") + apiGroup.Use(jwt.AuthMiddleware()) + { + routes := dao.RouterDao{}.GetAllRouter() + for _, route := range routes { + if route.RouterMethod == "GET" { + apiGroup.GET(route.RouterPath, InnerRouters[route.RouterName]) + continue + } + if route.RouterMethod == "POST" { + apiGroup.POST(route.RouterPath, InnerRouters[route.RouterName]) + } + } + } + color.Green("============ 启动Rest服务 ============") + err := r.Run(":" + string(config.Config.Server.Port)) + if err != nil { + panic("Server StartUp Error: " + err.Error()) + } +} diff --git a/src/infra/jwt/init.go b/src/infra/jwt/init.go new file mode 100644 index 0000000..b975557 --- /dev/null +++ b/src/infra/jwt/init.go @@ -0,0 +1,73 @@ +package jwt + +import ( + "d2-admin-service/src/infra/config" + "github.com/fatih/color" + "github.com/golang-jwt/jwt/v4" + "time" +) + +// CustomClaims 定义自定义claims结构体 +type CustomClaims struct { + jwt.RegisteredClaims + Username string `json:"username"` +} + +// GenToken 生成token +func GenToken(username string) string { + secret := config.Config.Jwt.Secret + // 私钥(用于HS256签名时用作secret,对于RS256等非对称算法则是私钥) + key := []byte(secret) + + // 生成claims + claims := &CustomClaims{ + RegisteredClaims: jwt.RegisteredClaims{ + Issuer: "odboy.cn", + ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)), // 设置过期时间 + Subject: username, // 用户ID或其他唯一标识符 + IssuedAt: jwt.NewNumericDate(time.Now()), + }, + Username: username, + } + + // 创建一个新的token对象 + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + // 使用密钥进行签名并获取完整的编码后的token + signedToken, err := token.SignedString(key) + if err != nil { + panic("Generated JWT Error: " + err.Error()) + } + color.Green("%s Generated JWT: %s\n", username, signedToken) + return signedToken +} + +// ParseToken 解析token +func ParseToken(signedToken string) (int, *CustomClaims) { + // 解析JWT + parser := jwt.Parser{} + // 需要设置Valid方法以验证claims中的标准字段,例如ExpiresAt + var parsedClaims *CustomClaims // 将parsedClaims声明为指针类型 + _, _, err := parser.ParseUnverified(signedToken, parsedClaims) + if err != nil { + //panic("无效Token" + err.Error()) + color.Red("无效token, %v\n", err) + return -1, nil + } + // 如果需要验证签名,请使用正确的秘钥和方法 + secret := config.Config.Jwt.Secret + key := []byte(secret) + verifiedToken, err := parser.Parse(signedToken, func(token *jwt.Token) (interface{}, error) { + return key, nil + }) + if err != nil { + color.Red("无效token, %v\n", err) + return -1, nil + } + parsedClaims, ok := verifiedToken.Claims.(*CustomClaims) + if !ok || !verifiedToken.Valid { + //panic("Invalid token") + color.Red("token未通过校验, %v\n", err) + return -1, nil + } + return 200, parsedClaims +} diff --git a/src/infra/rediscon/init.go b/src/infra/rediscon/init.go new file mode 100644 index 0000000..8261249 --- /dev/null +++ b/src/infra/rediscon/init.go @@ -0,0 +1,41 @@ +package rediscon + +import ( + "context" + "d2-admin-service/src/infra/config" + "fmt" + "github.com/go-redis/redis/v8" +) + +var RdbClient *redis.Client +var RdbCtx context.Context + +// 初始化Redis连接池 +func initRedisPool(host string, port int, password string, database int, poolSize int) *redis.Client { + opt := &redis.Options{ + Addr: fmt.Sprintf("%s:%d", host, port), // Redis服务器地址,如:"localhost:6379" + Password: password, // 如果有密码,则设置密码 + DB: database, // 选择数据库(非必需,默认是0号数据库) + PoolSize: poolSize, // 连接池大小 + } + // 创建并返回一个Redis客户端实例 + client := redis.NewClient(opt) + return client +} +func Connect() { + applicationConfig := config.Config + host := applicationConfig.Redis.Host + port := applicationConfig.Redis.Port + password := applicationConfig.Redis.Password + database := applicationConfig.Redis.Database + size := applicationConfig.Redis.Pool.Size + + ctx := context.Background() + RdbClient = initRedisPool(host, port, password, database, size) + // 检查连接是否成功 + if err := RdbClient.Ping(ctx).Err(); err != nil { + panic(err) + } + RdbCtx = ctx + //defer RdbClient.Close() +} diff --git a/src/infra/rediskey/init.go b/src/infra/rediskey/init.go new file mode 100644 index 0000000..6aa1eba --- /dev/null +++ b/src/infra/rediskey/init.go @@ -0,0 +1,5 @@ +package rediskey + +const ( + AUTH_TOKEN = "system:auth:token" +) diff --git a/src/infra/redistool/init.go b/src/infra/redistool/init.go new file mode 100644 index 0000000..7287420 --- /dev/null +++ b/src/infra/redistool/init.go @@ -0,0 +1,31 @@ +package redistool + +import ( + "d2-admin-service/src/infra/rediscon" + "github.com/go-redis/redis/v8" + "time" +) + +func Set(key string, value string, expiration time.Duration) { + _, err := rediscon.RdbClient.Set(rediscon.RdbCtx, key, value, expiration).Result() + if err != nil { + panic(err) + } +} + +func Get(key string) string { + result, err := rediscon.RdbClient.Get(rediscon.RdbCtx, key).Result() + if err == redis.Nil { + panic("Key not found") + } else if err != nil { + panic(err) + } + return result +} + +func Remove(key string) { + _, err := rediscon.RdbClient.Del(rediscon.RdbCtx, key).Result() + if err != nil { + panic(err) + } +} diff --git a/src/infra/resp/init.go b/src/infra/resp/init.go new file mode 100644 index 0000000..f03a0a8 --- /dev/null +++ b/src/infra/resp/init.go @@ -0,0 +1,21 @@ +package resp + +import "github.com/gin-gonic/gin" + +// gin.H封装了生成json数据的工具 +var ( + ResolveParamsError = gin.H{"errorCode": 10000, "msg": "无法解析参数"} + UsernameNotExistError = gin.H{"errorCode": 10001, "msg": "用户名不存在"} + PasswordError = gin.H{"errorCode": 10002, "msg": "密码错误"} + UsernameOrPasswordError = gin.H{"errorCode": 10003, "msg": "用户名或密码错误"} + NoOperationPermissionError = gin.H{"errorCode": 10004, "msg": "无操作权限"} + NoLoginError = gin.H{"errorCode": 10005, "msg": "未登录"} +) + +func Fail(errorCode int, data interface{}) gin.H { + return gin.H{"errorCode": errorCode, "data": data} +} + +func Success(data interface{}) gin.H { + return gin.H{"errorCode": 200, "data": data} +} diff --git a/src/infra/router.go b/src/infra/router.go new file mode 100644 index 0000000..671ad42 --- /dev/null +++ b/src/infra/router.go @@ -0,0 +1,5 @@ +package infra + +func IntRouters() { + +} diff --git a/src/modules/init.go b/src/modules/init.go new file mode 100644 index 0000000..bfb4641 --- /dev/null +++ b/src/modules/init.go @@ -0,0 +1,6 @@ +package modules + +func init() { + //在Go语言中,每个包都可以有一个或多个init函数。这些函数没有参数也没有返回值,它们会在包被导入时自动调用, + //并且按照源文件的(根据文件名字符串排序后的)顺序执行 +} diff --git a/src/modules/system/dao/user.go b/src/modules/system/dao/user.go new file mode 100644 index 0000000..0029bd1 --- /dev/null +++ b/src/modules/system/dao/user.go @@ -0,0 +1,15 @@ +package dao + +import ( + "d2-admin-service/src/infra/database" + "d2-admin-service/src/modules/system/domain" +) + +type UserDao struct { +} + +func (UserDao) GetUserByUsername(username string) *domain.User { + var user domain.User + database.DB.Find(&user, "username = ?", username) + return &user +} diff --git a/src/modules/system/domain/router.go b/src/modules/system/domain/router.go new file mode 100644 index 0000000..cb70c12 --- /dev/null +++ b/src/modules/system/domain/router.go @@ -0,0 +1,16 @@ +package domain + +import "github.com/jinzhu/gorm" + +type Router struct { + gorm.Model + RouterName string `gorm:"not null;unique_index:index_npm"` // 接口名称, 例如: getUser + RouterPath string `gorm:"not null;unique_index:index_npm"` // 接口路径, 例如: /api/v1/getUser + RouterMethod string `gorm:"not null;unique_index:index_npm"` // GET、POST + RouterDesc string `gorm:"not null"` // 接口说明 + RouterStatus int `gorm:"not null"` // 接口是否可用 +} + +func (Router) TableName() string { + return "system_router" +} diff --git a/src/modules/system/domain/user.go b/src/modules/system/domain/user.go new file mode 100644 index 0000000..561fcc8 --- /dev/null +++ b/src/modules/system/domain/user.go @@ -0,0 +1,13 @@ +package domain + +import "github.com/jinzhu/gorm" + +type User struct { + gorm.Model + Username string + Password string +} + +func (User) TableName() string { + return "system_user" +} diff --git a/src/modules/system/rest/auth.go b/src/modules/system/rest/auth.go new file mode 100644 index 0000000..97b6896 --- /dev/null +++ b/src/modules/system/rest/auth.go @@ -0,0 +1,71 @@ +package rest + +import ( + "d2-admin-service/src/infra/context" + "d2-admin-service/src/infra/jwt" + "d2-admin-service/src/infra/rediskey" + "d2-admin-service/src/infra/redistool" + "d2-admin-service/src/infra/resp" + "d2-admin-service/src/modules/system/dao" + "d2-admin-service/src/modules/system/rest/dto" + "github.com/gin-gonic/gin" + "net/http" + "time" +) + +type AuthController struct { +} + +// Login 登录 +func (AuthController) Login(c *gin.Context) { + userDao := dao.UserDao{} + + // 声明接收的变量 + var json dto.LoginDTO + // 将request的body中的数据,自动按照json格式解析到结构体 + if err := c.ShouldBindJSON(&json); err != nil { + c.JSON(http.StatusBadRequest, resp.ResolveParamsError) + return + } + user := userDao.GetUserByUsername(json.Username) + if user.Username == "" { + c.JSON(http.StatusBadRequest, resp.UsernameNotExistError) + return + } + // 判断用户名密码是否正确 + if json.Password != user.Password { + c.JSON(http.StatusBadRequest, resp.PasswordError) + return + } + token := jwt.GenToken(json.Username) + redistool.Set(rediskey.AUTH_TOKEN+":"+json.Username, token, time.Hour*20) + context.SetUser(c, user) + c.JSON(http.StatusOK, resp.Success(token)) +} + +// Logout 退出 +func (AuthController) Logout(c *gin.Context) { + user := context.GetUser(c) + redistool.Remove(rediskey.AUTH_TOKEN + ":" + user.Username) + c.JSON(http.StatusOK, resp.Success("")) +} + +// KickOut 踢出 +func (AuthController) KickOut(c *gin.Context) { + user := context.GetUser(c) + if user.Username != "admin" { + c.JSON(http.StatusBadRequest, resp.NoOperationPermissionError) + return + } + var json dto.KickOutDTO + if err := c.ShouldBindJSON(&json); err != nil { + c.JSON(http.StatusBadRequest, resp.ResolveParamsError) + return + } + redistool.Remove(rediskey.AUTH_TOKEN + ":" + json.Username) + c.JSON(http.StatusOK, resp.Success("")) +} + +func (AuthController) BindUserRole(c *gin.Context) {} +func (AuthController) BindRoleMenu(c *gin.Context) {} +func (AuthController) BindApiMenu(c *gin.Context) {} diff --git a/src/modules/system/rest/dto/kickout.go b/src/modules/system/rest/dto/kickout.go new file mode 100644 index 0000000..a7ca494 --- /dev/null +++ b/src/modules/system/rest/dto/kickout.go @@ -0,0 +1,5 @@ +package dto + +type KickOutDTO struct { + Username string `form:"username" json:"username" uri:"username" xml:"username" binding:"required"` +} diff --git a/src/modules/system/rest/dto/login.go b/src/modules/system/rest/dto/login.go new file mode 100644 index 0000000..37eb46e --- /dev/null +++ b/src/modules/system/rest/dto/login.go @@ -0,0 +1,7 @@ +package dto + +type LoginDTO struct { + // binding:"required"修饰的字段,若接收为空值,则报错,是必须字段 + Username string `form:"username" json:"username" uri:"username" xml:"username" binding:"required"` + Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"` +} diff --git a/src/modules/system/rest/user.go b/src/modules/system/rest/user.go new file mode 100644 index 0000000..0627af7 --- /dev/null +++ b/src/modules/system/rest/user.go @@ -0,0 +1,15 @@ +package rest + +import ( + "github.com/gin-gonic/gin" +) + +type UserController struct { +} + +func (UserController) PageUser(c *gin.Context) {} +func (UserController) CreateUser(c *gin.Context) {} +func (UserController) DeleteUser(c *gin.Context) {} +func (UserController) ModifyUserEmail(c *gin.Context) {} +func (UserController) ModifyUserPassword(c *gin.Context) {} +func (UserController) ModifyUserPhone(c *gin.Context) {}