본문 바로가기

GO lang/배포

[GO] Todo list - 배포(heroku)

이전 블로그[[GO] Todo list - session 별 데이터 관리] 소스를 이용하여 heroku.com 에서 배포하는 방법입니다.

최종소스는 여기서 참고하세요 ==> [heroku 배포용 최종소스], 변경된 DB까지 반영된 버전.

 

1. 기존 소스 코드를 모두 복사하여 자신이 원하는 폴더를 만든다.

heroku_todos>

 

 

2. mod, sum 파일 삭제 후 mod 초기화. go mod tidy

 

heroku_todos>go mod init heroku/todos
go: creating new go.mod: module heroku/todos
go: to add module requirements and sums:
        go mod tidy

heroku_todos>go mod tidy
go: finding module for package github.com/stretchr/testify/assert
go: finding module for package github.com/gorilla/sessions
go: finding module for package github.com/mattn/go-sqlite3
go: finding module for package github.com/gorilla/mux
go: finding module for package github.com/urfave/negroni
go: finding module for package github.com/unrolled/render
go: finding module for package golang.org/x/oauth2
go: finding module for package golang.org/x/oauth2/google
go: found github.com/mattn/go-sqlite3 in github.com/mattn/go-sqlite3 v1.14.9
go: found github.com/gorilla/mux in github.com/gorilla/mux v1.8.0
go: found github.com/gorilla/sessions in github.com/gorilla/sessions v1.2.1
go: found github.com/unrolled/render in github.com/unrolled/render v1.4.1
go: found github.com/urfave/negroni in github.com/urfave/negroni v1.0.0
go: found golang.org/x/oauth2 in golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
go: found golang.org/x/oauth2/google in golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
go: found github.com/stretchr/testify/assert in github.com/stretchr/testify v1.7.0

 

 

3. git 초기화. 

git 초기화 이후 heroku 설정을해야 heroku 의 repository 가 git 에 설정된다.

 

heroku_todos>git init
Initialized empty Git repository in heroku_todos/.git/

 

 

4. mod 에서 모듈명을 변경해서 하위패키지 이름 변경 필요함.

mod init 할때 모듈명을 "heroku/todos" 으로 변경하여 model 폴더 및 myapp 폴더 접근 경로를 수정해야 함.

- heroku/todos/model

- heroku/todos/myapp

 

  • main.go - 패키지 경로 및 포트정보 수정.
  • signin.go - DOMAIN_NAME 추가
  • app.go - 패키지 경로 수정. 

 

heroku_todos>go build -o ./bin/todos.exe -v .
heroku/todos

heroku_todos>.\bin\todos.exe
2021/11/23 23:05:56 Started App, portNo: 3000
[negroni] 2021-11-23T23:06:11+01:00 | 307 |      0s | localhost:3000 | GET /
[negroni] 2021-11-23T23:06:11+01:00 | 200 |      54.4733ms | localhost:3000 | GET /signin.html
[negroni] 2021-11-23T23:06:11+01:00 | 200 |      5.8559ms | localhost:3000 | GET /signin.css
[negroni] 2021-11-23T23:06:13+01:00 | 307 |      997.4µs | localhost:3000 | GET /auth/google/login
^C
heroku_todos>
heroku_todos>

 

 

5. Procfile 복사

heroku 예제 샘플을 참고하여, Procfile 을 조금 수정해서 사용하였습니다.

 

 

6. heroku 가입 및 환경 설정.

heroku.com 에 가입하고 윈도우 64비트용 패키지를 설치한다.

소스코드가 있는 메일 폴더에서 실행하면 된다.

 

heroku_todos>heroku login
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/94fccc2e-2bb3-46bc-bf9c-f38912abd960?requestor=SFMyNTY.g2gDbQAAAAs1LjEwMi41LjE1OG4GAED62U59AWIAAVGA.BqvA2hy5wCRYfx14u9BIqbhAoSwdqQSu_jp1q-6hnSo
Logging in... done
Logged in as xxx@gmail.com


heroku_todos>heroku create
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
Creating app... done, ⬢ fierce-sands-85340
https://fierce-sands-85340.herokuapp.com/ | https://git.heroku.com/fierce-sands-85340.git

heroku_todos>heroku config
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
=== fierce-sands-85340 Config Vars


heroku_todos>heroku apps
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
=== xxx@gmail.com Apps
aqueous-oasis-76275
fierce-sands-85340  <<=== 이번에 생성한 환경.
secret-shelf-65913
young-citadel-09000

 

 

7. git add

 

heroku_todos>git add .
warning: LF will be replaced by CRLF in go.mod.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in go.sum.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in public/signin.css.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in public/signin.html.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in public/todo.css.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in public/todo.html.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in public/todo.js.
The file will have its original line endings in your working directory

 

 

8. git commit
heroku_todos>git commit -m "todos v1.0"

 

 

9. git push
heroku_todos>git push heroku master

 

// 현재 환경이 연결된 서비스 이름 및 환경설정 확인 명령어
heroku_todos>heroku config
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
=== fierce-sands-85340 Config Vars

// git push 를 하면 자동으로 소스가 재컴파일되고 서비스도 다시 실행된다.
heroku_todos>heroku logs --tail
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
2021-11-23T22:19:06.482514+00:00 app[api]: Initial release by user xxx@gmail.com
2021-11-23T22:19:06.482514+00:00 app[api]: Release v1 created by user xxx@gmail.com
2021-11-23T22:19:07.721361+00:00 app[api]: Release v2 created by user xxx@gmail.com
2021-11-23T22:19:07.721361+00:00 app[api]: Enable Logplex by user xxx@gmail.com
2021-11-23T22:21:16.000000+00:00 app[api]: Build started by user xxx@gmail.com
2021-11-23T22:23:39.674711+00:00 app[api]: Release v3 created by user xxx@gmail.com
2021-11-23T22:23:39.674711+00:00 app[api]: Deploy 50cc5887 by user xxx@gmail.com
2021-11-23T22:23:39.694435+00:00 app[api]: Scaled to web@1:Free by user xxx@gmail.com
2021-11-23T22:23:42.126200+00:00 heroku[web.1]: Starting process with command `bin/todos`
2021-11-23T22:23:42.810637+00:00 app[web.1]: 2021/11/23 22:23:42 Started App, portNo: 36399
2021-11-23T22:23:43.317339+00:00 heroku[web.1]: State changed from starting to up
2021-11-23T22:24:14.000000+00:00 app[api]: Build succeeded

 

 

10. config 설정.

 

heroku_todos> heroku config:set DOMAIN_NAME="https://fierce-sands-85340.herokuapp.com"
heroku_todos> heroku config:set GOOGLE_CLIENT_ID="개인키"
heroku_todos> heroku config:set GOOGLE_SECRET_KEY="개인키"
heroku_todos> heroku config:set SESSION_KEY="개인키"

// 환경변수 확인 명령어
heroku_todos>heroku config
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
=== fierce-sands-85340 Config Vars
DOMAIN_NAME:       https://fierce-sands-85340.herokuapp.com
GOOGLE_CLIENT_ID:  개인키
GOOGLE_SECRET_KEY: 개인키
SESSION_KEY:       개인키

 

 

11. google > 사용자 인증번호 > Oauth2.0 client ID 설정 > URI 설정.

heroku create 시 자동생성된 주소를 사용하면 된다. heroku 에서 자동 할당하는 랜덤 주소이다.

 

 

 

 

서비스는 "https://fierce-sands-85340.herokuapp.com" 여기서 확인 가능함.

 

 

참고자료

[사용자 인증정보 추가]

[유투브 링크]