Heroku 部署示例
本项目演示了如何使用 Fiber 框架在 Heroku 上部署 Go 应用程序。
先决条件
请确保您已安装以下内容
- Golang
- Fiber 包
- Heroku CLI
设置
-
克隆仓库
git clone https://github.com/gofiber/recipes.git
cd recipes/heroku -
安装依赖
go get
-
登录 Heroku
heroku login
-
创建一个新的 Heroku 应用程序
heroku create
-
在项目目录中添加一个
Procfile
文件,内容如下:web: go run main.go
-
将应用程序部署到 Heroku
git add .
git commit -m "Deploy to Heroku"
git push heroku master
运行应用程序
- 在浏览器中打开应用程序
heroku open
示例
以下是 Fiber 应用程序的 main.go
文件示例
package main
import (
"log"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, Heroku!")
})
log.Fatal(app.Listen(":" + getPort()))
}
func getPort() string {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
return port
}