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.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
940
Android TVに関するアップデート / What's new on Android TV
satsukies
0
220
Kotlin Nativeでクロスプラットフォーム開発 / Cross-platform development with Kotlin Native
satsukies
1
940
Navigation Component
satsukies
5
4k
「OK google, プロジェクトのbuildして」
satsukies
2
1.6k
いまさら始めるInstant App
satsukies
1
410
View Animation
satsukies
1
840
Other Decks in Programming
See All in Programming
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
640
時間軸から考えるTerraformを使う理由と留意点
fufuhu
15
4.6k
CloudflareのChat Agent Starter Kitで簡単!AIチャットボット構築
syumai
2
470
もうちょっといいRubyプロファイラを作りたい (2025)
osyoyu
0
400
Processing Gem ベースの、2D レトロゲームエンジンの開発
tokujiros
2
120
旅行プランAIエージェント開発の裏側
ippo012
2
890
アセットのコンパイルについて
ojun9
0
120
Cache Me If You Can
ryunen344
2
630
Kiroで始めるAI-DLC
kaonash
2
580
はじめてのMaterial3 Expressive
ym223
2
250
Design Foundational Data Engineering Observability
sucitw
3
190
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
410
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
9
580
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
How to Ace a Technical Interview
jacobian
279
23k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Designing Experiences People Love
moore
142
24k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
The Cult of Friendly URLs
andyhume
79
6.6k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
358
30k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Scaling GitHub
holman
463
140k
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制限などもできる