Buffalo Documentation

repository·main·Indexed 27 days ago

https://github.com/gobuffalo/buffalo

Buffalo 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.

Tokens
8.5K
Snippets
24
Records
90
Agent score
93%

What's inside Buffalo

  1. Overview of Buffalo Web Development Ecosystem

    main
    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.
  2. Get started with Buffalo

    main

    You can begin using Buffalo by following these steps:

    1. Installation: Follow the official installation guide to set up the Buffalo CLI.
    2. Create a new project: Use the Buffalo CLI to scaffold a new web application with pre-configured routing, templating, and database support.
    3. Tutorials: Refer to the official tutorials to learn how to build and extend your application.
  3. Prerequisites for using Buffalo

    main

    To use Buffalo effectively, ensure your environment meets the following requirements:

    • Go Modules: Buffalo requires Go modules. Using GOPATH mode 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.
  4. Create and send a message with multiple bodies

    main

    Use mail.NewMessage() to construct an email. You can define the From, Subject, and To fields. To support multiple content types (e.g., HTML and Plain Text), use the AddBodies method, 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)
  5. Configure SSL or TLS for SMTPSender

    main

    If you are using a provider like Gmail that requires specific security settings, you can access the Dialer property on the SMTPSender to configure SSL or TLSConfig.

    sender, err := mail.NewSMTPSender(host, port, user, password)
    
    // Enable SSL
    sender.Dialer.SSL = true
    
    // Or configure custom TLS
    sender.Dialer.TLSConfig = &tls.Config{...}
    
    smtp = sender
  6. Initialize an SMTP sender

    main

    To send emails via SMTP, use mail.NewSMTPSender. You can pull configuration from environment variables like SMTP_HOST, SMTP_PORT, SMTP_USER, and SMTP_PASSWORD. The returned object implements the mail.Sender interface.

    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)
    }
  7. Buffalo Core Dependencies and Technologies

    main

    Buffalo integrates several high-quality Go packages to provide its core functionality:

    • Templating: Uses Plush instead of the standard html/template for 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.
  8. Ensure Resource types are declared for Middleware.Skip to work

    main

    When using g.Middleware.Skip(middleware, handler) to skip specific middleware for a resource handler, you must declare your resource handler as a variable of type buffalo.Resource first. If you use short-variable declaration (:=), the Skip function 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)
  9. Manage session data with the Session type

    main
    The Session type 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.
  10. Map HTTP methods to handlers

    main

    Use the following methods on an *App instance to map specific HTTP verbs and paths to a Handler function:

    • GET(path string, h Handler) *RouteInfo
    • POST(path string, h Handler) *RouteInfo
    • PUT(path string, h Handler) *RouteInfo
    • DELETE(path string, h Handler) *RouteInfo
    • PATCH(path string, h Handler) *RouteInfo
    • HEAD(path string, h Handler) *RouteInfo
    • OPTIONS(path string, h Handler) *RouteInfo
    • ANY(path string, h Handler): Maps all standard HTTP methods to the specified handler.

    Each method returns a *RouteInfo which can be used for further route configuration.