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
990
Android TVに関するアップデート / What's new on Android TV
satsukies
0
230
Kotlin Nativeでクロスプラットフォーム開発 / Cross-platform development with Kotlin Native
satsukies
1
970
Navigation Component
satsukies
5
4.1k
「OK google, プロジェクトのbuildして」
satsukies
2
1.7k
いまさら始めるInstant App
satsukies
1
440
View Animation
satsukies
1
870
Other Decks in Programming
See All in Programming
Docコメントで始める簡単ガードレール
keisukeikeda
1
110
AI活用のコスパを最大化する方法
ochtum
0
130
AIに任せる範囲を安全に広げるためにやっていること
fukucheee
0
130
TipKitTips
ktcryomm
0
160
Claude Code Skill入門
mayahoney
0
190
Unity6.3 AudioUpdate
cova8bitdots
0
120
AIコーディングの理想と現実 2026 | AI Coding: Expectations vs. Reality 2026
tomohisa
0
1.2k
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
1
140
Goの型安全性で実現する複数プロダクトの権限管理
ishikawa_pro
1
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
490
encoding/json/v2のUnmarshalはこう変わった:内部実装で見る設計改善
kurakura0916
0
390
「抽象に依存せよ」が分からなかった新卒1年目の私が Goのインターフェースと和解するまで
kurogenki
0
100
Featured
See All Featured
Information Architects: The Missing Link in Design Systems
soysaucechin
0
820
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.4k
Done Done
chrislema
186
16k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8k
Typedesign – Prime Four
hannesfritz
42
3k
KATA
mclloyd
PRO
35
15k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.7k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
980
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
970
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
82
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制限などもできる