跳到主内容

Heroku 部署示例

Github StackBlitz

本项目演示了如何使用 Fiber 框架在 Heroku 上部署 Go 应用程序。

先决条件

请确保您已安装以下内容

设置

  1. 克隆仓库

    git clone https://github.com/gofiber/recipes.git
    cd recipes/heroku
  2. 安装依赖

    go get
  3. 登录 Heroku

    heroku login
  4. 创建一个新的 Heroku 应用程序

    heroku create
  5. 在项目目录中添加一个 Procfile 文件,内容如下:

    web: go run main.go
  6. 将应用程序部署到 Heroku

    git add .
    git commit -m "Deploy to Heroku"
    git push heroku master

运行应用程序

  1. 在浏览器中打开应用程序
    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
}

参考资料