Merge pull request #673 from go-admin-team/dev

Dev
This commit is contained in:
wenjianzhang authored and GitHub committed 2022-07-27 21:36:11 +08:00
commit 70bd8b26ad
13 files changed
+70 -122

No files matched your search

-60
View File
@@ -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'
+18 -16
View File
@@ -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
+2
View File
@@ -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
+3 -3
View File
@@ -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
+1 -1
View File
@@ -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)
:::
+1 -1
View File
@@ -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)
:::
+7 -18
View File
@@ -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()))
}
+17 -4
View File
@@ -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
}
+1 -1
View File
@@ -1,7 +1,7 @@
package service
import (
"github.com/pkg/errors"
"errors"
"time"
"github.com/go-admin-team/go-admin-core/sdk/service"
+9 -11
View File
@@ -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]
}
+8 -3
View File
@@ -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 {
@@ -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,
-1
View File
@@ -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