使用Heroku创建Golang的RestAPI。(使用echo入门版)

目的

使用Golang在Heroku上创建RestAPI,使用的框架是echo。

环境信息

电脑:Mac(使用英特尔CPU)
Go:1.17.6 ←Go的版本很重要。请安装1.16或更高版本。
开发编辑器:Visual Studio Code

目录结构

~/go/src/go_echo $ tree
.
├── Procfile
├── go.mod
├── go.sum
└── main.go

项目的创建目录在GOPATH下。

环境设置

~/go/src/go_echo $ git init
Initialized empty Git repository in /Users/kawamurakouji/go/src/go_echo/.git/
~/go/src/go_echo $ go mod init
go: creating new go.mod: module go_echo
~/go/src/go_echo $ go get github.com/labstack/echo/v4
go get: added github.com/labstack/echo/v4 v4.6.3
go get: added github.com/labstack/gommon v0.3.1
go get: added github.com/mattn/go-colorable v0.1.11
go get: added github.com/mattn/go-isatty v0.0.14
go get: added github.com/valyala/bytebufferpool v1.0.0
go get: added github.com/valyala/fasttemplate v1.2.1
go get: added golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
go get: added golang.org/x/net v0.0.0-20210913180222-943fd674d43e
go get: added golang.org/x/sys v0.0.0-20211103235746-7861aae1554b
go get: added golang.org/x/text v0.3.7

~/go/src/go_echo $ go get github.com/labstack/echo/v4/middleware

编写程序

package main

import (
    "net/http"
    "os"

    "github.com/labstack/echo/v4"
    "github.com/labstack/echo/v4/middleware"
)

func main() {
    // Echo instance
    e := echo.New()

    // Middleware
    e.Use(middleware.Logger())
    e.Use(middleware.Recover())

    // Routes
    e.GET("/", hello)

    // Use 1323 as default port
    port := os.Getenv("PORT")
    if port == "" {
        port = "1323"
    }

    // Start server
    e.Logger.Fatal(e.Start(":" + port))
}

// Handler
func hello(c echo.Context) error {
    return c.String(http.StatusOK, "Hello, World!")
}

本地执行

~/go/src/go_echo $ go run main.go 

   ____    __
  / __/___/ /  ___
 / _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.6.3
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
                                    O\
⇨ http server started on [::]:1323
スクリーンショット 2022-02-12 19.08.09.png

部署准备

web: bin/go_echo
module go_echo
// +heroku goVersion go1.17    ←1行追加。HerokuにbuildするVersionを指定。
go 1.17

require (
    github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
    github.com/labstack/echo/v4 v4.6.3 // indirect
    github.com/labstack/gommon v0.3.1 // indirect
    github.com/mattn/go-colorable v0.1.11 // indirect
    github.com/mattn/go-isatty v0.0.14 // indirect
    github.com/valyala/bytebufferpool v1.0.0 // indirect
    github.com/valyala/fasttemplate v1.2.1 // indirect
    golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
    golang.org/x/net v0.0.0-20210913180222-943fd674d43e // indirect
    golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
    golang.org/x/text v0.3.7 // indirect
    golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
)
~/go/src/go_echo $ git add ./
~/go/src/go_echo $ git commit -m "initial commit"
[main (root-commit) 00aff07] initial commit
 4 files changed, 96 insertions(+)
 create mode 100644 Procfile
 create mode 100644 go.mod
 create mode 100644 go.sum
 create mode 100644 main.go
~/go/src/go_echo $ heroku create       
Creating app... done, ⬢ obscure-fjord-75737
https://XXXXX-XXXXX-XXXXX.herokuapp.com/ | https://git.heroku.com/XXXXX-XXXXX-XXXXX.git

部署执行

git push heroku main
Enumerating objects: 521, done.
Counting objects: 100% (521/521), done.
Delta compression using up to 8 threads
Compressing objects: 100% (309/309), done.
Writing objects: 100% (521/521), 226.26 KiB | 45.25 MiB/s, done.
Total 521 (delta 141), reused 501 (delta 134)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Go app detected
remote: -----> Fetching jq... done
remote:
remote: -----> Detected go modules - go.mod
remote:
remote: -----> Installing go1.12.1
remote: -----> Fetching go1.12.1.linux-amd64.tar.gz... done
remote:  !!    Installing package '.' (default)
remote:  !!
remote:  !!    To install a different package spec add a comment in the following form to your `go.mod` file:
remote:  !!    // +heroku install ./cmd/...
remote:  !!
remote: -----> Running: go install -v -tags heroku -mod=vendor .
remote: gopkg.in/bluesuncorp/validator.v5
remote: github.com/gin-gonic/gin/render
remote: github.com/manucorporat/sse
remote: github.com/mattn/go-colorable
remote: golang.org/x/net/context
remote: github.com/heroku/x/hmetrics
remote: github.com/heroku/x/hmetrics/onload
remote: github.com/gin-gonic/gin/binding
remote: github.com/gin-gonic/gin
remote: github.com/heroku/go-getting-started
remote:
remote: Compiled the following binaries:
remote:        ./bin/go-getting-started
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 5.5M
remote: -----> Launching...
remote:        Released v3
remote:        https://go-on-heroku.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/go-on-heroku.git
 * [new branch]      main -> main

→验证访问所示URL时是否会显示”Hello, World”。

广告
将在 10 秒后关闭
bannerAds