Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove global pkg and dependency #35

Merged
merged 8 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ require (
github.com/go-playground/validator/v10 v10.9.0
github.com/go-redis/redis/v8 v8.11.4
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/uuid v1.1.2
github.com/labstack/echo-contrib v0.11.0
github.com/labstack/echo/v4 v4.6.3
github.com/labstack/gommon v0.3.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.0
github.com/stretchr/testify v1.7.0
github.com/valyala/fasttemplate v1.2.1
go.mongodb.org/mongo-driver v1.8.2
gorm.io/datatypes v1.0.5
gorm.io/driver/mysql v1.2.3
Expand Down Expand Up @@ -63,7 +65,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down
16 changes: 6 additions & 10 deletions internal/project/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"demo/framework/bean"
/*#bean.replace("{{ .PkgPath }}/framework/bean")**/
/**#bean*/
"demo/framework/internals/global"
/*#bean.replace("{{ .PkgPath }}/framework/internals/global")**/
/**#bean*/
beanValidator "demo/framework/internals/validator"
/*#bean.replace(beanValidator "{{ .PkgPath }}/framework/internals/validator")**/
/**#bean*/
Expand Down Expand Up @@ -55,17 +52,16 @@ func init() {
func start(cmd *cobra.Command, args []string) {
// Create a bean object
b := bean.New()
global.EchoInstance = b.Echo
global.DBConn = (*global.DBDeps)(b.DBConn)

b.InitDB()

b.BeforeServe = func() {
// init global middleware if you need
// Init global middleware if you need
// middlerwares.Init()

// init router
routers.Init()
// Init DB dependency.
b.InitDB()

// Init different routes.
routers.Init(b)
}

// Below is an example of how you can initialize your own validator. Just create a new directory
Expand Down
7 changes: 3 additions & 4 deletions internal/project/env.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"beanVersion": "{{ .BeanVersion }}",
"packagePath": "{{ .PkgPath }}",
"projectName": "{{ .PkgName }}",
"projectVersion": "1.0",
"environment": "local",
"isLogStdout": false,
"logFile": "logs/console.log",
"isBodyDump": true,
"debugLog": "",
"requestLog": "",
"bodydumpLog": "",
"prometheus": {
"isPrometheusMetrics": false,
"skipEndpoints": ["/ping", "/route/stats"]
Expand Down
1 change: 0 additions & 1 deletion internal/project/framework/bean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func (b *Bean) InitDB() {

isTenant := viper.GetBool("database.mysql.isTenant")
if isTenant {
dbdrivers.InitTenantIdMutexMap()
masterMySQLDB, masterMySQLDBName = dbdrivers.InitMysqlMasterConn()
tenantMySQLDBs, tenantMySQLDBNames = dbdrivers.InitMysqlTenantConns(masterMySQLDB)
tenantMongoDBs, tenantMongoDBNames = dbdrivers.InitMongoTenantConns(masterMySQLDB)
Expand Down
63 changes: 9 additions & 54 deletions internal/project/framework/internals/async/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@
package async

import (
"fmt"
"reflect"

/**#bean*/
"demo/framework/internals/global"
/*#bean.replace("{{ .PkgPath }}/framework/internals/global")**/
"demo/framework/internals/sentry"
/*#bean.replace("{{ .PkgPath }}/framework/internals/sentry")**/

"github.com/getsentry/sentry-go"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
)

// `Execute` provides a safe way to execute a function asynchronously, recovering if they panic
type Task func(c echo.Context)

// `Execute` provides a safe way to execute a function asynchronously, recovering if they panic
// and provides all error stack aiming to facilitate fail causes discovery.
func Execute(fn func(ctx echo.Context)) {
func Execute(fn Task, e *echo.Echo) {

go func() {
// Acquire a context from global echo instance and reset it to avoid race condition.
c := global.EchoInstance.AcquireContext()
c := e.AcquireContext()
c.Reset(nil, nil)

defer recoverPanic(c)

fn(c)
}()
}
Expand All @@ -35,49 +30,9 @@ func Execute(fn func(ctx echo.Context)) {
func recoverPanic(c echo.Context) {

if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%v", r)
}

// Run this function synchronously to release the `context` properly.
sendErrorToSentry(c, err)
sentry.PushData(c, r, nil, false)
}

// Release the acquired context.
global.EchoInstance.ReleaseContext(c)
}

// This function is only use in this package/file to avoid import cycle.
// For normal sentry usage, please refer to the `{{ .PkgPath }}/internals/sentry` package.
func sendErrorToSentry(c echo.Context, err error) {

isSentry := viper.GetBool("sentry.isSentry")
sentryDSN := viper.GetString("sentry.dsn")

if !isSentry || sentryDSN == "" {
global.EchoInstance.Logger.Error(err)
return
}

// IMPORTANT: Clone the current sentry hub from the echo context before it's gone.
hub := sentryecho.GetHubFromContext(c)

if hub == nil {
hub = sentry.CurrentHub().Clone()
}

client, scope := hub.Client(), hub.Scope()

event := sentry.NewEvent()
event.Level = sentry.LevelError
event.Exception = []sentry.Exception{
{
Value: err.Error(),
Type: reflect.TypeOf(err).String(),
Stacktrace: sentry.NewStacktrace(),
},
}

client.CaptureEvent(event, &sentry.EventHint{RecoveredException: err}, scope)
c.Echo().ReleaseContext(c)
}
82 changes: 0 additions & 82 deletions internal/project/framework/internals/cli/cli.go

This file was deleted.

34 changes: 0 additions & 34 deletions internal/project/framework/internals/global/constants.go

This file was deleted.

Loading