Buffalo 入门

2025-06-07

Buffalo 入门

简介 - 现代世界中的快速软件开发

(请随意跳到教程

这篇文章是有关现代世界快速软件开发的系列文章中的第一篇。

在 Otomato 软件公司,我们一直在努力实现快速开发人员入职。但真正促使我们深入研究这个问题的,是几个月前我与 Elad Meidar 的一次谈话。

Elad 跟我说过,在如今的世界里,选择技术栈已经变得非常不容易。假设你已经选好了工具——只有最有经验的开发人员才能真正知道如何正确设置 CI、测试、部署、检测、监控、安全等等。即使是最初的运行,也需要大量的运维知识。

我们目前的目标是将我们积累的知识转化为服务。与此同时,我们也在探索现有的工具。

进入布法罗

Buffalo 是一个使用 Go 语言进行快速 Web 开发的框架(或者说工具)。云原生 DevOps 开发者( Otomato团队也从事这一工作)对 Golang 情有独钟,因此我选择以 Buffalo 开启这个系列。

Buffalo 官方文档的入门部分很棒,但我浏览了一下,发现缺少一些我打算公开的操作细节。看来操作知识又被埋没了!

安装

当然,你需要安装 Go。
在 Mac 上:



brew install golang


Enter fullscreen mode Exit fullscreen mode

在 Ubuntu/Debian 上:



sudo apt update && sudo apt install golang


Enter fullscreen mode Exit fullscreen mode

注意:在较旧的系统(例如 Ubuntu 20.04)上,使用 安装时默认会获得非常旧的 Go 版本(1.13) 。因此,请选择此处的apt下载并提取选项

如需更多安装选项,请点击此处

你做前端吗?

Buffalo 既可以生成纯后端 API 服务,也可以生成包含前端内容的完整 Web 应用。前端使用 JavaScript 编写,因此如果您需要 JavaScript,您还需要 Node 以及 yarn 或 npm(默认)。
在 Mac 上:



brew install nodejs


Enter fullscreen mode Exit fullscreen mode

在 Ubuntu/Debian 上:



sudo apt update
sudo apt install nodejs npm


Enter fullscreen mode Exit fullscreen mode

您想要容器吗?

Buffalo 在生成项目时会做出一些合理的假设。其中之一就是您需要将应用封装到容器中。当然,您可以选择不这样做,但为什么要这么做呢?如果您打算顺其自然地享受容器化带来的好处,那么您可能已经安装了 Docker。如果没有,请立即安装,因为我们后续的教程会用到它。

最后——引入 Buffalo

在Mac上:



brew install gobuffalo/tap/buffalo


Enter fullscreen mode Exit fullscreen mode

在 Linux 上;



wget https://github.com/gobuffalo/cli/releases/download/v0.18.8/buffalo_0.18.8_Linux_x86_64.tar.gz
tar -xvzf buffalo_0.18.8_Linux_x86_64.tar.gz
sudo mv buffalo /usr/local/bin/buffalo


Enter fullscreen mode Exit fullscreen mode

创建项目

Buffalo 具有项目脚手架功能,允许我们生成一个完整的新应用程序:

  • 本地 git 仓库
  • 后端 API
  • 数据库集成
  • 前端
  • Dockerfile
  • CI 管道。

让我们创建一个名为 的 Web 应用testr。它将用于管理新老学员的测试任务。(我之前提到过,Otomato 也提供技术培训吗?)

创建新项目的命令是buffalo new。Buffalo
默认使用的数据库后端是 PostgreSQL。
我们将使用 Github 进行 SCM,因此选择 Github Actions 作为我们的 CI 提供商。



buffalo new testr --ci-provider github


Enter fullscreen mode Exit fullscreen mode

在 Buffalo 向我们展示它带来和生成的东西(实际上相当多的东西)之后,它会说:



Initialized empty Git repository in /Users/username/git/testr/.git/
DEBU[2022-08-28T23:37:54+03:00] Exec: git add .
DEBU[2022-08-28T23:37:54+03:00] Exec: git commit -q -m Initial Commit
INFO[2022-08-28T23:37:54+03:00] Congratulations! Your application, testr, has been successfully generated!
INFO[2022-08-28T23:37:54+03:00] You can find your new application at: /Users/antweiss/git/testr
INFO[2022-08-28T23:37:54+03:00] Please read the README.md file in your new application for next steps on running your application.


Enter fullscreen mode Exit fullscreen mode

因此我们将按照它告诉我们的去做:



cd testr
git add .
git commit -q -m "Initial Commit"


Enter fullscreen mode Exit fullscreen mode

设置数据库

在真正开始开发代码之前,我们需要
启动一个数据库。当然,我们也可以使用托管数据库,但这可能会花费一些钱。因此,对于本地开发来说,在容器中运行数据库更有意义。

让我们运行 PostgreSQL(默认的 Buffalo DB 后端):



docker run --name buffalo-postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres


Enter fullscreen mode Exit fullscreen mode

注意:我们在这里使用一个非常简单的密码运行 PostgreSQL,这对于本地开发来说没问题,但不适合任何类似生产环境的应用。
我们还将它暴露在了localhost:5432PostgreSQL 上,Buffalo 应用已配置为默认查找它。

这些配置是在 buffalo 生成的文件中定义的,database.yml我们很快就会使用它。

运行 DB 容器还不够。我们还需要为我们的应用创建一个数据库。

这可以通过进入容器并运行经典的 SQL 命令来实现。但 Buffalo 的创建者推荐使用 Soda——一个小型且实用的 CLI 实用程序,可以简化数据库管理。

安装苏打水:



go install github.com/gobuffalo/pop/v6/soda@latest


Enter fullscreen mode Exit fullscreen mode

并创建一个数据库:



soda create -a


Enter fullscreen mode Exit fullscreen mode

database.yamlSoda 创建了Buffalo 为我们生成的文件中配置的所有数据库。

注意:Buffalo 默认使用其自带的 ORM 库进行pop数据库集成。Pop 提供了一个包装器soda- ,因此我们也可以soda通过 Buffalo 别名buffalo pop create -a或运行命令buffalo db create -a

Buffalo 借助pop模型生成等功能,提供了更多实用的数据库功能。我们将在后续文章中介绍。

开始开发

Buffalo 为我们提供了一个buffalo dev命令,允许通过实时重新加载运行我们的应用程序 - 即每次我们更改代码时重新启动应用程序服务器。

快跑吧!



buffalo dev


Enter fullscreen mode Exit fullscreen mode

现在我们可以在浏览器中访问http://localhost:3000并看到我们的应用程序正在运行!
应用程序正在运行

而且--我们正在直播!

我们看到的 Web UI 是testr/templates/home/index.plush.html使用Plush模板引擎生成的。这部分内容将在另一篇文章中介绍。

让我们来点 CI

作为本演练的最后一步 - 让我们将代码推送到 Github 并验证生成的 CI 管道是否有效。

生成新的 Github 仓库

我衷心推荐使用Github 的ghcli 工具
testr目录运行:



gh repo create --public <your-user-or-org>/buffalo-testr --push  --source .


Enter fullscreen mode Exit fullscreen mode

这将创建 repo 并立即将代码推送到它,进而启动中定义的工作流程.github/workflows/test.yml



name: test

on:
  push:
  pull_request:

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:9.6-alpine
        env:
          POSTGRES_DB: testr_test
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v3
        with:
          go-version: ~1.18
          cache: true
      - name: setup
        run: |
          go install github.com/gobuffalo/cli/cmd/buffalo@latest
      - name: test
        env:
          TEST_DATABASE_URL: postgres://postgres:postgres@localhost:5432/testr_test?sslmode=disable
        run: |
          buffalo test


Enter fullscreen mode Exit fullscreen mode

我们可以看到这个工作流程:

  1. 启动 PostgreSQL 服务容器
  2. 安装水牛
  3. 运行buffalo test——这反过来在容器中创建数据库并运行一些测试:


[POP] 2022/09/12 15:24:04 info - dropped database testr_test
[POP] 2022/09/12 15:24:05 info - created database testr_test
pg_dump: error: connection to server at "127.0.0.1", port 5432 failed: FATAL:  database "testr_development" does not exist
[POP] 2022/09/12 15:24:05 info - Migrations already up to date, nothing to apply
[POP] 2022/09/12 15:24:05 info - 0.0102 seconds
[POP] 2022/09/12 15:24:05 warn - Migrator: unable to dump schema: open migrations/schema.sql: no such file or directory
time="2022-09-12T15:24:06Z" level=info msg="go test -p 1 -tags development testr/actions testr/cmd/app testr/grifts testr/locales testr/models testr/public testr/templates"
go: downloading github.com/gobuffalo/suite/v4 v4.0.3
go: downloading github.com/gobuffalo/httptest v1.5.1
go: downloading github.com/stretchr/testify v1.8.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
ok      testr/actions   0.019s
?       testr/cmd/app   [no test files]
?       testr/grifts    [no test files]
?       testr/locales   [no test files]
ok      testr/models    0.012s
?       testr/public    [no test files]
?       testr/templates [no test files]


Enter fullscreen mode Exit fullscreen mode

瞧!工作流程成功了。它不会为我们创建 Docker 镜像(所以没有生成任何工件)——但它确实运行了一些基本的集成测试。

Buffalo 应用程序的测试是另一篇文章的另一个主题。

总结一下

Buffalo 是一个经过深思熟虑的快速开发框架,适用于全栈应用或独立后端 API。它确实包含了很多入门功能,但仍然让开发者感到茫然——缺乏关于在何处以及如何将代码部署到生产环境的明确指导。

您使用什么来快速启动新服务?
您希望我们介绍哪些其他快速开发框架?

请在评论中告诉我们 - 我们的研究才刚刚开始!

文章来源:https://dev.to/otomato_io/getting-started-with-buffalo-1cc0
PREV
不仅仅是另一个带有实时统计数据的冠状病毒 (covid-19) 仪表板
NEXT
自动为您的 PR 提供灯塔评分(使用 Vercel 和 Github Actions)