Generators


    Built-in Generators

    $ buffalo g --help
    
    A collection of generators to make life easier
    
    Usage:
      buffalo generate [command]
    
    Aliases:
      generate, g
    
    
    Available Commands:
      action      Generates new action(s)
      goth        Generates a actions/goth.go file configured to the specified providers.
      resource    Generates a new actions/resource file
      webpack     Generates a webpack asset pipeline.
    
    Use "buffalo generate [command] --help" for more information about a command.
    

    See also /docs/db#generators for a list of database related generators.

    Actions

    $ buffalo g action --help
    
    Generates new action(s)
    
    Usage:
      buffalo generate action [name] [actionName...] [flags]
    
    Aliases:
      action, a, actions
    
    
    $ buffalo g a users show index create
    
    --> templates/users/show.html
    --> templates/users/index.html
    --> templates/users/create.html
    --> actions/users.go
    --> actions/users_test.go
    --> goimports -w .
    

    Resources

    $ buffalo g resource --help
    
    Generates a new actions/resource file
    
    Usage:
      buffalo generate resource [name] [flags]
    
    Aliases:
      resource, r
    
    $ buffalo g resource users
    
    --> actions/users.go
    --> actions/users_test.go
    --> goimports -w .
    

    // actions/users.go
    package actions
    
    import "github.com/gobuffalo/buffalo"
    
    type UsersResource struct {
    	buffalo.Resource
    }
    
    func init() {
    	App().Resource("/users", &UsersResource{&buffalo.BaseResource{}})
    }
    
    // List default implementation.
    func (v *UsersResource) List(c buffalo.Context) error {
    	return c.Render(200, r.String("Users#List"))
    }
    
    // Show default implementation.
    func (v *UsersResource) Show(c buffalo.Context) error {
    	return c.Render(200, r.String("Users#Show"))
    }
    
    // New default implementation.
    func (v *UsersResource) New(c buffalo.Context) error {
    	return c.Render(200, r.String("Users#New"))
    }
    
    // Create default implementation.
    func (v *UsersResource) Create(c buffalo.Context) error {
    	return c.Render(200, r.String("Users#Create"))
    }
    
    // Edit default implementation.
    func (v *UsersResource) Edit(c buffalo.Context) error {
    	return c.Render(200, r.String("Users#Edit"))
    }
    
    // Update default implementation.
    func (v *UsersResource) Update(c buffalo.Context) error {
    	return c.Render(200, r.String("Users#Update"))
    }
    
    // Destroy default implementation.
    func (v *UsersResource) Destroy(c buffalo.Context) error {
    	return c.Render(200, r.String("Users#Destroy"))
    }
    

    // actions/users_test.go
    package actions_test
    
    import (
    	"testing"
    
    	"github.com/stretchr/testify/require"
    )
    
    func Test_UsersResource_List(t *testing.T) {
    	r := require.New(t)
    	r.Fail("Not Implemented!")
    }
    
    func Test_UsersResource_Show(t *testing.T) {
    	r := require.New(t)
    	r.Fail("Not Implemented!")
    }
    
    func Test_UsersResource_New(t *testing.T) {
    	r := require.New(t)
    	r.Fail("Not Implemented!")
    }
    
    func Test_UsersResource_Create(t *testing.T) {
    	r := require.New(t)
    	r.Fail("Not Implemented!")
    }
    
    func Test_UsersResource_Edit(t *testing.T) {
    	r := require.New(t)
    	r.Fail("Not Implemented!")
    }
    
    func Test_UsersResource_Update(t *testing.T) {
    	r := require.New(t)
    	r.Fail("Not Implemented!")
    }
    
    func Test_UsersResource_Destroy(t *testing.T) {
    	r := require.New(t)
    	r.Fail("Not Implemented!")
    }
    

    Goth

    Goth provides a simple, clean, and idiomatic way to write authentication packages for Go web applications.

    $ buffalo g goth twitter facebook linkedin github
    
    --> actions/goth.go
    --> goimports -w .
    
    // actions/goth.go
    package actions
    
    import (
    	"fmt"
    	"os"
    
    	"github.com/gobuffalo/buffalo"
    	"github.com/markbates/goth"
    	"github.com/markbates/goth/gothic"
    	"github.com/markbates/goth/providers/facebook"
    	"github.com/markbates/goth/providers/github"
    	"github.com/markbates/goth/providers/linkedin"
    	"github.com/markbates/goth/providers/twitter"
    )
    
    func init() {
    	gothic.Store = App().SessionStore
    
    	goth.UseProviders(
    		twitter.New(os.Getenv("TWITTER_KEY"), os.Getenv("TWITTER_SECRET"), fmt.Sprintf("%s%s", App().Host, "/auth/twitter/callback")),
    		facebook.New(os.Getenv("FACEBOOK_KEY"), os.Getenv("FACEBOOK_SECRET"), fmt.Sprintf("%s%s", App().Host, "/auth/facebook/callback")),
    		linkedin.New(os.Getenv("LINKEDIN_KEY"), os.Getenv("LINKEDIN_SECRET"), fmt.Sprintf("%s%s", App().Host, "/auth/linkedin/callback")),
    		github.New(os.Getenv("GITHUB_KEY"), os.Getenv("GITHUB_SECRET"), fmt.Sprintf("%s%s", App().Host, "/auth/github/callback")),
    	)
    
    	app := App().Group("/auth")
    	app.GET("/{provider}", buffalo.WrapHandlerFunc(gothic.BeginAuthHandler))
    	app.GET("/{provider}/callback", AuthCallback)
    }
    
    func AuthCallback(c buffalo.Context) error {
    	user, err := gothic.CompleteUserAuth(c.Response(), c.Request())
    	if err != nil {
    		return c.Error(401, err)
    	}
    	// Do something with the user, maybe register them/sign them in
    	return c.Render(200, r.JSON(user))
    }