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
1k
5分で作るモックサーバー
DroidKaigi.collect { #5@Nagoya } の 5分LTで発表しました
satsukies
July 08, 2023
Tweet
Share
More Decks by satsukies
See All by satsukies
Android スキルセットをフル活用して始めるスマートテレビアプリ開発
satsukies
1
710
Android TVに関するアップデート / What's new on Android TV
satsukies
0
200
Kotlin Nativeでクロスプラットフォーム開発 / Cross-platform development with Kotlin Native
satsukies
1
830
Navigation Component
satsukies
5
3.7k
「OK google, プロジェクトのbuildして」
satsukies
2
1.5k
いまさら始めるInstant App
satsukies
1
360
View Animation
satsukies
1
750
Other Decks in Programming
See All in Programming
Microservices rules (July 2024) : what good looks like
cer
PRO
0
1.6k
Terraformテスト入門
msato
0
520
Architectures with Lightweight Stores: New Rules and Options
manfredsteyer
PRO
0
100
Rust.Nagoya #1
codemountains
0
170
最古の関数型言語「Lisp」ことはじめ / lisp_in_kamiyama
uhooi
1
190
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
2
81k
TiDB Serverless ~理想のServerless DBを考える~
soso_15315
1
160
Android開発者のための Kotlin Multiplatform入門
ntaro
0
190
入社1ヶ月でここまでやった!Findy Toolsインフラ支援の最適化
rvirus0817
6
1.4k
Introduction of Happy Eyeballs Version 2 (RFC8305) to the Socket library
coe401_
1
220
AWSでゲームサーバーを運用! Amazon GameLiftのお話
iriikeita
0
200
スクラムマスターって孤独じゃないですか?
yoshitaroyoyo
1
140
Featured
See All Featured
Creatively Recalculating Your Daily Design Routine
revolveconf
214
11k
Bash Introduction
62gerente
607
210k
The Illustrated Children's Guide to Kubernetes
chrisshort
39
47k
Principles of Awesome APIs and How to Build Them.
keavy
124
16k
Why Our Code Smells
bkeepers
PRO
332
56k
What the flash - Photography Introduction
edds
65
11k
RailsConf 2023
tenderlove
16
720
A better future with KSS
kneath
231
17k
What's new in Ruby 2.0
geeforr
338
31k
Designing on Purpose - Digital PM Summit 2013
jponch
113
6.6k
Design by the Numbers
sachag
277
18k
Designing Experiences People Love
moore
136
23k
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制限などもできる