From 44d3ed8f1b67288853367b475f7c8c409de8996d Mon Sep 17 00:00:00 2001 From: Yonghwan SO Date: Tue, 27 Sep 2022 23:33:24 +0900 Subject: [PATCH 1/2] applied sync.Once to make 'app' to be the only root App --- .../actions/_fixtures/inputs/clean/actions/app.go | 11 ++++++++--- .../_fixtures/outputs/clean/actions/app.go.tmpl | 11 ++++++++--- .../_fixtures/outputs/multi/actions/app.go.tmpl | 11 ++++++++--- internal/genny/actions/actions.go | 2 +- .../genny/newapp/api/templates/actions/app.go.tmpl | 11 +++++++---- .../genny/newapp/core/templates/actions/app.go.tmpl | 12 +++++++++--- .../genny/newapp/web/templates/actions/app.go.tmpl | 10 ++++++---- .../resource/_fixtures/default/actions/app.go.tmpl | 10 ++++++---- .../resource/_fixtures/nested/actions/app.go.tmpl | 10 ++++++---- internal/genny/resource/actions.go | 2 +- 10 files changed, 60 insertions(+), 30 deletions(-) diff --git a/internal/genny/actions/_fixtures/inputs/clean/actions/app.go b/internal/genny/actions/_fixtures/inputs/clean/actions/app.go index ebfc56cd..f7a963fe 100644 --- a/internal/genny/actions/_fixtures/inputs/clean/actions/app.go +++ b/internal/genny/actions/_fixtures/inputs/clean/actions/app.go @@ -1,18 +1,23 @@ package actions import ( + "sync" + "github.com/gobuffalo/buffalo" ) -var app *buffalo.App +var ( + app *buffalo.App + appOnce sync.Once +) func App() *buffalo.App { - if app == nil { + appOnce.Do(func() { app = buffalo.New(buffalo.Options{}) app.GET("/", HomeHandler) app.ServeFiles("/", assetsBox) // serve files from the public directory - } + }) return app } diff --git a/internal/genny/actions/_fixtures/outputs/clean/actions/app.go.tmpl b/internal/genny/actions/_fixtures/outputs/clean/actions/app.go.tmpl index 26fe0fe6..ea535a82 100644 --- a/internal/genny/actions/_fixtures/outputs/clean/actions/app.go.tmpl +++ b/internal/genny/actions/_fixtures/outputs/clean/actions/app.go.tmpl @@ -1,19 +1,24 @@ package actions import ( + "sync" + "github.com/gobuffalo/buffalo" ) -var app *buffalo.App +var ( + app *buffalo.App + appOnce sync.Once +) func App() *buffalo.App { - if app == nil { + appOnce.Do(func() { app = buffalo.New(buffalo.Options{}) app.GET("/", HomeHandler) app.GET("/user/index", UserIndex) app.ServeFiles("/", assetsBox) // serve files from the public directory - } + }) return app } diff --git a/internal/genny/actions/_fixtures/outputs/multi/actions/app.go.tmpl b/internal/genny/actions/_fixtures/outputs/multi/actions/app.go.tmpl index 137b3a9d..e68fa94b 100644 --- a/internal/genny/actions/_fixtures/outputs/multi/actions/app.go.tmpl +++ b/internal/genny/actions/_fixtures/outputs/multi/actions/app.go.tmpl @@ -1,20 +1,25 @@ package actions import ( + "sync" + "github.com/gobuffalo/buffalo" ) -var app *buffalo.App +var ( + app *buffalo.App + appOnce sync.Once +) func App() *buffalo.App { - if app == nil { + appOnce.Do(func() { app = buffalo.New(buffalo.Options{}) app.GET("/", HomeHandler) app.GET("/user/show", UserShow) app.GET("/user/edit", UserEdit) app.ServeFiles("/", assetsBox) // serve files from the public directory - } + }) return app } diff --git a/internal/genny/actions/actions.go b/internal/genny/actions/actions.go index a9a1d763..bbba121a 100644 --- a/internal/genny/actions/actions.go +++ b/internal/genny/actions/actions.go @@ -76,7 +76,7 @@ func updateApp(pres *presenter) genny.RunFn { lines = append(lines, e) } } - f, err = gogen.AddInsideBlock(f, "app == nil", strings.Join(lines, "\n\t\t")) + f, err = gogen.AddInsideBlock(f, "appOnce.Do(func() {", strings.Join(lines, "\n\t\t")) if err != nil { return err } diff --git a/internal/genny/newapp/api/templates/actions/app.go.tmpl b/internal/genny/newapp/api/templates/actions/app.go.tmpl index 60a03c87..50b23c78 100644 --- a/internal/genny/newapp/api/templates/actions/app.go.tmpl +++ b/internal/genny/newapp/api/templates/actions/app.go.tmpl @@ -1,6 +1,8 @@ package actions import ( + "sync" + {{ if .opts.App.WithPop -}} "{{ .opts.App.ModelsPkg }}" {{ end -}} @@ -24,8 +26,9 @@ import ( var ENV = envy.Get("GO_ENV", "development") var ( - app *buffalo.App - T *i18n.Translator + app *buffalo.App + appOnce sync.Once + T *i18n.Translator ) // App is where all routes and middleware for buffalo @@ -42,7 +45,7 @@ var ( // placed last in the route declarations, as it will prevent routes // declared after it to never be called. func App() *buffalo.App { - if app == nil { + appOnce.Do(func() { app = buffalo.New(buffalo.Options{ Env: ENV, SessionStore: sessions.Null{}, @@ -69,7 +72,7 @@ func App() *buffalo.App { {{ end -}} app.GET("/", HomeHandler) - } + }) return app } diff --git a/internal/genny/newapp/core/templates/actions/app.go.tmpl b/internal/genny/newapp/core/templates/actions/app.go.tmpl index 8168b006..519c8350 100644 --- a/internal/genny/newapp/core/templates/actions/app.go.tmpl +++ b/internal/genny/newapp/core/templates/actions/app.go.tmpl @@ -1,6 +1,8 @@ package actions import ( + "sync" + "github.com/gobuffalo/buffalo" "github.com/gobuffalo/envy" forcessl "github.com/gobuffalo/mw-forcessl" @@ -11,7 +13,11 @@ import ( // ENV is used to help switch settings based on where the // application is being run. Default is "development". var ENV = envy.Get("GO_ENV", "development") -var app *buffalo.App + +var ( + app *buffalo.App + appOnce sync.Once +) // App is where all routes and middleware for buffalo // should be defined. This is the nerve center of your @@ -27,7 +33,7 @@ var app *buffalo.App // placed last in the route declarations, as it will ensure routes // declared after it will never be called. func App() *buffalo.App { - if app == nil { + appOnce.Do(func() { app = buffalo.New(buffalo.Options{ Env: ENV, SessionName: "_{{.opts.App.Name.File}}_session", @@ -40,7 +46,7 @@ func App() *buffalo.App { app.Use(paramlogger.ParameterLogger) app.GET("/", HomeHandler) - } + }) return app } diff --git a/internal/genny/newapp/web/templates/actions/app.go.tmpl b/internal/genny/newapp/web/templates/actions/app.go.tmpl index f63838aa..c132e4c3 100644 --- a/internal/genny/newapp/web/templates/actions/app.go.tmpl +++ b/internal/genny/newapp/web/templates/actions/app.go.tmpl @@ -2,6 +2,7 @@ package actions import ( "net/http" + "sync" "{{ .opts.App.PackagePkg }}/locales" {{ if .opts.App.WithPop -}} @@ -26,8 +27,9 @@ import ( var ENV = envy.Get("GO_ENV", "development") var ( - app *buffalo.App - T *i18n.Translator + app *buffalo.App + appOnce sync.Once + T *i18n.Translator ) // App is where all routes and middleware for buffalo @@ -44,7 +46,7 @@ var ( // placed last in the route declarations, as it will prevent routes // declared after it to never be called. func App() *buffalo.App { - if app == nil { + appOnce.Do(func() { app = buffalo.New(buffalo.Options{ Env: ENV, SessionName: "_{{.opts.App.Name.File}}_session", @@ -73,7 +75,7 @@ func App() *buffalo.App { app.GET("/", HomeHandler) app.ServeFiles("/", http.FS(public.FS())) // serve files from the public directory - } + }) return app } diff --git a/internal/genny/resource/_fixtures/default/actions/app.go.tmpl b/internal/genny/resource/_fixtures/default/actions/app.go.tmpl index fb345755..2900b1a2 100644 --- a/internal/genny/resource/_fixtures/default/actions/app.go.tmpl +++ b/internal/genny/resource/_fixtures/default/actions/app.go.tmpl @@ -2,6 +2,7 @@ package actions import ( "net/http" + "sync" "github.com/markbates/coke/locales" "github.com/markbates/coke/public" @@ -20,8 +21,9 @@ import ( var ENV = envy.Get("GO_ENV", "development") var ( - app *buffalo.App - T *i18n.Translator + app *buffalo.App + appOnce sync.Once + T *i18n.Translator ) // App is where all routes and middleware for buffalo @@ -38,7 +40,7 @@ var ( // placed last in the route declarations, as it will prevent routes // declared after it to never be called. func App() *buffalo.App { - if app == nil { + appOnce.Do(func() { app = buffalo.New(buffalo.Options{ Env: ENV, SessionName: "_resource_session", @@ -61,7 +63,7 @@ func App() *buffalo.App { app.Resource("/widgets", WidgetsResource{}) app.ServeFiles("/", http.FS(public.FS())) // serve files from the public directory - } + }) return app } diff --git a/internal/genny/resource/_fixtures/nested/actions/app.go.tmpl b/internal/genny/resource/_fixtures/nested/actions/app.go.tmpl index 65a44bd5..45ab7a45 100644 --- a/internal/genny/resource/_fixtures/nested/actions/app.go.tmpl +++ b/internal/genny/resource/_fixtures/nested/actions/app.go.tmpl @@ -2,6 +2,7 @@ package actions import ( "net/http" + "sync" "github.com/markbates/coke/locales" "github.com/markbates/coke/public" @@ -20,8 +21,9 @@ import ( var ENV = envy.Get("GO_ENV", "development") var ( - app *buffalo.App - T *i18n.Translator + app *buffalo.App + appOnce sync.Once + T *i18n.Translator ) // App is where all routes and middleware for buffalo @@ -38,7 +40,7 @@ var ( // placed last in the route declarations, as it will prevent routes // declared after it to never be called. func App() *buffalo.App { - if app == nil { + appOnce.Do(func() { app = buffalo.New(buffalo.Options{ Env: ENV, SessionName: "_resource_session", @@ -61,7 +63,7 @@ func App() *buffalo.App { app.Resource("/admin/widgets", AdminWidgetsResource{}) app.ServeFiles("/", http.FS(public.FS())) // serve files from the public directory - } + }) return app } diff --git a/internal/genny/resource/actions.go b/internal/genny/resource/actions.go index 9d947d77..bdeb7715 100644 --- a/internal/genny/resource/actions.go +++ b/internal/genny/resource/actions.go @@ -15,7 +15,7 @@ func addResource(pres presenter) genny.RunFn { return err } stmt := fmt.Sprintf("app.Resource(\"/%s\", %sResource{})", pres.Name.URL(), pres.Name.Resource()) - f, err = gogen.AddInsideBlock(f, "if app == nil {", stmt) + f, err = gogen.AddInsideBlock(f, "appOnce.Do(func() {", stmt) if err != nil { return err } From 46752e217a89f5a2eff11d475b8caf493cf1f770 Mon Sep 17 00:00:00 2001 From: Yonghwan SO Date: Wed, 28 Sep 2022 00:30:28 +0900 Subject: [PATCH 2/2] continue: to be compatible with old versions --- internal/genny/actions/actions.go | 13 ++++++++++++- internal/genny/resource/actions.go | 14 +++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/internal/genny/actions/actions.go b/internal/genny/actions/actions.go index bbba121a..7c6cf7d3 100644 --- a/internal/genny/actions/actions.go +++ b/internal/genny/actions/actions.go @@ -68,6 +68,7 @@ func updateApp(pres *presenter) genny.RunFn { if err != nil { return err } + var lines []string body := f.String() for _, a := range pres.Actions { @@ -76,9 +77,19 @@ func updateApp(pres *presenter) genny.RunFn { lines = append(lines, e) } } + f, err = gogen.AddInsideBlock(f, "appOnce.Do(func() {", strings.Join(lines, "\n\t\t")) if err != nil { - return err + if strings.Contains(err.Error(), "could not find desired block") { + f, err = gogen.AddInsideBlock(f, "if app == nil {", strings.Join(lines, "\n\t\t")) + if err != nil { + return err + } else { + r.Logger.Warnf("This app was built with CLI v0.18.8 or older. See https://gobuffalo.io/documentation/known-issues/#cli-v0.18.8") + } + } else { + return err + } } return r.File(f) } diff --git a/internal/genny/resource/actions.go b/internal/genny/resource/actions.go index bdeb7715..6d7960b3 100644 --- a/internal/genny/resource/actions.go +++ b/internal/genny/resource/actions.go @@ -2,6 +2,7 @@ package resource import ( "fmt" + "strings" "github.com/gobuffalo/flect/name" "github.com/gobuffalo/genny/v2" @@ -14,11 +15,22 @@ func addResource(pres presenter) genny.RunFn { if err != nil { return err } + stmt := fmt.Sprintf("app.Resource(\"/%s\", %sResource{})", pres.Name.URL(), pres.Name.Resource()) f, err = gogen.AddInsideBlock(f, "appOnce.Do(func() {", stmt) if err != nil { - return err + if strings.Contains(err.Error(), "could not find desired block") { + f, err = gogen.AddInsideBlock(f, "if app == nil {", stmt) + if err != nil { + return err + } else { + r.Logger.Warnf("This app was built with CLI v0.18.8 or older. See https://gobuffalo.io/documentation/known-issues/#cli-v0.18.8") + } + } else { + return err + } } + return r.File(f) } }