Getting Started
Request handling
- Routing
- Action Controller
- Resources
- Context
- Request Binding
- Middleware
- Error Handling
- Sessions
- Cookies
Frontend
Database
- Getting started with Pop
- Soda CLI
- Database Configuration
- Buffalo Integration
- Models
- Generators
- Migrations
- Fizz
- Mutations
- Querying
- Raw Queries
- Callbacks
- Scoping
- Associations and Relationships
- One to one associations
- One to many associations
Guides
- API Applications
- File Uploads
- Background Job Workers
- Mailers
- Tasks
- Plugins
- Local Authentication
- Third Party Authentication
- Events
- Go Modules
- Localization
- Logging
- Template Engines
- Testing
- Videos
Deploy
API Applications
Guides
API Applications#
Applications that only serve API end-points, typically JSON, are very different from those that serve HTML, JavaScript, and CSS. In this guide, youβll learn how to build an API-only app, using Buffalo.
Creating a New API Application#
When creating a new Buffalo application using the buffalo new
command, the optional --api
flag will generate an application that is better suited to serving APIs than a stock Buffalo application.
$ buffalo new coke --api
Slimmed Project Layout#
Applications generated with the --api
flag donβt contain any front systems. This means there is no templating, stylesheets, etcβ¦
API
Default
$ buffalo new coke --api
βββ actions/
β βββ app.go
β βββ render.go
βββ cmd/
β βββ app/
β βββ main.go
βββ config/
βββ fixtures/
βββ grifts/
βββ locales/
βββ models/
βββ .buffalo.dev.yml
βββ .codeclimate.yml
βββ .docketignore
βββ .env
βββ .gitignore
βββ database.yml
βββ Dockerfile
βββ go.mod
βββ go.sum
βββ inflections.json
βββ README.md
$ buffalo new coke
βββ .yarn/
βββ actions/
β βββ app.go
β βββ render.go
βββ assets/
βββ cmd/
β βββ app/
β βββ main.go
βββ config/
βββ fixtures/
βββ grifts/
βββ locales/
βββ models/
βββ public/
βββ templates/
βββ .babelrc
βββ .buffalo.dev.yml
βββ .codeclimate.yml
βββ .docketignore
βββ .env
βββ .gitignore
βββ .pnp.cjs
βββ .pnp.loader.mjs
βββ .yarnrc.yml
βββ database.yml
βββ Dockerfile
βββ go.mod
βββ go.sum
βββ inflections.json
βββ package.json
βββ postcss.config.js
βββ README.md
βββ webpack.config.js
βββ yarn.lock
Tuned actions/app.go actions/render.go
Files#
API applications have actions/app.go
and actions/render.go
files that are a good starting point for API applications.
API
Default
$ buffalo new coke --api
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{
Env: ENV,
SessionStore: sessions.Null{},
PreWares: []buffalo.PreWare{
cors.Default().Handler,
},
SessionName: "_coke_session",
})
app.Use(forceSSL())
app.Use(paramlogger.ParameterLogger)
app.Use(contenttype.Set("application/json"))
app.Use(popmw.Transaction(models.DB))
app.GET("/", HomeHandler)
}
return app
}
func init() {
r = render.New(render.Options{
DefaultContentType: "application/json",
})
}
$ buffalo new coke
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{
Env: ENV,
SessionName: "_coke_session",
})
app.Use(forceSSL())
app.Use(paramlogger.ParameterLogger)
app.Use(csrf.New)
app.Use(popmw.Transaction(models.DB))
app.Use(translations())
app.GET("/", HomeHandler)
app.ServeFiles("/", http.FS(public.FS())) // serve files from the public directory
}
return app
}
func init() {
r = render.New(render.Options{
HTMLLayout: "application.plush.html",
TemplatesFS: templates.FS(),
AssetsFS: public.FS(),
Helpers: render.Helpers{},
})
}