Skip to content

Commit

Permalink
mod: Update main
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Sep 22, 2022
1 parent 56787bf commit 62cc18b
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/internal/test/data
tmp
/vendor
/configs/rbac_policy.csv
/configs/gen_rbac_policy.csv
/configs/rbac_policy.csv.bak
/test/data
/internal/swagger/v3/.openapi-generator
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM golang:alpine as builder

ARG APP=ginadmin
ARG VERSION=v9.0.0
ARG VERSION=v9.0.1
ARG RELEASE_TAG=$(VERSION)

WORKDIR /go/src/${APP}
COPY . .
RUN go build -ldflags "-w -s -X github.com/LyricTian/gin-admin/v9/cmd.VERSION=${RELEASE_TAG}" -o ./${APP} .
RUN go build -ldflags "-w -s -X main.VERSION=${RELEASE_TAG}" -o ./${APP} .

FROM alpine
ARG APP=ginadmin
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

NOW = $(shell date -u '+%Y%m%d%I%M%S')

RELEASE_VERSION = v9.0.0
RELEASE_VERSION = v9.0.1

APP = ginadmin
SERVER_BIN = ${APP}
Expand All @@ -15,10 +15,10 @@ RELEASE_TAG = $(RELEASE_VERSION).$(GIT_COUNT).$(GIT_HASH)
all: start

start:
@go run -ldflags "-X github.com/LyricTian/gin-admin/v9/cmd.VERSION=$(RELEASE_TAG)" main.go start --configdir ./configs
@go run -ldflags "-X main.VERSION=$(RELEASE_TAG)" main.go start --configdir ./configs

build:
@go build -ldflags "-w -s -X github.com/LyricTian/gin-admin/v9/cmd.VERSION=$(RELEASE_TAG)" -o $(SERVER_BIN)
@go build -ldflags "-w -s -X main.VERSION=$(RELEASE_TAG)" -o $(SERVER_BIN)

wire:
@wire gen ./internal/inject
Expand Down
85 changes: 0 additions & 85 deletions cmd/cmd.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/LyricTian/gin-admin/v9/internal"
"github.com/LyricTian/gin-admin/v9/internal/config"
"github.com/LyricTian/gin-admin/v9/internal/x/utilx"
"github.com/LyricTian/gin-admin/v9/pkg/logger"
"github.com/LyricTian/gin-admin/v9/pkg/x/gormx"

Expand Down Expand Up @@ -47,7 +48,7 @@ var StartCmd = &cli.Command{
}()

