[[Killgrave]]を使ってみた。[[WSL]]の[[Ubuntu]]にて。 <div class="link-card"> <div class="link-card-header"> <img src="https://github.githubassets.com/favicons/favicon.svg" class="link-card-site-icon"/> <span class="link-card-site-name">GitHub</span> </div> <div class="link-card-body"> <div class="link-card-content"> <div> <p class="link-card-title">GitHub - friendsofgo/killgrave: Simple way to generate mock servers written in Go</p> </div> <div class="link-card-description"> Simple way to generate mock servers written in Go. Contribute to friendsofgo/killgrave development b... </div> </div> <img src="https://opengraph.githubassets.com/3f39cc6231575688737029a449c3e990a2566f72932be4e3a17d45347ae386aa/friendsofgo/killgrave" class="link-card-image" /> </div> <a href="https://github.com/friendsofgo/killgrave"></a> </div> ## インストール `-host` ではなく `--host` なのでそこだけなおしておく。 ```console docker run -it --rm -p 3000:3000 -v $PWD/:/home -w /home friendsofgo/killgrave --host 0.0.0.0 ``` とりあえずエラーになる。 ``` stat imposters: no such file or directory: the directory imposters doesn't exists ``` `imposters`が必要っぽい。 [デフォルト値は以下の通り](https://github.com/friendsofgo/killgrave#getting-started)。 ``` imposters path: imposters host: localhost port: 3000 CORS: [] proxy: none watcher: false ``` ## Imposterの作成 最低1つは`imposters`配下に`.imp.json`を作成する必要がある。 > https://github.com/friendsofgo/killgrave#creating-an-imposter ためしにREADMEに記載されていた[[JSON]]を`sample.imp.json`として作成する。 ```json [ { "request": { "method": "GET", "endpoint": "/gophers/01D8EMQ185CA8PRGE20DKZTGSR" }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "body": "{\"data\":{\"type\":\"gophers\",\"id\":\"01D8EMQ185CA8PRGE20DKZTGSR\",\"attributes\":{\"name\":\"Zebediah\",\"color\":\"Purples\",\"age\":55}}}" } } ] ``` もう一度実行する。 ```console $ docker run -it --rm -p 3000:3000 -v $PWD/:/home -w /home friendsofgo/killgrave --host 0.0.0.0 2023/12/01 07:47:37 imposter imposters/sample.imp.json loaded 2023/12/01 07:47:37 The fake server is on tap now: 0.0.0.0:3000 ``` 起動したようだ。ためしに以下へアクセスしてみる。 http://localhost:3000/gophers/01D8EMQ185CA8PRGE20DKZTGSR bodyに設定したJSONが取得できた。 ## hotreloadできるようにする `-w`オプションをつける。 ```console docker run -it --rm -p 3007:3000 -v $PWD/:/home -w /home friendsofgo/killgrave --host 0.0.0.0 -w ``` `imposters`配下を変更すると即時反映される。 ## トラブルシューティング - `--host 0.0.0.0` を抜くとアクセスできなかった - jsonファイルで`request.param`の値がstring(ダブルクォーテーションで囲む)になっていないと起動しない