Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit 3e4e481

Browse files
committed
feat: add healthcheck api
1 parent c307f1b commit 3e4e481

File tree

3 files changed

+85
-16
lines changed

3 files changed

+85
-16
lines changed

nuxt.config.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ module.exports = {
1111
mode: 'universal',
1212
srcDir: 'src/',
1313

14-
// ビルド時に渡される env の値は、ここに記載することで文字列に置換される
14+
/**
15+
* 環境変数
16+
* ビルド時に渡される env の値は、ここに記載することで文字列に置換される
17+
*/
1518
env: {
1619
NODE_ENV: process.env.NODE_ENV,
1720
BUILD_ENV: process.env.BUILD_ENV,
@@ -20,12 +23,13 @@ module.exports = {
2023
externalEndpointUrl: process.env.externalEndpointUrl
2124
},
2225

23-
/*
24-
** Build configuration
26+
/**
27+
* Build configuration
28+
* webpack のビルドに関する設定はここに書く
2529
*/
2630
build: {
27-
/*
28-
** You can extend webpack config here
31+
/**
32+
* You can extend webpack config here
2933
*/
3034
extend(config: Configuration, ctx: Context): void {
3135
// Run ESLint on save
@@ -54,8 +58,11 @@ module.exports = {
5458
}
5559
},
5660
extractCSS: isProduction,
61+
62+
// ビルドを爆速にする
5763
// https://qiita.com/toaru/items/0690a9110c94052bb479
5864
hardSource: true,
65+
5966
terser: {
6067
terserOptions: {
6168
compress: {
@@ -74,8 +81,8 @@ module.exports = {
7481
host: '0.0.0.0' // デフォルト: localhost
7582
},
7683

77-
/*
78-
** Headers of the page
84+
/**
85+
* Headers of the page
7986
*/
8087
head: {
8188
title: pkg.name,
@@ -87,21 +94,22 @@ module.exports = {
8794
link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
8895
},
8996

90-
/*
91-
** Customize the progress-bar color
97+
/**
98+
* Customize the progress-bar color
9299
*/
93100
loading: {
94101
color: 'blue',
95102
height: '5px'
96103
},
97104

98-
/*
99-
** Global CSS
105+
/**
106+
* Global CSS
107+
* 他の scss ファイルに依存しない scss はこちらに
100108
*/
101109
css: ['@/assets/styles/reset.scss', '@/assets/styles/main.scss'],
102110

103-
/*
104-
** Plugins to load before mounting the App
111+
/**
112+
* Plugins to load before mounting the App
105113
*/
106114
plugins: [
107115
'@/plugins/axios.ts',
@@ -143,8 +151,8 @@ module.exports = {
143151
config: {} // Additional config
144152
},
145153

146-
/*
147-
** Axios module configuration
154+
/**
155+
* Axios module configuration
148156
*/
149157
axios: {
150158
// See https://github.com/nuxt-community/axios-module#options
@@ -168,7 +176,17 @@ module.exports = {
168176
}
169177
},
170178

179+
/**
180+
* Sass の @extend を使う場合はこのパスの scss ファイルに CSS クラスを書く
181+
*/
171182
styleResources: {
172183
scss: ['@/assets/styles/components/**/*.scss']
173-
}
184+
},
185+
186+
/**
187+
* nuxt サーバーを API サーバーとして使う場合のミドルウェアを定義する
188+
*/
189+
serverMiddleware: [
190+
{ path: '/api/healthcheck', handler: '~/api/healthcheck.js' }
191+
]
174192
}

src/api/healthcheck.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// https://ja.nuxtjs.org/api/configuration-servermiddleware/
2+
const axios = require('axios')
3+
4+
const HEALTHCHECK_PATH = '/healthcheck'
5+
6+
// 関数の仕様はこちら
7+
// https://github.com/senchalabs/connect#appusefn
8+
module.exports = (req, res, next) => {
9+
console.log('/start healthcheck---')
10+
11+
let internalHealthcheckUrl
12+
13+
if (process.env.BUILD_ENV === 'docker') {
14+
/** docker のビルド環境の環境変数から値を取ってくる */
15+
internalHealthcheckUrl = `${
16+
process.env.internalEndpointUrl
17+
}${HEALTHCHECK_PATH}`
18+
} else {
19+
/** docker でビルドされていない場合は、 .env.local から値を取ってくる */
20+
internalHealthcheckUrl = `http://localhost:5000${HEALTHCHECK_PATH}`
21+
}
22+
console.log('internalHealthcheckUrl:', internalHealthcheckUrl)
23+
24+
res.writeHead(200, {
25+
'Content-Type': 'text/plain'
26+
// 'Content-Type': 'application/json'
27+
})
28+
29+
axios.get(`${internalHealthcheckUrl}`).then(response => {
30+
console.log(response.data)
31+
console.log(response.status)
32+
33+
// res.writeHead を使う場合は、 end しか使えない
34+
// https://qiita.com/kukimo/items/e686d480209464c6372f
35+
// res.write('ok')
36+
res.end('ok')
37+
})
38+
39+
console.log('/end healthcheck---')
40+
41+
// このミドルウェアがレスポンスを返さず、次に処理を移譲させるなら next() を実行すること
42+
// https://expressjs.com/en/guide/writing-middleware.html
43+
// next()
44+
}

tools/server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ function uuid() {
8787
return uuid
8888
}
8989

90+
/**
91+
* get '/healthcheck'
92+
*/
93+
app.get('/healthcheck', function(req, res) {
94+
res.send('ok')
95+
})
96+
9097
/**
9198
* post '/login'
9299
*/

0 commit comments

Comments
 (0)