ctx := logger.NewTag(context.Background(), "start")
return Run(ctx, func() (func(), error) {
return utilx.Run(ctx, func() (func(), error) {
cfgDir := c.String("configdir")
if cfgDir == "" {
cfgDir = "configs"
Expand All @@ -62,7 +63,6 @@ var StartCmd = &cli.Command{
daemon := c.Bool("deamon")
staticDir := c.String("staticdir")
logger.Context(ctx).Info("Starting server",
zap.String("version", VERSION),
zap.String("configdir", cfgDir),
zap.String("staticdir", staticDir),
zap.Int("pid", os.Getpid()),
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ require (
github.com/google/uuid v1.3.0
github.com/google/wire v0.5.0
github.com/jinzhu/copier v0.3.2
github.com/jinzhu/inflection v1.0.0
github.com/json-iterator/go v1.1.12
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
github.com/rs/xid v1.2.1
github.com/sony/sonyflake v1.0.0
github.com/stretchr/testify v1.8.0
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.1
github.com/swaggo/swag v1.8.4
github.com/urfave/cli/v2 v2.11.1
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
golang.org/x/text v0.3.7
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/natefinch/lumberjack.v2 v2.0.0
Expand Down Expand Up @@ -74,7 +78,6 @@ require (
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.11.0 // indirect
github.com/jackc/pgx/v4 v4.16.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
Expand All @@ -87,17 +90,13 @@ require (
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a // indirect
github.com/swaggo/gin-swagger v1.5.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.10 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g=
github.com/gin-contrib/cors v1.4.0/go.mod h1:bs9pNM0x/UsmHPBWT2xZz9ROh8xYjYkiURUfmBoMlcs=
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
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=
Expand Down Expand Up @@ -327,7 +328,6 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
30 changes: 23 additions & 7 deletions internal/module/rbac/casbinx.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,37 @@ func (a *Casbinx) Load(ctx context.Context) error {
}
}

policyFile := filepath.Join(config.C.General.ConfigDir, "rbac_policy.csv")
_ = os.Rename(policyFile, policyFile+".bak")
// Load the local policy model
policyFile := filepath.Join(config.C.General.ConfigDir, "policy.csv")
if _, err := os.Stat(policyFile); err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
policyBuf, err := ioutil.ReadFile(policyFile)
if err != nil {
return err
} else if len(policyBuf) > 0 {
buf.WriteByte('\n')
buf.Write(policyBuf)
}
}

genPolicyFile := filepath.Join(config.C.General.ConfigDir, "gen_rbac_policy.csv")
_ = os.Rename(genPolicyFile, genPolicyFile+".bak")

err = ioutil.WriteFile(policyFile, buf.Bytes(), 0666)
err = ioutil.WriteFile(genPolicyFile, buf.Bytes(), 0666)
if err != nil {
logger.Context(ctx).Error("write rbac policy file error", zap.String("file", policyFile), zap.Error(err))
logger.Context(ctx).Error("Failed to write policy file", zap.String("file", genPolicyFile), zap.Error(err))
return err
}

// set readonly
_ = os.Chmod(policyFile, 0444)
_ = os.Chmod(genPolicyFile, 0444)

// load casbin
modelFile := filepath.Join(config.C.General.ConfigDir, "casbin_model.conf")
e, err := casbin.NewEnforcer(modelFile, policyFile)
e, err := casbin.NewEnforcer(modelFile, genPolicyFile)
if err != nil {
return err
}
Expand All @@ -78,7 +94,7 @@ func (a *Casbinx) Load(ctx context.Context) error {
e.EnableLog(config.C.Middleware.Casbin.Debug)
a.enforcer = e

logger.Context(ctx).Info("Load casbin success", zap.String("file", policyFile))
logger.Context(ctx).Info("Load casbin success", zap.String("file", genPolicyFile))

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2237,12 +2237,12 @@ const docTemplate = `{

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "9.0.1",
Version: "v9.0.1",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "ginadmin",
Description: "A simple, modular, high-performance RBAC development framework built on golang.",
Title: "Bobber DevOps",
Description: "A DevOps platform based on golang for service monitoring, configuration center, log search, etc.",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
}
Expand Down
6 changes: 3 additions & 3 deletions internal/swagger/swagger.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"swagger": "2.0",
"info": {
"description": "A simple, modular, high-performance RBAC development framework built on golang.",
"title": "ginadmin",
"description": "A DevOps platform based on golang for service monitoring, configuration center, log search, etc.",
"title": "Bobber DevOps",
"contact": {},
"version": "9.0.1"
"version": "v9.0.1"
},
"paths": {
"/api/rbac/v1/current/logout": {
Expand Down
8 changes: 4 additions & 4 deletions internal/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ definitions:
type: object
info:
contact: {}
description: A simple, modular, high-performance RBAC development framework built
on golang.
title: ginadmin
version: 9.0.1
description: A DevOps platform based on golang for service monitoring, configuration
center, log search, etc.
title: Bobber DevOps
version: v9.0.1
paths:
/api/rbac/v1/current/logout:
post:
Expand Down
37 changes: 37 additions & 0 deletions internal/x/utilx/utilx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,48 @@ package utilx

import (
"context"
"os"
"os/signal"
"syscall"
"time"

"github.com/LyricTian/gin-admin/v9/internal/config"
"github.com/LyricTian/gin-admin/v9/internal/x/contextx"
"github.com/LyricTian/gin-admin/v9/pkg/logger"
"go.uber.org/zap"
)

func IsRootUser(ctx context.Context) bool {
return contextx.FromUserID(ctx) == config.C.Dictionary.RootUser.ID
}

func Run(ctx context.Context, handler func() (func(), error)) error {
state := 1
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
cleanFn, err := handler()
if err != nil {
return err
}

EXIT:
for {
sig := <-sc
logger.Context(ctx).Info("Received signal", zap.String("signal", sig.String()))

switch sig {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
state = 0
break EXIT
case syscall.SIGHUP:
default:
break EXIT
}
}

cleanFn()
logger.Context(ctx).Info("Server exit, bye...")
time.Sleep(time.Millisecond * 100)
os.Exit(state)
return nil
}
Loading

0 comments on commit 62cc18b

Please sign in to comment.