diff --git a/.github/workflows/dev-lwx.yml b/.github/workflows/dev-lwx.yml deleted file mode 100644 index b62a4c6b..00000000 --- a/.github/workflows/dev-lwx.yml +++ /dev/null @@ -1,60 +0,0 @@ -name: build - -on: - push: - branches: [ dev-lwx ] - pull_request: - branches: [ dev-lwx ] - -jobs: - build: - name: Build - runs-on: ubuntu-latest - steps: - - name: Set up Go 1.15 - uses: actions/setup-go@v1 - with: - go-version: 1.15 - id: go - - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - name: Get dependencies - run: | - go get -v -t -d ./... - if [ -f Gopkg.toml ]; then - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - dep ensure - fi - - name: Build - run: | - CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -a -installsuffix cgo -o go-admin . - mv go-admin ./scripts - - - uses: Azure/docker-login@v1 - with: - login-server: registry.cn-shanghai.aliyuncs.com - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_PASSWORD }} - - run: | - docker build ./scripts -t registry.cn-shanghai.aliyuncs.com/go-admin-team/go-admin:${{ github.sha }} - docker push registry.cn-shanghai.aliyuncs.com/go-admin-team/go-admin:${{ github.sha }} - - uses: Azure/k8s-set-context@v1 - with: - kubeconfig: ${{ secrets.KUBE_CONFIG }} - - - uses: Azure/k8s-create-secret@v1 - with: - namespace: 'go-admin' - container-registry-url: registry.cn-shanghai.aliyuncs.com - container-registry-username: ${{ secrets.REGISTRY_USERNAME }} - container-registry-password: ${{ secrets.REGISTRY_PASSWORD }} - secret-name: aliyuncs-k8s-secret - - - uses: Azure/k8s-deploy@v1 - with: - namespace: 'go-admin' - manifests: 'scripts/k8s/deploy.yml' - images: 'registry.cn-shanghai.aliyuncs.com/go-admin-team/go-admin:${{ github.sha }}' - imagepullsecrets: 'aliyuncs-k8s-secret' - kubectl-version: 'latest' diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index bf741c08..123e1ebf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,29 +16,23 @@ jobs: runs-on: ubuntu-latest steps: - - name: Set up Go 1.14 - uses: actions/setup-go@v1 + - name: Set up Go 1.15 + uses: actions/setup-go@v3 with: - go-version: 1.14 + go-version: 1.15 id: go - name: Check out code into the Go module directory - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Get dependencies - run: | - go get -v -t -d ./... - if [ -f Gopkg.toml ]; then - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - dep ensure - fi + run: go mod tidy - name: Build - run: | - ref=${{ github.ref }} - CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -a -installsuffix cgo -o go-admin . + run: make build - name: Log in to the Container registry - uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + uses: docker/login-action@v2 + if: startsWith(${{github.ref}}, 'refs/tags/') with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} @@ -46,12 +40,20 @@ jobs: - name: Extract metadata (tags, labels) for Docker id: meta - uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + if: startsWith(${{github.ref}}, 'refs/tags/') + uses: docker/metadata-action@v4 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + flavor: | + latest=auto + tags: | + type=schedule + type=ref,event=tag + type=sha,prefix=,format=long,enable=true,priority=100 - name: Build and push Docker image - uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + uses: docker/build-push-action@v3 + if: startsWith(${{github.ref}}, 'refs/tags/') with: context: . file: scripts/Dockerfile diff --git a/Dockerfile b/Dockerfile index 430e91ef..c5e33a5c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,6 +19,8 @@ FROM alpine COPY --from=builder /go/release/go-admin / +COPY --from=builder /go/release/config/settings.yml /config/settings.yml + COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /etc/localtime EXPOSE 8000 diff --git a/Makefile b/Makefile index 24509c97..135c7e70 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,11 @@ PROJECT:=go-admin .PHONY: build build: - CGO_ENABLED=0 go build -o go-admin main.go + CGO_ENABLED=0 go build -ldflags="-w -s" -a -installsuffix -o go-admin . build-linux: - env GOOS=linux GOARCH=amd64 go build + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -a -installsuffix -o go-admin . build-sqlite: - go build -tags sqlite3 -o go-admin main.go + go build -tags sqlite3 -ldflags="-w -s" -a -installsuffix -o go-admin . #.PHONY: test #test: # go test -v ./... -cover diff --git a/README.Zh-cn.md b/README.Zh-cn.md index 1e1f3e6f..ff2b8c95 100644 --- a/README.Zh-cn.md +++ b/README.Zh-cn.md @@ -158,7 +158,7 @@ D:\Code\go-admin>go build cgo: exec gcc: exec: "gcc": executable file not found in %PATH% ``` -[解决cgo问题进入](https://doc.go-admin.dev/guide/other/faq.html#_5-cgo-exec-missing-cc-exec-missing-cc-file-does-not-exist) +[解决cgo问题进入](https://doc.go-admin.dev/zh-CN/guide/faq#cgo-%E7%9A%84%E9%97%AE%E9%A2%98) ::: diff --git a/README.md b/README.md index c151f080..efabf308 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ D:\Code\go-admin>go build cgo: exec gcc: exec: "gcc": executable file not found in %PATH% ``` -[Solve the cgo problem and enter](https://doc.go-admin.dev/guide/other/faq.html#_5-cgo-exec-missing-cc-exec-missing-cc-file-does-not-exist) +[Solve the cgo problem and enter](https://doc.go-admin.dev/guide/faq#cgo-%E7%9A%84%E9%97%AE%E9%A2%98) ::: diff --git a/app/admin/apis/sys_role.go b/app/admin/apis/sys_role.go index 507132e9..1a1e75b0 100644 --- a/app/admin/apis/sys_role.go +++ b/app/admin/apis/sys_role.go @@ -2,10 +2,11 @@ package apis import ( "fmt" + "net/http" + "github.com/gin-gonic/gin/binding" "github.com/go-admin-team/go-admin-core/sdk" "go-admin/app/admin/models" - "net/http" "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin-core/sdk/api" @@ -14,7 +15,6 @@ import ( "go-admin/app/admin/service" "go-admin/app/admin/service/dto" - "go-admin/common/global" ) type SysRole struct { @@ -128,11 +128,7 @@ func (e SysRole) Insert(c *gin.Context) { e.Error(500, err, "创建失败,"+err.Error()) return } - _, err = global.LoadPolicy(c) - if err != nil { - e.Error(500, err, "") - return - } + e.OK(req.GetId(), "创建成功") } @@ -168,11 +164,7 @@ func (e SysRole) Update(c *gin.Context) { e.Logger.Error(err) return } - _, err = global.LoadPolicy(c) - if err != nil { - e.Error(500, err, "") - return - } + e.OK(req.GetId(), "更新成功") } @@ -198,17 +190,14 @@ func (e SysRole) Delete(c *gin.Context) { return } - err = s.Remove(&req) + cb := sdk.Runtime.GetCasbinKey(c.Request.Host) + err = s.Remove(&req, cb) if err != nil { e.Logger.Error(err) e.Error(500, err, "") return } - _, err = global.LoadPolicy(c) - if err != nil { - e.Error(500, err, fmt.Sprintf("删除角色 %v 失败,失败信息 %s", req.GetId(), err.Error())) - return - } + e.OK(req.GetId(), fmt.Sprintf("删除角色角色 %v 状态成功!", req.GetId())) } diff --git a/app/admin/service/sys_role.go b/app/admin/service/sys_role.go index 6c514045..b87d20b9 100644 --- a/app/admin/service/sys_role.go +++ b/app/admin/service/sys_role.go @@ -2,6 +2,7 @@ package service import ( "errors" + "github.com/go-admin-team/go-admin-core/sdk/config" "gorm.io/gorm/clause" @@ -102,12 +103,21 @@ func (e *SysRole) Insert(c *dto.SysRoleInsertReq, cb *casbin.SyncedEnforcer) err return err } + mp := make(map[string]interface{}, 0) + polices := make([][]string, 0) for _, menu := range dataMenu { for _, api := range menu.SysApi { - _, err = cb.AddNamedPolicy("p", data.RoleKey, api.Path, api.Action) + if mp[data.RoleKey+"-"+api.Path+"-"+api.Action] != "" { + mp[data.RoleKey+"-"+api.Path+"-"+api.Action] = "" + polices = append(polices, []string{data.RoleKey, api.Path, api.Action}) + } } } - _ = cb.SavePolicy() + _, err = cb.AddNamedPolicies("p", polices) + if err != nil { + return err + } + //if len(c.MenuIds) > 0 { // s := SysRoleMenu{} // s.Orm = e.Orm @@ -176,12 +186,12 @@ func (e *SysRole) Update(c *dto.SysRoleUpdateReq, cb *casbin.SyncedEnforcer) err if err != nil { return err } - _ = cb.SavePolicy() + return nil } // Remove 删除SysRole -func (e *SysRole) Remove(c *dto.SysRoleDeleteReq) error { +func (e *SysRole) Remove(c *dto.SysRoleDeleteReq, cb *casbin.SyncedEnforcer) error { var err error tx := e.Orm if config.DatabaseConfig.Driver != "sqlite3" { @@ -205,6 +215,9 @@ func (e *SysRole) Remove(c *dto.SysRoleDeleteReq) error { if db.RowsAffected == 0 { return errors.New("无权更新该数据") } + + _, _ = cb.RemoveFilteredPolicy(0, model.RoleKey) + return nil } diff --git a/app/jobs/service/sys_job.go b/app/jobs/service/sys_job.go index 9d7268ea..a1ab8108 100644 --- a/app/jobs/service/sys_job.go +++ b/app/jobs/service/sys_job.go @@ -1,7 +1,7 @@ package service import ( - "github.com/pkg/errors" + "errors" "time" "github.com/go-admin-team/go-admin-core/sdk/service" diff --git a/cmd/migrate/migration/init.go b/cmd/migrate/migration/init.go index 541f1d6b..739e6b40 100644 --- a/cmd/migrate/migration/init.go +++ b/cmd/migrate/migration/init.go @@ -4,20 +4,18 @@ import ( "log" "path/filepath" "sort" - "strconv" "sync" - "github.com/spf13/cast" "gorm.io/gorm" ) var Migrate = &Migration{ - version: make(map[int]func(db *gorm.DB, version string) error), + version: make(map[string]func(db *gorm.DB, version string) error), } type Migration struct { db *gorm.DB - version map[int]func(db *gorm.DB, version string) error + version map[string]func(db *gorm.DB, version string) error mutex sync.Mutex } @@ -29,19 +27,19 @@ func (e *Migration) SetDb(db *gorm.DB) { e.db = db } -func (e *Migration) SetVersion(k int, f func(db *gorm.DB, version string) error) { +func (e *Migration) SetVersion(k string, f func(db *gorm.DB, version string) error) { e.mutex.Lock() defer e.mutex.Unlock() e.version[k] = f } func (e *Migration) Migrate() { - versions := make([]int, 0) + versions := make([]string, 0) for k := range e.version { versions = append(versions, k) } - if !sort.IntsAreSorted(versions) { - sort.Ints(versions) + if !sort.StringsAreSorted(versions) { + sort.Strings(versions) } var err error var count int64 @@ -55,14 +53,14 @@ func (e *Migration) Migrate() { count = 0 continue } - err = (e.version[v])(e.db, strconv.Itoa(v)) + err = (e.version[v])(e.db.Debug(), v) if err != nil { log.Fatalln(err) } } } -func GetFilename(s string) int { +func GetFilename(s string) string { s = filepath.Base(s) - return cast.ToInt(s[:13]) + return s[:13] } diff --git a/cmd/migrate/migration/models/initdb.go b/cmd/migrate/migration/models/initdb.go index 644ba499..a9345031 100644 --- a/cmd/migrate/migration/models/initdb.go +++ b/cmd/migrate/migration/models/initdb.go @@ -3,10 +3,11 @@ package models import ( "fmt" "go-admin/common/global" - "gorm.io/gorm" "io/ioutil" "log" "strings" + + "gorm.io/gorm" ) func InitDb(db *gorm.DB) (err error) { @@ -16,9 +17,13 @@ func InitDb(db *gorm.DB) (err error) { err = ExecSql(db, filePath) } else if global.Driver == "mysql" { filePath = "config/db-begin-mysql.sql" - err = ExecSql(db, filePath) + if err = ExecSql(db, filePath); err != nil { + return err + } filePath = "config/db.sql" - err = ExecSql(db, filePath) + if err = ExecSql(db, filePath); err != nil { + return err + } filePath = "config/db-end-mysql.sql" err = ExecSql(db, filePath) } else { diff --git a/cmd/migrate/migration/version/1599190683659_tables.go b/cmd/migrate/migration/version/1599190683659_tables.go index 09a8e0e0..64780788 100644 --- a/cmd/migrate/migration/version/1599190683659_tables.go +++ b/cmd/migrate/migration/version/1599190683659_tables.go @@ -3,11 +3,11 @@ package version import ( "runtime" - "gorm.io/gorm" - "go-admin/cmd/migrate/migration" "go-admin/cmd/migrate/migration/models" common "go-admin/common/models" + + "gorm.io/gorm" ) func init() { @@ -41,7 +41,7 @@ func _1599190683659Tables(db *gorm.DB, version string) error { return err } if err := models.InitDb(tx); err != nil { - + return err } return tx.Create(&common.Migration{ Version: version, diff --git a/go.mod b/go.mod index 0632556b..22321ed9 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,6 @@ require ( github.com/qiniu/go-sdk/v7 v7.11.1 github.com/robfig/cron/v3 v3.0.1 github.com/shirou/gopsutil/v3 v3.22.1 - github.com/spf13/cast v1.3.1 github.com/spf13/cobra v1.0.0 github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/swag v1.6.7