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
satsukies
July 08, 2023
Programming
0
1.2k
5分で作るモックサーバー
DroidKaigi.collect { #5@Nagoya } の 5分LTで発表しました
satsukies
July 08, 2023
Tweet
Share
More Decks by satsukies
See All by satsukies
Android スキルセットをフル活用して始めるスマートテレビアプリ開発
satsukies
1
840
Android TVに関するアップデート / What's new on Android TV
satsukies
0
210
Kotlin Nativeでクロスプラットフォーム開発 / Cross-platform development with Kotlin Native
satsukies
1
910
Navigation Component
satsukies
5
3.9k
「OK google, プロジェクトのbuildして」
satsukies
2
1.6k
いまさら始めるInstant App
satsukies
1
390
View Animation
satsukies
1
800
Other Decks in Programming
See All in Programming
JavaScriptツール群「UnJS」を5分で一気に駆け巡る!
k1tikurisu
10
1.8k
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
4
370
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
1
640
Rails アプリ地図考 Flush Cut
makicamel
1
110
Amazon ECS とマイクロサービスから考えるシステム構成
hiyanger
2
490
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
120
Spring gRPC について / About Spring gRPC
mackey0225
0
220
Pulsar2 を雰囲気で使ってみよう
anoken
0
230
CI改善もDatadogとともに
taumu
0
110
ARA Ansible for the teams
kksat
0
150
Lottieアニメーションをカスタマイズしてみた
tahia910
0
120
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
Docker and Python
trallard
44
3.3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Fireside Chat
paigeccino
34
3.2k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
Designing for Performance
lara
604
68k
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制限などもできる