Skip to content

Commit

Permalink
feat: add user and jwt authentication and sample code
Browse files Browse the repository at this point in the history
- add casbin permission and sample code
  • Loading branch information
OldSmokeGun committed Sep 5, 2023
1 parent 259c4b5 commit 7de26a1
Show file tree
Hide file tree
Showing 176 changed files with 21,446 additions and 2,010 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ var watchKeys = []string{

# 日志

日志基于 [slog](https://pkg.go.dev/golang.org/x/exp/slog),日志的切割基于 [file-rotatelogs](https://github.com/lestrrat-go/file-rotatelogs)
日志基于 [slog](https://pkg.go.dev/log/slog),日志的切割基于 [file-rotatelogs](https://github.com/lestrrat-go/file-rotatelogs)

日志内容默认输出到 `logs` 目录中,并且根据每天的日期进行分割

Expand All @@ -198,7 +198,7 @@ var watchKeys = []string{
```go
package v1

import "golang.org/x/exp/slog"
import "log/slog"

type Handler struct {
logger *slog.Logger
Expand Down
26 changes: 11 additions & 15 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ version: '3'
vars:
APP_BIN_PATH: bin/app
APP_MAIN_DIR: cmd
# wire
WIRE_ENTRY_DIR: ./internal/command
# swagger
API_SWAGGER_SCAN_DIR: internal/app/adapter/server/http
API_SWAGGER_SCAN_ENTRY: http.go
API_SWAGGER_OUT_DIR: internal/app/adapter/server/http/api/docs
# proto
API_PROTO_DIR: internal/app/adapter/server/grpc/api
API_PROTO_PB_DIR: internal/app/adapter/server/grpc/api
API_PROTO_GENERATE_FILE: internal/app/adapter/server/grpc/api/generate.go

tasks:
build:
Expand Down Expand Up @@ -45,6 +46,10 @@ tasks:
desc: build the files required for the application
cmd: go generate ./...

wire:
desc: automatically inject dependencies
cmd: wire {{.WIRE_ENTRY_DIR}}

download:
desc: download the dependencies required for compilation
run: once
Expand Down Expand Up @@ -88,20 +93,11 @@ tasks:
proto:
desc: generate the proto file
silent: true
cmds:
- |
for f in $(find {{.API_PROTO_DIR}} -name *.proto)
do
kratos proto client --proto_path=./proto ${f}
done
# inject tag
- |
for f in $(find {{.API_PROTO_PB_DIR}} -name *.pb.go)
do
protoc-go-inject-tag -input=${f}
done
cmd: go generate {{.API_PROTO_GENERATE_FILE}}

format:
desc: format all go files
silent: true
cmd: goimports-reviser -imports-order std,project,company,general -format -recursive ./...
cmds:
- gofmt -w .
- goimports-reviser -imports-order std,company,general,project -format -recursive ./...
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
klog "github.com/go-kratos/kratos/v2/log"

"go-scaffold/internal/command"
_ "go-scaffold/pkg/dump"
"go-scaffold/pkg/log"
iklog "go-scaffold/pkg/log/kratos"

klog "github.com/go-kratos/kratos/v2/log"
)

func main() {
Expand Down
4 changes: 4 additions & 0 deletions dbconfig.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
development:
dialect: mysql
datasource: host=${DB_HOST} dbname=${DB_NAME} user=${DB_USER} password=${DB_PASSWORD} sslmode=required
dir: migrations
9 changes: 3 additions & 6 deletions etc/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ http:
addr: "0.0.0.0:9527"
timeout: 5
externalAddr: "" # external access address, such as reverse proxy
# jwt:
# key: ""
# casbin:
# model:
# path: "etc/rbac_model.conf"
# adapter:
# file:
# path: "etc/rbac_policy.csv"
# gorm:
# tableName: ""
# file: "etc/rbac_policy.csv"
# gorm: {}
# ent: {}

grpc:
server:
Expand Down
6 changes: 3 additions & 3 deletions etc/rbac_model.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[request_definition]
r = sub, obj, act
r = sub, obj

[policy_definition]
p = sub, obj, act
p = sub, obj

[role_definition]
g = _, _
Expand All @@ -11,4 +11,4 @@ g = _, _
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && keyMatch3(r.obj, p.obj) && r.act == p.act
m = r.sub == 'user_1' || g(r.sub, p.sub) && r.obj == p.obj
78 changes: 45 additions & 33 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module go-scaffold

go 1.20
go 1.21

require (
entgo.io/ent v0.11.7
entgo.io/ent v0.12.3
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/bwmarrin/snowflake v0.3.0
github.com/casbin/casbin/v2 v2.66.1
github.com/casbin/gorm-adapter/v3 v3.15.0
github.com/casbin/casbin/v2 v2.77.1
github.com/casbin/ent-adapter v0.3.0
github.com/casbin/gorm-adapter/v3 v3.18.1
github.com/davecgh/go-spew v1.1.1
github.com/fatih/color v1.14.1
github.com/go-kratos/consul v0.1.5
Expand All @@ -17,7 +18,9 @@ require (
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-redis/redismock/v8 v8.11.5
github.com/go-sql-driver/mysql v1.7.0
github.com/go-sql-driver/mysql v1.7.1
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/uuid v1.3.1
github.com/google/wire v0.5.1-0.20220620021424-0675cdc9191c
github.com/hashicorp/consul/api v1.18.0
github.com/jackc/pgx v3.6.2+incompatible
Expand All @@ -30,7 +33,7 @@ require (
github.com/rubenv/sql-migrate v1.4.0
github.com/samber/lo v1.37.0
github.com/segmentio/kafka-go v0.4.39
github.com/spf13/cobra v1.6.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/swaggo/echo-swagger v1.3.5
github.com/swaggo/swag v1.16.1
Expand All @@ -40,33 +43,31 @@ require (
go.opentelemetry.io/otel/exporters/jaeger v1.12.0
go.opentelemetry.io/otel/sdk v1.12.0
go.opentelemetry.io/otel/trace v1.14.0
golang.org/x/exp v0.0.0-20230307190834-24139beb5833
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
gorm.io/driver/mysql v1.4.7
gorm.io/driver/postgres v1.5.0
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11
gorm.io/plugin/dbresolver v1.4.1
gorm.io/driver/mysql v1.5.1
gorm.io/driver/postgres v1.5.2
gorm.io/gorm v1.25.4
gorm.io/plugin/dbresolver v1.4.7
gorm.io/plugin/soft_delete v1.2.0
)

require (
ariga.io/atlas v0.9.1-0.20230119145809-92243f7c55cb // indirect
ariga.io/atlas v0.13.3 // indirect
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apolloconfig/agollo/v4 v4.3.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/armon/go-metrics v0.4.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/frankban/quicktest v1.14.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/glebarez/go-sqlite v1.21.1 // indirect
github.com/glebarez/sqlite v1.7.0 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/glebarez/sqlite v1.9.0 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -84,35 +85,41 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.4.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.16.0 // indirect
github.com/hashicorp/hcl/v2 v2.18.0 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgconn v1.14.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.1 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.1 // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/microsoft/go-mssqldb v0.21.0 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -126,29 +133,34 @@ require (
github.com/spf13/viper v1.15.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/swaggo/files v1.0.0 // indirect
github.com/tidwall/gjson v1.16.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/zclconf/go-cty v1.12.1 // indirect
github.com/zclconf/go-cty v1.14.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.7 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.7 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/goleak v1.1.12 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.1 // indirect
google.golang.org/genproto v0.0.0-20230131230820-1c016267d619 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/sqlserver v1.4.2 // indirect
modernc.org/libc v1.22.3 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/sqlite v1.21.1 // indirect
gorm.io/driver/sqlserver v1.5.1 // indirect
modernc.org/libc v1.24.1 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.1 // indirect
modernc.org/sqlite v1.25.0 // indirect
)
Loading

0 comments on commit 7de26a1

Please sign in to comment.