Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
5分で作るモックサーバー
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
satsukies
July 08, 2023
Programming
0
1.3k
5分で作るモックサーバー
DroidKaigi.collect { #5@Nagoya } の 5分LTで発表しました
satsukies
July 08, 2023
Tweet
Share
More Decks by satsukies
See All by satsukies
Android スキルセットをフル活用して始めるスマートテレビアプリ開発
satsukies
1
980
Android TVに関するアップデート / What's new on Android TV
satsukies
0
230
Kotlin Nativeでクロスプラットフォーム開発 / Cross-platform development with Kotlin Native
satsukies
1
960
Navigation Component
satsukies
5
4.1k
「OK google, プロジェクトのbuildして」
satsukies
2
1.6k
いまさら始めるInstant App
satsukies
1
430
View Animation
satsukies
1
860
Other Decks in Programming
See All in Programming
360° Signals in Angular: Signal Forms with SignalStore & Resources @ngLondon 01/2026
manfredsteyer
PRO
0
100
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
Vibe codingでおすすめの言語と開発手法
uyuki234
0
210
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
510
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.3k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
440
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
6
1.8k
CSC307 Lecture 06
javiergs
PRO
0
680
[KNOTS 2026登壇資料]AIで拡張‧交差する プロダクト開発のプロセス および携わるメンバーの役割
hisatake
0
230
CSC307 Lecture 04
javiergs
PRO
0
650
AIによる高速開発をどう制御するか? ガードレール設置で開発速度と品質を両立させたチームの事例
tonkotsuboy_com
2
670
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
170
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
What's in a price? How to price your products and services
michaelherold
247
13k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
6.9k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
920
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
270
WENDY [Excerpt]
tessaabrams
9
36k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Into the Great Unknown - MozCon
thekraken
40
2.2k
The Curious Case for Waylosing
cassininazir
0
230
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
62
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
0
270
Transcript
5分で作るモックサーバー DroidKaigi.collect { #5@Nagoya } satsukies
自己紹介 • @satsukies (さつき) ◦ twitter / github • 前職:
CyberAgent / ABEMA ◦ Androidアプリエンジニア • 現職: 株式会社DeployGate ◦ Androidアプリエンジニア ◦ フロントエンド/バックエンドもやってます
こういうこと、ありませんか • API繋ぎこみしたい!! ◦ だけど、バックエンドも同時開発進行中ですぐにできない...! • エッジケースとか デバッグしたい!! ◦ 任意
レスポンスをアプリに受け取らせてみたい ◦ だけど正攻法だと思ったよりも大変かも...!
goでサーバー立てれ 意外とサクッといけるかも?
• OkHttp + RetrofitでAPI呼び出し 実装をしているAndroid App • インターネットに繋がっているPC ◦ 今回
MacOS環境で検証しています • ちょっと試してみたいと思える心 必要なも
1. 開発環境を整えよう まず golangでコードがかける環境を用意しましょう すでにHomebrewを導入していれ 以下 1行実行でOK VS CodeやIntelliJ GoLandなど
IDEがあるとちょっと楽 $ brew install go
2. go環境 構築 まず 作業用 ディレクトリを用意して移動 $ mkdir mock-server $
cd /path/to/mock-server
2. go環境 構築 go moduleを新規作成して、必要なファイルを生成しておきます $ go mod init mock-server
go: creating new go.mod: module echo-server $ touch main.go response.json
$ ls go.mod main.go response.json 2. go環境 構築 ここまで作業すると、3つ ファイルが存在する状態になる
ずです
3. 返したいレスポンスをJSONファイルに記述 生成しておいたresponse.jsonにJSONを記述する // response.json { "id": 12345, "message": "This
message is send by mock-server" }
4. main.goにサーバー実装を記述 net/httpパッケージを使うと、とっても簡単に実装できます // main.go func main() { http.HandleFunc("/", handleRequest)
http.ListenAndServe(":8080", nil) }
4. main.goにサーバー実装を記述 JSONファイルを読み出して、中身をそ ままWriteしちゃいます // main.go func handleRequest(w http.ResponseWriter, r
*http.Request) { byteArray, _ := ioutil.ReadFile("response.json") w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(byteArray) }
JSONファイルを読み出して、中身をそ ままWriteしちゃいます package main import ( "io/ioutil" "net/http" ) func
main() { http.HandleFunc("/", handleRequest) http.ListenAndServe(":8080", nil) } func handleRequest(w http.ResponseWriter, r *http.Request) { byteArray, _ := ioutil.ReadFile("response.json") w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(byteArray) }
5. 実際に動かしてみる go アプリケーション 動かす も簡単 $ go run main.go
5. 実際に動かしてみる localhost:8080 にアクセスすると....
6. ngrokを使って外部公開しよう ngrokというサービスを使うと、手元 環境を簡単に公開できます https://ngrok.com/
6. ngrokを使って外部公開しよう ngrok インストールもHomebrew経由で簡単にできます $ brew install ngrok // token追加
$ ngrok config add-authtoken {your_auth_token}
6. ngrokを使って外部公開しよう WebからSignUp後に提供されるコマンドを使ってログインすると使える $ brew install ngrok // token追加 $
ngrok config add-authtoken {your_auth_token}
6. ngrokを使って外部公開しよう ngrok http {port} で起動できます $ ngrok http 8080
7. Android Appを修正 Retrofit annotationに 相対/絶対パス以外に完全なURLも渡せる(!)
7. Android Appを修正 Retrofit annotationに 相対/絶対パス以外に完全なURLも渡せる A relative or absolute
path, or full URL of the endpoint.
7. Android Appを修正 Retrofit annotationに 相対/絶対パス以外に完全なURLも渡せる @GET("/hoge") override suspend fun
hoge(): HogeResponse { … } @GET("https://sample.com/foo") override suspend fun foo(): FooResponse { … }
7. Android Appを修正 Retrofit annotationに渡しているAPI pathをngrok URLで置き換える // before @GET("/hoge")
override suspend fun hoge(): HogeResponse { … }
7. Android Appを修正 Retrofit annotationに渡しているAPI pathをngrok URLで置き換える // after @GET(“https://xxxx-yy-zzz.ngrok-free.app”)
override suspend fun hoge(): HogeResponse { … }
超簡易的なモックサーバが これで動かせます🎉
まとめ • Retrofitで呼び出し先を指定する方法 相対パスだけで ない ◦ 絶対パス、完全URLも使える • Goを使って簡易的なモックサーバーがサクッと作れる ◦
複雑で ない用途であれ 扱いやすいかも?! • ngrokを活用することで簡単に外部公開できる ◦ あくまでテスト用途にしておき、重要なデータ 扱わない ◦ 有料プランで固定 URLやIP制限などもできる