-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f4848d
commit 01dff09
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
--- | ||
path: /docker-swaggger-ui | ||
created: "2024-05-08" | ||
title: Dockerとyamlで言語非依存にSwagger UIを構築する | ||
visual: "./visual.png" | ||
tags: ["docker", "swagger"] | ||
userId: sadnessOjisan | ||
isFavorite: false | ||
isProtect: false | ||
--- | ||
|
||
## 言語非依存にSwagger UIを構築したいモチベーション | ||
|
||
スキーマファーストにAPI Specを作るということは、それはバックエンドチーム、クライアントサイドチームの共有財産だと思います。 | ||
お互いが異なる言語をつかているのならば、どちらかの言語で書いてしまうとどちらかに学習コストが発生するので、SpecはJSONやyamlなどで書きたいです。 | ||
ただJSONだとコメントが書けなかったり、JSON SChema 手書きはしんどかったりで避けたいです。 | ||
そこで yaml だけで swagger-ui を使って API Spec を作りたいです。 | ||
|
||
## swagger-ui が v3 から yaml を読める | ||
|
||
swagger-ui は v3 から yaml に対応しました。 | ||
元々は環境変数SWAGGER_JSONにspecのjsonへのパスを渡すことで構築できていました。 | ||
|
||
see: https://swagger.io/docs/open-source-tools/swagger-ui/usage/installation/#docker | ||
|
||
これが API_URL という環境変数にyamlへのパスを渡すと同じように構築できるようになりました。 | ||
(なんだけど、公式のドキュメント見てもそんなことは書かれてないんだよな) | ||
|
||
``` | ||
FROM swaggerapi/swagger-ui:v5.17.3 | ||
# see: https://qiita.com/NewGyu/items/6a00e0f09a7f8a93cfd5 | ||
COPY spec.yaml /usr/share/nginx/html/spec.yaml | ||
ENV API_URL=spec.yaml | ||
EXPOSE 8080 | ||
``` | ||
|
||
swagger ui は組み込まれているNginxで配信されているので、nginx が見てるルートにスペックをコピーし、そこにAPI_URLを設定しておきます。 | ||
|
||
## Basic 認証する | ||
|
||
さて、API仕様書を別チームで共有するからには、そのSpecはインターネット越しに常に最新のものが見られるようにしたいです。 | ||
ただインターネットに公開されているのは危険なのでBasicをかけます。 | ||
なんらかの言語を使っているならその言語の機能でBasic認証は簡単に実装できますが、今我々はそのようなものはないです。 | ||
そのためNginxの上に乗っかって、Basic認証をします。 | ||
|
||
``` | ||
FROM swaggerapi/swagger-ui:v5.17.3 | ||
# see: https://qiita.com/NewGyu/items/6a00e0f09a7f8a93cfd5 | ||
COPY spec.yaml /usr/share/nginx/html/spec.yaml | ||
ENV API_URL=spec.yaml | ||
# Basic認証 | ||
# see: https://github.com/ravindu1024/swagger-ui-basic-auth/blob/master/Dockerfile | ||
RUN apk update | ||
RUN apk add apache2-utils | ||
ARG USERNAME=hoge | ||
ARG PASSWORD=fugafuga | ||
RUN htpasswd -c -b /etc/nginx/.htpasswd ${USERNAME} ${PASSWORD} && cat /etc/nginx/.htpasswd | ||
COPY basic-auth.sh /docker-entrypoint.d/ | ||
RUN chmod +x /docker-entrypoint.d/basic-auth.sh | ||
RUN cat /etc/nginx/conf.d/default.conf | ||
EXPOSE 8080 | ||
``` | ||
|
||
``` | ||
sed -i 's|location / {|location / {\n\tauth_basic \"Restricted Content\";\n\tauth_basic_user_file /etc/nginx/.htpasswd;\n|g' /etc/nginx/conf.d/default.conf | ||
sleep 5 && nginx -s reload & | ||
``` | ||
|
||
htpasswdは Apache のためのようにも思えるが、Nginx も auth_basic_user_file で htpasswd を指定するとBasic認証ができます。 | ||
そのため htpasswd を作り、それをコンテナ立ち上げ時にdefault設定されているNginxの設定に対して htpasswd を使うように書き換えます。 | ||
そうすることで Basic 認証が実現できます。 | ||
|
||
このやり方は https://github.com/ravindu1024/swagger-ui-basic-auth/blob/master/Dockerfile にある Dockerfile を参考にしました。 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
path: /miss-gradle-version | ||
created: "2024-05-08" | ||
title: InteliJ で作った Kotlin new Project が Gradle のバージョン設定が古くてビルドできなかった | ||
visual: "./visual.png" | ||
tags: ["kotlin", "intelij", "gradle"] | ||
userId: sadnessOjisan | ||
isFavorite: false | ||
isProtect: false | ||
--- | ||
|
||
## new project した新品をビルドできない | ||
|
||
最近はサーバーサイド Kotlin の勉強をしているのですが、InteliJ から new project しただけのプロジェクトがビルドできなくて困っていました。 | ||
|
||
``` | ||
Could not open init generic class cache for initialization script '/private/var/folders/7m/c_dxyw993zbddrn7s6_g06l40000gn/T/wrapper_init6.gradle' (/Users/ideyuta/.gradle/caches/7.4.2/scripts/62yitbq6ojelk1741cldo8m6e). | ||
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 65 | ||
* Try: | ||
> Run with --stacktrace option to get the stack trace. | ||
> Run with --info or --debug option to get more log output. | ||
> Run with --scan to get full insights. | ||
``` | ||
|
||
みたところ Gradle が何かおかしいようです。 | ||
|
||
## Gradle と Java の対応を合わせる | ||
|
||
Java と Gradle のバージョンが合っていないときにこのエラーが出るらしいです。 | ||
そこで Gradle 公式の対応表で確認してみます。 | ||
|
||
see: https://docs.gradle.org/current/userguide/compatibility.html#java | ||
|
||
いま私が使っている JDK は v21 です。 | ||
なので Gradle は 8.5 以上が良いとのことです。 | ||
|
||
Gradle のバージョン切り替えは grade-wrapper.properties の distributionUrl で行えます。 | ||
これは gradle/wrapper/gradle-wrapper.properties にあります。 | ||
|
||
``` | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
``` | ||
|
||
いま distributionUrl は 7.4.2 だったので、これを 8.5 にします。 | ||
|
||
``` | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
``` | ||
|
||
そしてビルドすると、コンパイルに成功しました。 | ||
|
||
``` | ||
BUILD SUCCESSFUL in 478ms | ||
1 actionable task: 1 executed | ||
``` | ||
|
||
## まとめ | ||
|
||
new project で作られるプロジェクトはビルドが安定しているという訳ではない。 | ||
選択する JDK のバージョンによってはうまく行かない。 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.