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

Update new project template to use extracted middlewares #1213

Merged
merged 9 commits into from
Aug 8, 2018
2 changes: 1 addition & 1 deletion buffalo/cmd/filetests/apiapp.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[{
"path": "actions/app.go",
"contains": [
"app.Use(middleware.SetContentType(\"application/json\"))"
"app.Use(contenttype.Set(\"application/json\"))"
],
"!contains": [
"app.ServeFiles"
Expand Down
38 changes: 30 additions & 8 deletions buffalo/cmd/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,50 @@ func Test_NewCmd_Nominal(t *testing.T) {
r := require.New(t)
c := RootCmd

tdir, err := ioutil.TempDir("", "gopath")
gp, err := envy.MustGet("GOPATH")
r.NoError(err)
cpath := path.Join(gp, "src", "github.com", "gobuffalo")
tdir, err := ioutil.TempDir(cpath, "testapp")
r.NoError(err)
defer os.RemoveAll(tdir)
gp, err := envy.MustGet("GOPATH")

pwd, err := os.Getwd()
r.NoError(err)
defer envy.MustSet("GOPATH", gp)
err = envy.MustSet("GOPATH", tdir)
os.Chdir(tdir)
defer os.Chdir(pwd)

c.SetArgs([]string{
"new",
"hello_world",
"--skip-pop",
"--skip-webpack",
"--vcs=none",
})
err = c.Execute()
r.NoError(err)
}

func Test_NewCmd_API(t *testing.T) {
r := require.New(t)
c := RootCmd

cpath := path.Join(tdir, "src", "github.com", "gobuffalo")
err = os.MkdirAll(cpath, 0700)
gp, err := envy.MustGet("GOPATH")
r.NoError(err)
cpath := path.Join(gp, "src", "github.com", "gobuffalo")
tdir, err := ioutil.TempDir(cpath, "testapp")
r.NoError(err)
defer os.RemoveAll(tdir)

pwd, err := os.Getwd()
r.NoError(err)
os.Chdir(cpath)
os.Chdir(tdir)
defer os.Chdir(pwd)

c.SetArgs([]string{
"new",
"hello_world",
"--skip-pop",
"--skip-webpack",
"--api",
"--vcs=none",
})
err = c.Execute()
Expand Down
18 changes: 10 additions & 8 deletions generators/newapp/templates/actions/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package actions
import (
"github.com/gobuffalo/envy"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/middleware"
"github.com/gobuffalo/buffalo/middleware/ssl"
"github.com/gobuffalo/mw-forcessl"
"github.com/gobuffalo/mw-paramlogger"
"github.com/unrolled/secure"

{{ if .opts.WithPop }}
"{{.opts.ModelsPkg}}"
"github.com/gobuffalo/mw-poptx"
{{ end -}}

{{ if .opts.AsWeb -}}
"github.com/gobuffalo/buffalo/middleware/csrf"
"github.com/gobuffalo/buffalo/middleware/i18n"
"github.com/gobuffalo/mw-csrf"
"github.com/gobuffalo/mw-i18n"
"github.com/gobuffalo/packr"
{{ end -}}

{{ if .opts.AsAPI -}}
"github.com/rs/cors"
"github.com/gobuffalo/x/sessions"
"github.com/gobuffalo/mw-contenttype"
{{ end -}}
)

Expand Down Expand Up @@ -51,11 +53,11 @@ func App() *buffalo.App {

{{ if .opts.AsAPI -}}
// Set the request content type to JSON
app.Use(middleware.SetContentType("application/json"))
app.Use(contenttype.Set("application/json"))
{{ end }}

if ENV == "development" {
app.Use(middleware.ParameterLogger)
app.Use(paramlogger.ParameterLogger)
}

{{ if .opts.AsWeb -}}
Expand All @@ -68,7 +70,7 @@ func App() *buffalo.App {
// Wraps each request in a transaction.
// c.Value("tx").(*pop.Connection)
// Remove to disable this.
app.Use(middleware.PopTransaction(models.DB))
app.Use(poptx.PopTransaction(models.DB))
{{ end }}

{{ if .opts.AsWeb -}}
Expand Down Expand Up @@ -106,7 +108,7 @@ func translations() buffalo.MiddlewareFunc {
// we recommend using a proxy: https://gobuffalo.io/en/docs/proxy
// for more information: https://github.com/unrolled/secure/
func forceSSL() buffalo.MiddlewareFunc {
return ssl.ForceSSL(secure.Options{
return forcessl.Middleware(secure.Options{
SSLRedirect: ENV == "production",
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
})
Expand Down