くらげになりたい。

くらげのようにふわふわ生きたい日曜プログラマなブログ。趣味の備忘録です。

SpringBootをはじめたあとにやるはじめの一歩(Heroku使う編)

前の記事の続き。ひさびさにSpringBootでアプリを作ろうと思ったけど、だいぶ記憶が。。。
思い出してやったことの備忘録φ(..)メモメモ

wannabe-jellyfish.hatenablog.com

前の記事で以下まで終わっているイメージ。ただ、言語はKotlinになりました。

  1. Spring Initializrでひな形プロジェクトを作成
  2. giboで.gitignoreを作成

「パッケージ構成の設定」のディレクトリ構成を作るスクリプト

GUIでぽちぽちディレクトリ構成を作るのがめんどくさいので、スクリプト化。
propertiesもgistにあげて、wgetを取得するようにした。

#!/bin/bash

ROOT='.'
SRC_ROOT="${ROOT}/src/main/kotlin/com/example"
RES_ROOT="${ROOT}/src/main/resources"

### create base directories
mkdir ${SRC_ROOT}/app
mkdir ${SRC_ROOT}/domain
mkdir ${SRC_ROOT}/domain/entitiy
mkdir ${SRC_ROOT}/domain/repository
mkdir ${SRC_ROOT}/domain/service
mkdir ${SRC_ROOT}/domain/utils

### download resource via Gist

# 本番環境用の設定&共通設定
wget https://gist.github.com/memory-lovers/c5e291c4120d0e8f70b7b112e56982f4/raw/48b48bcae1cd513c9aebd01b71d7e07cff2620f3/application.properties -O ${RES_ROOT}/application.properties

# ローカル環境用のDB設定。H2を使う感じ。
wget https://gist.github.com/memory-lovers/2dc35d8b09b004f24199dc21e8b0cf85/raw/c827450e7ee77b3f04352507bf23b4c5775a9b2a/application-local.properties -O ${RES_ROOT}/application-local.properties

一部、仮当てなので以下の設定が必要。

  1. application.propertiesのログ設定をcom.exampleから自分のルートパッケージに変更
  2. .propertiesにあるDBの設定を追記

Heroku用の設定を追加

とりあえず、公開するよう場として、Herokuを使うので、それ用の設定。HerokuのDBを使ったりとか。
これもGist&スクリプト化。

#!/bin/bash

ROOT='.'
SRC_ROOT="${ROOT}/src/main/kotlin/com/example"
RES_ROOT="${ROOT}/src/main/resources"

# 本番環境用のHerokuのDB設定
wget https://gist.github.com/memory-lovers/191f79c32d50eca012df96e58f0ab746/raw/a739d16e7b1d748414f68a1c8b2c67da58b439c4/application-heroku.properties -O ${RES_ROOT}/application-heroku.properties

# 試験環境用のHerokuのDB設定
wget https://gist.github.com/memory-lovers/db31a6f7ffc3ec47eba5382aeccd0db6/raw/0de61ad65dba07eca22edf76fa448ce97c93edc3/application-heroku_dev.properties -O ${RES_ROOT}/application-heroku_dev.properties

# HerokuのDBを使うようのConfig
wget https://gist.github.com/memory-lovers/352aa0c33070c2acd842fe9aa596e1b7/raw/f46909b7f9a5e80630aa59ac764ceb1a142cd6bd/HerokuConfig -O ${SRC_ROOT}/HerokuConfig.java

# HerokuのProfile。デフォルトでは、試験環境用の設定を使う形
wget https://gist.github.com/memory-lovers/7ac33e32e8d2afc9bd13af34f8b8022b/raw/f625f7b76597fd6bdb195c43bf04fb74ae8ed6ea/Procfile -O ${ROOT}/Procfile

こっちも仮当てなので、以下の設定が必要。

  1. .propertiesにあるDBの設定を追記

以上!!

参考になる書籍

Spring Boot 2 プログラミング入門

Spring Boot 2 プログラミング入門