Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

applied sync.Once to make 'app' to be the only root App #229

Merged
merged 2 commits into from
Oct 1, 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
11 changes: 8 additions & 3 deletions internal/genny/actions/_fixtures/inputs/clean/actions/app.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 13 additions & 2 deletions internal/genny/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -76,9 +77,19 @@ 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
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)
}
Expand Down
11 changes: 7 additions & 4 deletions internal/genny/newapp/api/templates/actions/app.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actions

import (
"sync"

{{ if .opts.App.WithPop -}}
"{{ .opts.App.ModelsPkg }}"
{{ end -}}
Expand All @@ -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
Expand All @@ -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{},
Expand All @@ -69,7 +72,7 @@ func App() *buffalo.App {
{{ end -}}

app.GET("/", HomeHandler)
}
})

return app
}
Expand Down
12 changes: 9 additions & 3 deletions internal/genny/newapp/core/templates/actions/app.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actions

import (
"sync"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/envy"
forcessl "github.com/gobuffalo/mw-forcessl"
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -40,7 +46,7 @@ func App() *buffalo.App {
app.Use(paramlogger.ParameterLogger)

app.GET("/", HomeHandler)
}
})

return app
}
Expand Down
10 changes: 6 additions & 4 deletions internal/genny/newapp/web/templates/actions/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"net/http"
"sync"

"{{ .opts.App.PackagePkg }}/locales"
{{ if .opts.App.WithPop -}}
Expand All @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions internal/genny/resource/_fixtures/default/actions/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"net/http"
"sync"

"github.com/markbates/coke/locales"
"github.com/markbates/coke/public"
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions internal/genny/resource/_fixtures/nested/actions/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"net/http"
"sync"

"github.com/markbates/coke/locales"
"github.com/markbates/coke/public"
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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
}
Expand Down
16 changes: 14 additions & 2 deletions internal/genny/resource/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resource

import (
"fmt"
"strings"

"github.com/gobuffalo/flect/name"
"github.com/gobuffalo/genny/v2"
Expand All @@ -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, "if app == nil {", stmt)
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)
}
}
Expand Down