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

Commit

Permalink
applied sync.Once to make 'app' to be the only root App
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Oct 1, 2022
1 parent 52e99d8 commit ed7590d
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 30 deletions.
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
}
2 changes: 1 addition & 1 deletion internal/genny/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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
2 changes: 1 addition & 1 deletion internal/genny/resource/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ed7590d

Please sign in to comment.