Buffalo Documentation
repository·main·Indexed 27 days ago
https://github.com/gobuffalo/buffaloBuffalo is a comprehensive Go web development ecosystem providing a full-stack project structure with routing, ORM integration, and front-end asset management. It integrates tools like Plush for templating, Gorilla Mux for routing, and Pop/Soda for database interactions. The framework features a central App struct, a robust Context interface for request lifecycle management, and built-in support for sessions, flash messages, cookies, and SMTP mailers.
What's inside Buffalo
- Buffalo is a holistic Go web development ecosystem designed to accelerate project creation. It provides a complete project structure including front-end assets (JavaScript, SCSS) and back-end components (database integration, routing) that are pre-configured and ready to run. It is intended to be used as a development environment rather than just a framework.
Get started with Buffalo
mainYou can begin using Buffalo by following these steps:
- Installation: Follow the official installation guide to set up the Buffalo CLI.
- Create a new project: Use the Buffalo CLI to scaffold a new web application with pre-configured routing, templating, and database support.
- Tutorials: Refer to the official tutorials to learn how to build and extend your application.
Prerequisites for using Buffalo
mainTo use Buffalo effectively, ensure your environment meets the following requirements:
- Go Modules: Buffalo requires Go modules. Using
GOPATHmode will likely break most Buffalo ecosystem functionality. - Go Version: Buffalo actively supports the last two major versions of Go (currently Go 1.23 and 1.24). While it may work on older versions, upgrading to one of the latest two versions is highly recommended for a better experience.
- Go Modules: Buffalo requires Go modules. Using
Create and send a message with multiple bodies
mainUse
mail.NewMessage()to construct an email. You can define theFrom,Subject, andTofields. To support multiple content types (e.g., HTML and Plain Text), use theAddBodiesmethod, passing in data for template rendering and the rendered content from your Buffalo render engine.// Creates a new message m := mail.NewMessage() m.From = "sender@myapp.com" m.Subject = "New Contact" m.To = []string{"contact@myapp.com"} // Data for templates data := map[string]interface{}{ "contact": c, } // Add multiple bodies (HTML and Plain Text) err := m.AddBodies(data, r.HTML("mail/contact.html"), r.Plain("mail/contact.txt")) if err != nil { return err } // Send via the initialized sender err = smtp.Send(m)Configure SSL or TLS for SMTPSender
mainIf you are using a provider like Gmail that requires specific security settings, you can access the
Dialerproperty on theSMTPSenderto configureSSLorTLSConfig.sender, err := mail.NewSMTPSender(host, port, user, password) // Enable SSL sender.Dialer.SSL = true // Or configure custom TLS sender.Dialer.TLSConfig = &tls.Config{...} smtp = senderInitialize an SMTP sender
mainTo send emails via SMTP, use
mail.NewSMTPSender. You can pull configuration from environment variables likeSMTP_HOST,SMTP_PORT,SMTP_USER, andSMTP_PASSWORD. The returned object implements themail.Senderinterface.var smtp mail.Sender var err error port := env.Get("SMTP_PORT", "1025") host := env.Get("SMTP_HOST", "localhost") user := env.Get("SMTP_USER", "") password := env.Get("SMTP_PASSWORD", "") smtp, err = mail.NewSMTPSender(host, port, user, password) if err != nil { log.Fatal(err) }Buffalo Core Dependencies and Technologies
mainBuffalo integrates several high-quality Go packages to provide its core functionality:
- Templating: Uses Plush instead of the standard
html/templatefor increased flexibility. - Routing: Powered by Gorilla Mux for stable and powerful request routing.
- Models/ORM: Uses Pop and its CLI tool Soda for database interactions and migrations.
- Web Toolkit: Leverages the Gorilla toolkit for advanced handling of sessions, cookies, and WebSockets.
- Templating: Uses Plush instead of the standard
Ensure Resource types are declared for Middleware.Skip to work
mainWhen using
g.Middleware.Skip(middleware, handler)to skip specific middleware for a resource handler, you must declare your resource handler as a variable of typebuffalo.Resourcefirst. If you use short-variable declaration (:=), theSkipfunction may fail to recognize and match the handler.Correct Pattern:
var cr Resource cr = &myResource{} g = a.Resource("/path", cr) g.Middleware.Skip(SomeMiddleware, cr.Show)Incorrect Pattern:
cr := &myResource{} // Skip might not work correctly here g.Middleware.Skip(SomeMiddleware, cr.Show)// Works: var cr Resource cr = &carsResource{&buffaloBaseResource{}} g = a.Resource("/cars", cr) g.Use(SomeMiddleware) g.Middleware.Skip(SomeMiddleware, cr.Show) // Doesn't Work: cr := &carsResource{&buffaloBaseResource{}} g = a.Resource("/cars", cr) g.Use(SomeMiddleware) g.Middleware.Skip(SomeMiddleware, cr.Show)Generate a mailer with Buffalo CLI
mainUse the Buffalo CLI to scaffold a new mailer component in your application.
buffalo generate mailer welcome_emailManage session data with the Session type
mainTheSessiontype wraps the Gorilla sessions API to provide a cleaner interface for managing user session data. You can use it to store, retrieve, and delete values associated with a user's session.Map HTTP methods to handlers
mainUse the following methods on an
*Appinstance to map specific HTTP verbs and paths to aHandlerfunction:GET(path string, h Handler) *RouteInfoPOST(path string, h Handler) *RouteInfoPUT(path string, h Handler) *RouteInfoDELETE(path string, h Handler) *RouteInfoPATCH(path string, h Handler) *RouteInfoHEAD(path string, h Handler) *RouteInfoOPTIONS(path string, h Handler) *RouteInfoANY(path string, h Handler): Maps all standard HTTP methods to the specified handler.
Each method returns a
*RouteInfowhich can be used for further route configuration.Bind request bodies with Bind
mainTheBind(value any) errormethod populates a Go struct or map from the request body. The binding logic (JSON, XML, etc.) is determined by theContent-Typeheader of the request.