> Go언어로 짠 것 Heroku에 배포하기
Crescent
Table of Contents
0. 서론
Getting Started on Heroku with Go - Heroku Dev Center
- 순서가 조금은 뒤죽박죽 일 수도 있습니다.
- 설명이 부족할 수 있습니다.
- 미래의 제가 이 글을 쓰는 저에게 감사를 표하기 위해 썼습니다.
01. Edit main.go
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.GET("/api/v1/:slug", getUser)
port := os.Getenv("PORT")
router.Run(":" + port)
}
import "os"
해두고os.Getenv()
를 이용해 중요한 변수들을 관리한다.gin
에서는 배포용일 때는gin.SetMode(gin.ReleasMode)
를 써야 한다고 한다.- 포트 번호는 정해두면 배포에 문제가 생긴다고 한다.
go mod init crescent-stdio/todo-list
go tidy
go mod vendor
- 기본 설정
02. git
git init
git branch -m main
git add .
git commit -m "Heroku deploy test"
- 기본적인
git
을 설정하고 커밋한다.
03. Heroku CLI
brew tap heroku/brew && brew install heroku
Mac
기준으로Heroku
를 설치- 다른 os는 다음 참고: Getting Started on Heroku with Go
heroku create crescent-todo
heroku git:remote -a crescent-todo
crescent-todo
는 이름 마음대로
![Heroku 사이트에서 Config Vars에 .env 변수를 추가한 모습](/image/220910-deploying-golang-to-heroku/config-vars.png)
GIN_MODE="release"
.env
파일을 다음과 같이 넣어준다.(GIN_MODE
가 있어야 한다고 에러메세지가 그러긴 했는데 확실히는 모르겠다)- 아까
main.go
에는PORT
가 있지만.env
와Config Vars
에 안넣어도 잘 작동한다.
web: bin/todo
root
에Procfile
을bin/뭐시기
로 설정한다.
04. main.go
build
go build -o bin/todo -v .
Procfile
에 설정한 이름 그대로 쓰면 된다..
← 빼먹지 말기
05. Heroku Deploy
git push heroku main
![Heroku에서 dyno를 ON한 모습](/image/220910-deploying-golang-to-heroku/dyno.png)
heroku ps:scale web=1
Configure Dynos
가서ON
시킨다.
git push heroku main
- 푸싱!!
- 배포!!!
heroku open crescent-todo
deploy
한 사이트 오픈
heroku logs --tail
- 실시간 로그는 다음으로 확인할 수 있다.