Skip to content
Permalink
Browse files

Adding screencast for using plush templates inside buffalo apps (#169)

* adding episode25 skeleton

* adding a new route + template

* adding outline & show notes

* removing layout stuff

* adding CSRF links:

* fixing parenthesis

* Adding gotime stuff (#173)

* adding episode25 skeleton

* adding a new route + template

* adding outline & show notes

* removing layout stuff

* adding CSRF links:

* fixing parenthesis

* upgrading buffalo

* rm the public folder

* upgrades

* upgrades

* adding webpage for episode 25
  • Loading branch information...
arschles committed Jul 2, 2019
1 parent 5bc6a42 commit f3d9e0788afced74c640fe29489cfb81257268f6
Showing with 8,315 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +3 −0 episode25/.babelrc
  3. +21 −0 episode25/.buffalo.dev.yml
  4. +20 −0 episode25/.codeclimate.yml
  5. +3 −0 episode25/.dockerignore
  6. +9 −0 episode25/.env
  7. +34 −0 episode25/Dockerfile
  8. +29 −0 episode25/README.md
  9. +24 −0 episode25/actions/actions_test.go
  10. +93 −0 episode25/actions/app.go
  11. +24 −0 episode25/actions/home.go
  12. +7 −0 episode25/actions/home_test.go
  13. +27 −0 episode25/actions/render.go
  14. +118 −0 episode25/assets/css/_buffalo.scss
  15. +4 −0 episode25/assets/css/application.scss
  16. BIN episode25/assets/images/favicon.ico
  17. +721 −0 episode25/assets/images/logo.svg
  18. +5 −0 episode25/assets/js/application.js
  19. +13 −0 episode25/config/buffalo-app.toml
  20. +23 −0 episode25/database.yml
  21. +11 −0 episode25/docker-compose.yml
  22. +32 −0 episode25/fixtures/sample.toml
  23. +23 −0 episode25/go.mod
  24. +1,015 −0 episode25/go.sum
  25. +15 −0 episode25/grifts/db.go
  26. +11 −0 episode25/grifts/init.go
  27. +3 −0 episode25/inflections.json
  28. +3 −0 episode25/locales/all.en-us.yaml
  29. +38 −0 episode25/main.go
  30. +22 −0 episode25/models/models.go
  31. +24 −0 episode25/models/models_test.go
  32. +38 −0 episode25/package.json
  33. BIN episode25/public/assets/674f50d287a8c48dc19ba404d20fe713.eot
  34. BIN episode25/public/assets/images/favicon.ico
  35. +721 −0 episode25/public/assets/images/logo.svg
  36. +14 −0 episode25/templates/_flash.html
  37. +21 −0 episode25/templates/application.html
  38. +25 −0 episode25/templates/index.html
  39. +112 −0 episode25/webpack.config.js
  40. +4,978 −0 episode25/yarn.lock
  41. +26 −0 www/content/screencast/episode_25_buffalo_templating_with_plush.md
@@ -41,3 +41,8 @@ episode22/public
episode22/public/*
episode24/tmp
episode24/tmp/episode24-build

episode25/tmp
episode25/tmp/episode25-build
episode25/node_modules
episode25/public/assets
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
@@ -0,0 +1,21 @@
app_root: .
ignored_folders:
- vendor
- log
- logs
- assets
- public
- grifts
- tmp
- bin
- node_modules
- .sass-cache
included_extensions:
- .go
- .env
build_path: tmp
build_delay: 200ns
binary_name: episode25-build
command_flags: []
enable_colors: true
log_name: buffalo
@@ -0,0 +1,20 @@
engines:
fixme:
enabled: true
gofmt:
enabled: true
golint:
enabled: true
govet:
enabled: true
exclude_paths:
- grifts/**/*
- "**/*_test.go"
- "*_test.go"
- "**_test.go"
- logs/*
- public/*
- templates/*
ratings:
paths:
- "**.go"
@@ -0,0 +1,3 @@
node_modules/
*.log
bin/
@@ -0,0 +1,9 @@
# This .env file was generated by buffalo, add here the env variables you need
# buffalo to load into the ENV on application startup so your application works correctly.
# To add variables use KEY=VALUE format, you can later retrieve this in your application
# by using os.Getenv("KEY").
#
# Example:
# DATABASE_PASSWORD=XXXXXXXXX
# SESSION_SECRET=XXXXXXXXX
# SMTP_SERVER=XXXXXXXXX
@@ -0,0 +1,34 @@
# This is a multi-stage Dockerfile and requires >= Docker 17.05
# https://docs.docker.com/engine/userguide/eng-image/multistage-build/
FROM gobuffalo/buffalo:v0.14.6 as builder

RUN mkdir -p $GOPATH/src/episode25
WORKDIR $GOPATH/src/episode25

# this will cache the npm install step, unless package.json changes
ADD package.json .
ADD yarn.lock .
RUN yarn install --no-progress
ADD . .
RUN go get $(go list ./... | grep -v /vendor/)
RUN buffalo build --static -o /bin/app

FROM alpine
RUN apk add --no-cache bash
RUN apk add --no-cache ca-certificates

WORKDIR /bin/

COPY --from=builder /bin/app .

# Uncomment to run the binary in "production" mode:
# ENV GO_ENV=production

# Bind the app to 0.0.0.0 so it can be seen from outside the container
ENV ADDR=0.0.0.0

EXPOSE 3000

# Uncomment to run the migrations before running the binary:
# CMD /bin/app migrate; /bin/app
CMD exec /bin/app
@@ -0,0 +1,29 @@
# The [Buffalo](https://gobuffalo.io) Series #4 - Templates

Go in 5 Minutes, episode 25.

We've been looking at the [Buffalo framework](https://gobuffalo.io) lately, and
we're gonna keep on going today with [templating](https://gobuffalo.io/en/docs/templating).

If you're building a webapp, templating is crucial for you to be able to serve dynamic content to your users. For example, if you need to fetch the user's name and information from your database, you'll need to somehow put that data into HTML that you can serve to their browser.

Templates are how you do it, and Buffalo has a great, easy to understand template engine to help you out!

Here's what we'll learn today:

- Introduction to templating
- The [plush](https://github.com/gobuffalo/plush) template engine
- How to use plush from your Buffalo app

And like always I'll be showing live code.

Check out the screencast for more!

# Show Notes

- Buffalo templates: https://gobuffalo.io/en/docs/templating
- Go standard library [`html/template`](https://godoc.org/html/template) and [`text/template`](https://godoc.org/text/template)
- [CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery)
- [Prevention cheat sheet](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet)
- See [here](https://stackoverflow.com/questions/941594/understanding-the-rails-authenticity-token) for a great explanation on how `authenticity_token` fits in
@@ -0,0 +1,24 @@
package actions

import (
"testing"

"github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/suite"
)

type ActionSuite struct {
*suite.Action
}

func Test_ActionSuite(t *testing.T) {
action, err := suite.NewActionWithFixtures(App(), packr.New("../fixtures", "../fixtures"))
if err != nil {
t.Fatal(err)
}

as := &ActionSuite{
Action: action,
}
suite.Run(t, as)
}
@@ -0,0 +1,93 @@
package actions

import (
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/envy"
forcessl "github.com/gobuffalo/mw-forcessl"
paramlogger "github.com/gobuffalo/mw-paramlogger"
"github.com/gobuffalo/packr/v2"
"github.com/unrolled/secure"

// "episode25/models"

// "github.com/gobuffalo/buffalo-pop/pop/popmw"
csrf "github.com/gobuffalo/mw-csrf"
i18n "github.com/gobuffalo/mw-i18n"
)

// 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 T *i18n.Translator

// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
//
// Routing, middleware, groups, etc... are declared TOP -> DOWN.
// This means if you add a middleware to `app` *after* declaring a
// group, that group will NOT have that new middleware. The same
// is true of resource declarations as well.
//
// It also means that routes are checked in the order they are declared.
// `ServeFiles` is a CATCH-ALL route, so it should always be
// 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 {
app = buffalo.New(buffalo.Options{
Env: ENV,
SessionName: "_episode25_session",
})

// Automatically redirect to SSL
app.Use(forceSSL())

// Log request parameters (filters apply).
app.Use(paramlogger.ParameterLogger)

// Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
// Remove to disable this.
app.Use(csrf.New)

// Wraps each request in a transaction.
// c.Value("tx").(*pop.Connection)
// Remove to disable this.
// app.Use(popmw.Transaction(models.DB))

// Setup and use translations:
app.Use(translations())

app.GET("/", HomeHandler)
app.POST("/", HomePostHandler)

app.ServeFiles("/", assetsBox) // serve files from the public directory
}

return app
}

// translations will load locale files, set up the translator `actions.T`,
// and will return a middleware to use to load the correct locale for each
// request.
// for more information: https://gobuffalo.io/en/docs/localization
func translations() buffalo.MiddlewareFunc {
var err error
if T, err = i18n.New(packr.New("../locales", "../locales"), "en-US"); err != nil {
app.Stop(err)
}
return T.Middleware()
}

// forceSSL will return a middleware that will redirect an incoming request
// if it is not HTTPS. "http://example.com" => "https://example.com".
// This middleware does **not** enable SSL. for your application. To do that
// we recommend using a proxy: https://gobuffalo.io/en/docs/proxy
// for more information: https://github.com/unrolled/secure/
func forceSSL() buffalo.MiddlewareFunc {
return forcessl.Middleware(secure.Options{
SSLRedirect: ENV == "production",
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
})
}
@@ -0,0 +1,24 @@
package actions

import (
"net/http"

"github.com/gobuffalo/buffalo"
)

// HomeHandler is a default handler to serve up
// a home page.
func HomeHandler(c buffalo.Context) error {
return c.Render(200, r.HTML("index.html"))
}

// HomePostHandler is a handler to serve the POST / page
func HomePostHandler(c buffalo.Context) error {
var name string
if c.Param("name") != "" {
name = c.Param("name")
c.Set("name", name)
return c.Render(200, r.HTML("index.html"))
}
return c.Redirect(http.StatusFound, "/")
}
@@ -0,0 +1,7 @@
package actions

func (as *ActionSuite) Test_HomeHandler() {
res := as.HTML("/").Get()
as.Equal(200, res.Code)
as.Contains(res.Body.String(), "Welcome to Buffalo")
}
@@ -0,0 +1,27 @@
package actions

import (
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/packr/v2"
)

var r *render.Engine
var assetsBox = packr.NewBox("../public")

func init() {
r = render.New(render.Options{
// HTML layout to be used for all HTML requests:
HTMLLayout: "application.html",

// Box containing all of the templates:
TemplatesBox: packr.New("../templates", "../templates"),
AssetsBox: assetsBox,

// Add template helpers here:
Helpers: render.Helpers{
// uncomment for non-Bootstrap form helpers:
// "form": plush.FormHelper,
// "form_for": plush.FormForHelper,
},
})
}

0 comments on commit f3d9e07

Please sign in to comment.
You can’t perform that action at this time.