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
おうちにAPIを生やすお話.pdf
Search
yagi2
August 03, 2018
0
440
おうちにAPIを生やすお話.pdf
Kyoto.LT 第21回
https://kyotolt.connpass.com/event/93043/
yagi2
August 03, 2018
Tweet
Share
More Decks by yagi2
See All by yagi2
Kotlin DSLを理解してみる / Understanding Kotlin DSL
yagi2
4
2.4k
略解 The Navigation Architecture Component
yagi2
1
200
10分で完全に(雰囲気を)理解するマテリアルデザイン
yagi2
1
400
KtorでおうちAPIをリプレースした話
yagi2
1
990
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Statistics for Hackers
jakevdp
796
220k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Writing Fast Ruby
sferik
627
61k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Side Projects
sachag
452
42k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Building Applications with DynamoDB
mza
90
6.1k
Practical Orchestrator
shlominoach
186
10k
Transcript
͓͏ͪʹAPIΛੜ͓͢ 2018/08/03 Kyoto.LT #21 γʔαʔגࣜձࣾ Itsuki AOYAGI (@yagi2)
ࣗݾհ • ੨༄ थʢItsuki AOYAGI) • ͗ʹ͍ʢ@yagi2, @yaginierʣ • γʔαʔגࣜձࣾ
• AndroidΞϓϦΤϯδχΞ • ͓͏͕ͪେ͖
ࣗݾհ • ੨༄ थʢItsuki AOYAGI) • ͗ʹ͍ʢ@yagi2, @yaginierʣ • γʔαʔגࣜձࣾ
• AndroidΞϓϦΤϯδχΞ • ͓͏͕ͪେ͖
࠷ۙ؆୯ʹ͓͏ͪͱITΛܨ͛ΒΕΔ ɾGoogle Home ɾAmazon Alexa ɾHomePod ……
࠷ۙ؆୯ʹ͓͏ͪͱITΛܨ͛ΒΕΔ ɾGoogle Home ɾAmazon Alexa ɾHomePod …… ͕ɺҙ֎ͱͰ͖Δ͜ͱݶΒΕ͍ͯΔ……
σϑΥϧτͰແ͍ػೳΛΔʹ • ࣗͰΞΫγϣϯεΩϧΛ͏ • ൺֱత؆୯ʹՃͰ͖Δ͕ॊೈੑऑ͍ • IFTTTΛͬͯWebhook͍ͯ͠Ζ͍ΖΔ • Webhook͢ΔͷͰՄೳੑແݶେ͕ͩ༻ҙ͢Δͷଟ͍
σϑΥϧτͰແ͍ػೳΛΔʹ • ࣗͰΞΫγϣϯεΩϧΛ͏ • ൺֱత؆୯ʹՃͰ͖Δ͕ॊೈੑऑ͍ • IFTTTΛͬͯWebhook͍ͯ͠Ζ͍ΖΔ • Webhook͢ΔͷͰՄೳੑແݶେ͕ͩ༻ҙ͢Δͷଟ͍
͓͏ͪAPI
APIʢAPlication Interfaceʣ ͬͯΔਓ
APIͬͨ͜ͱ͋Δਓ
API࡞ͬͨ͜ͱ͋Δਓ
ࣗʹAPIαʔό͕͋Δਓ
͓͏ͪAPI • ࣗͷ͋Ε͜ΕΛHTTP(S)ܦ༝Ͱ͋Ε͜ΕͰ͖ ΔΑ͏ʹ͢Δͭ • ͓͏ͪͷঢ়ଶʢGETʣ • Ոిͷૢ࡞ʢPOSTʣ
ԿΛ࣮ݱ͍͔ͨ͠ • PCͷిݯΛೖΕ͍ͨʢWake on LANʣ • ΠϯλʔωοτϥδΦͷૢ࡞Λ͍ͨ͠ • ΤΞίϯͷՈిͷૢ࡞Λ͍ͨ͠ •
෦ͷԹͱ͔औΓ͍ͨ
ࠓճेീ൪ͷKotlinͰAPIΛ࡞Δ
KotlinͰͷAPI • બࢶ͍Ζ͍Ζ͋ΔʢͳʹͤJavaͷࢿ࢈͕͑ΔͷͰʣ • SpringBootવΓ…… • KotlinͱSpring BootΛͬͯαΫοͱୟ͚ΔAPIΛ࡞Δ • ͔͠͠KotlinͰΓ͍ͨ
• ͷͰKtorͱ͍͏ϑϨʔϜϫʔΫΛ͏
Ktor • ΊͬͪΌബ͍WebΞϓϦͷϑϨʔϜϫʔΫ ( https://github.com/ktorio/ktor ) • ඇಉظॲཧ͍͍ײ͡ʹͳ͍ͬͯΔ • KotlinͷCoroutinesͱ͍͏ػೳ
fun main(args: Array<String>) { embeddedServer(Netty, 8080) { routing { get("/") { call.respondText("Hello, world!", ContentType.Text.Html) } } }.start(wait = true) }
KtorͰͷAPI • https://github.com/yagi2/Kaya • Private Repository • ͱ͍࣮͑ࠓճͷຊ࣭తͳ෦Ͱͳ͍ͷͰলུ • KtorͰ͓͏ͪAPIΛϦϓϨʔεͨ͠
- Speaker Deck
ͯ͞APIͨ͠
ͳͥAPIΛ࡞ͬͨͷ͔ • Application Interface • ͭ·ΓAPIΛ༻ҙ͢Ε͍ΖΜͳͱ͜Ζ͔Β͑Δ • Web,εϚϗ,HomeܥσόΠε
ࠓճͷߏ • API • Kotlin+KtorΛRaspberryPi্Ͱಈ͔͢ʢࣗʣ • ΫϥΠΞϯτ • WebϒϥβʢૉHTMLʣ •
AndroidΞϓϦʢेീ൪ʣ • Google Home miniʢࣗʣ ※DDNSΛͬͯࣗ֎͔ΒAPIʹΞΫηεՄೳ
ྑ͍ • ͓͏ͪͷ͋Ε͜Εʹର͢ΔೖྗͷΠϯλʔϑΣʔε͕૿͑ͨ • εϚϗࣾձͳݱʹͽͬͨΓ • ೖྗΠϯλʔϑΣʔε͕ଟ͍ʹӽͨ͜͠ͱͳ͍࠷ߴ • ͳΜͳΒcurlͰՈిૢ࡞Ͱ͖Δ •
͓͏ͪͷ͋Ε͜ΕΛҰݩཧͰ͖Δͷྑ͍ • APIΛ࡞Δೳྗؒҧ͍ͳ্͕͘Δ
ྑ͘ͳ͍ • ͳΜ͔RasPi͕ࢮΜͩΓϓϩηε͕ࢮΜͩΓ͢Δ • MackerelͰࢮ׆ࢹ͍ͯ͠Δ • ঢ়ଶΛRasPiʹͨͤ͗͢Δͱࠓਖ਼نͷϦϞίϯ͔Βૢ࡞ Λͨ͘͠ͳ͘ͳΔ • APIΛհ͞ͳ͍ͷͰঢ়ଶཧ͕໘ʹͳΔ
ࠓޙΓ͍ͨ͜ͱ • ࠓRESTfulAPIͳͷͰGraphQLʹͨ͠Γ͍ͨ͠ɻ • WebUI࡞Γ͍ͨ • Թͱ͔֎෦ͷՈిૢ࡞ɺऔಘʹ͍ΖΜͳόΠφϦ͕ґଘͯ͠ ΔͷͰDockerͱ͔ͰυΧυΧ͍ͨ͠
࣮ࡍʹى͖ͨා͍
ͦͷ̍ ͳΜ͔͍Ζ͍Ζઢͯͨ͠ΒLED͕ ম͖͖Εͨ
ిؾͷษڧΛ͔ͬ͠Γ͠·͠ΐ͏
ͦͷ̎ ͳΜ͔উखʹPC͕͍ͨΓϥδΦ ͕͍ͨΓ͢Δոحݱ
https://twitter.com/rombulow/status/990684453734203392
Կ͕ى͖ͨͷ͔ • GETͰτάϧͳϦΫΤετΛ࣮͢Δ • ςϨϏʹGET → ిݯONͳΒOFF OFFͳΒON • Chromeͷ৽نλϒ։͍ͨͱ͖ͷ࠷ۙ։͍ͨϖʔδʹAPI
͏ϖʔδ͕͋ΔͱGET͕ϦΫΤετ͞ΕΔ
HTTPΛཧղͤΑ HTTPͷGETႈʹ͢Δ͖Ͱ ͋ͬͯɺ෭࡞༻ΛͨͤΔͱ Googleͱ͔ͷBOTͱ͔ͷΫϩʔϥ ͕͓Ոʹ৵ೖͯ͘͠Δ
ͱ͍͑ΊͪΌΜ͜ศརͩ͠ ݱGoogle Homeͱ͔͋ΔͷͰ ΈΜͳΖ͏ ͓ΘΓ