Upgrade to Pro — share decks privately, control downloads, hide ads and more …

rails new --api してからやったこと 〜2017年・夏〜 / EC Tech MTG 3

rails new --api してからやったこと 〜2017年・夏〜 / EC Tech MTG 3

EC Tech MTG #3(GMOペパボEC事業部の社内勉強会)のための資料です

Kōhei Yamamoto

August 02, 2017
Tweet

More Decks by Kōhei Yamamoto

Other Decks in Technology

Transcript

  1. ʙ೥ɾՆʙ
    !LZNNU(.01FQBCP *OD
    &$5FDI.5(
    rails new --api͔ͯ͠Β
    ΍ͬͨ͜ͱ

    View full-size slide

  2. ࿩͢͜ͱ
    w3BJMTͰ"1*αʔόΛ࡞Δʹ͋ͨͬͯɺ։ൃੜ࢈ੑΛ্͛ΔͨΊʹ

    ΍ͬͨ͜ͱΛ঺հ͠·͢
    wΰʔϧ
    w"1*ͳͲ։ൃ࣌ʹҰൠʹߟ͑ΔͱΑͦ͞͏ͳ͜ͱ͕ΒΛ஌ͬͯ΋Β͏
    wຊࢿྉ͸3BJMTΛྫʹ͋͛ͯઆ໌͠·͕͢ߟ͔͑ͨ͸Ԡ༻Ͱ͖ͦ͏

    View full-size slide

  3. എܠ
    w+40/Λฦ͢8FC"1*αʔόΛ৽نʹॻ͘͜ͱʹͳͬͨ
    wϑϩϯτΤϯυͷ4JOHMF1BHF"QQMJDBUJPO 41"
    ͕࢖͏
    wΏ͘Ώ͘͸ެ։"1*ͱ͍ͨ͠
    w3BJMTͰ։ൃͯ͠)FSPLVͷ্Ͱಈ͔͢
    •$ rails new --api -d postgresql
    ͍͍ػձͳͷͰ։ൃָ͕ʹͳΔ؀ڥΛ࡞Γ͍ͨ
    ࠓޙ"1*ͱݺͼ·͢

    View full-size slide

  4. ΍ͬͨ͜ͱҰཡ
    w%PDLFSͰ։ൃ؀ڥ࡞੒
    wελϒαʔόͷੜ੒
    wίʔσΟϯάن໿ͷࣗಈνΣοΫ

    View full-size slide

  5. %PDLFSͰ։ൃ؀ڥ࡞੒

    View full-size slide

  6. %PDLFSͰ։ൃ؀ڥ࡞੒ʛ8IZ
    wݸਓ1$ͷ؀ڥʹΑͬͯ͸ɺ։ൃ؀ڥͷηοτΞοϓ͕ͳ͔ͳ͔͏·͘
    ͍͔ͳ͍͜ͱ͕͋ͬͨ
    wྫɿOBUJWFFYUFOTJPOΛ࢖͏HFNΛσβΠφ͞Μͷ1$΁

    Πϯετʔϧ͠Α͏ͱࣦͯ͠ഊ͠ଓ͚Δ
    wϓϩμΫτ։ൃͰ͸σβΠφͱΤϯδχΞͷ྆ํ͕։ൃ؀ڥΛ࢖͏
    w͔ΜͨΜʹηοτΞοϓͰ͖ɺ͏·͘ಈ͔ͳ͍ͱ͖ͷݪҼ੾Γ෼͚͕
    ָͳ։ൃ؀ڥʹ͔ͨͬͨ͠

    View full-size slide

  7. Dockerfile
    FROM ruby:2.4.1
    ENV APP_HOME /usr/src/app
    RUN mkdir -p $APP_HOME
    WORKDIR $APP_HOME
    COPY Gemfile \
    Gemfile.lock \
    $APP_HOME/
    ENV BUNDLE_GEMFILE=$APP_HOME/Gemfile \
    BUNDLE_JOBS=4
    "1*Λ্ཱͪ͛ΔͨΊͷ؀ڥΛ%PDLFS

    Πϝʔδͱͯ͠࡞੒͢Δ
    %PDLFS)VC͔Βऔಘͨ͠3VCZͷެࣜ

    ΠϝʔδΛ΋ͱʹɺΠϝʔδ಺ʹ3BJMTͷ

    ΞϓϦΛઃஔ͢ΔσΟϨΫτϦΛ४උ

    View full-size slide

  8. docker-compose.yml
    version: '3'
    services:
    db:
    image: postgres:9.6.3
    app: &app_base
    build: . # build a container from Dockerfile
    command: bin/rails s -b '0.0.0.0'
    depends_on:
    - db
    spring:
    <<: *app_base
    command: bin/spring server
    %# 3BJMT 4QSJOHͦΕͧΕͷ

    ίϯςφΛ্ཱͪ͛ΔͨΊͷઃ
    ఆϑΝΠϧʢࠨਤ͸ൈਮʣ
    app͸͖͞΄Ͳͷ
    DockerfileΛ΋ͱʹͨ͠3BJMT
    Λಈ͔͢ίϯςφ
    db্ཱ͕͕ͪͬͨ͋ͱʹapp
    Λ্ཱͪ͛Δ
    db͸1PTUHSF42-ͷެࣜ

    ΠϝʔδΛͦͷ··ར༻ͨ͠

    ίϯςφ

    View full-size slide

  9. "1*ͷηοτΞοϓ
    $ git clone [email protected]:foo/bar.git && cd ./bar
    $ docker-compose run --rm app bundle install
    $ docker-compose run --rm app bin/rake db:setup
    $ docker-compose up # start db, app, spring
    εςοϓͰηοτΞοϓͰ͖Δ

    View full-size slide

  10. bin/setup
    chdir APP_ROOT do
    puts '== Installing dependencies =='
    system! 'docker-compose run --rm app bundle install'
    puts "\n== Preparing database =="
    system! 'docker-compose run --rm app bin/rake db:setup'
    puts "\n== Removing old logs and tempfiles =="
    system! 'docker-compose run --rm app \
    bin/rails log:clear tmp:clear'
    puts "\n== Restarting application server =="
    system! 'docker-compose up'
    end
    3BJMTͰೖͬͨΞϓϦͷॳظઃఆ
    ΛࣗಈԽ͢ΔͨΊͷεΫϦϓτ
    !LFODIBO͘Μ͞Μ͔Βbin/setup
    Ͱઌड़ͨ͠ηοτΞοϓΛ͢΂࣮ͯߦ
    ͢ΔͨΊͷQVMMSFRVFTU͕ʂʂ
    ʢࠨਤ͸ൈਮʣ
    !POL͞ΜͷࢿྉͰ঺հ͞Ε͍ͯͨํ๏
    IUUQTXXXTMJEFTIBSFOFUUBLBGVNJPOBLB
    SBJMT

    View full-size slide

  11. "1*ͷηοτΞοϓɾվ
    $ git clone [email protected]:foo/bar.git && cd ./bar
    $ bin/setup
    εςοϓʹͳͬͨ

    View full-size slide

  12. ελϒαʔόͷੜ੒

    View full-size slide

  13. ελϒαʔόΛੜ੒͢Δʛ8IZ
    wόοΫΤϯυ൝ "1*
    ͱϑϩϯτΤϯυ൝ 41"
    Ͱ෼͔Εͯ։ൃ

    ͍ͯ͠Δ
    w"1*͕׬੒͠ͳ͍ͱ41"͸࡞Γʹ͍͘
    w͔͠͠ฒߦͯ͠։ൃ͍ͨ͠
    wαϯϓϧͷϨεϙϯεΛฦ͢ελϒαʔό͕΄͍͠

    View full-size slide

  14. 0QFO"1*
    paths:
    /products/{productId}:
    get:
    operationId: getProduct
    parameters:
    - in: path
    name: id
    type: integer
    responses:
    '200':
    schema:
    type: object
    properties:
    product:
    type: object
    properties:
    name:
    type: string
    example: ͓ࡼ
    Χϥʔϛʔγϣοϓ"1*Ͱ΋಺෦తʹಋೖࡁΈ
    ͷ0QFO"1*Ͱ"1*࢓༷Λઌʹॻ͘
    ʢࠨਤ͸ൈਮʣ
    :".-Ͱॻ͍ͨ0QFO"1*ͰͷఆٛΛZBNM@SFG@SFTPMWFSΛ
    ࢖ͬͯ0QFO"1*ͱͯ͠WBMJEͳswagger.jsonʹม׵͠ɺ
    Ͳ͔͜ʹग़ྗ͓ͯ͘͠
    IUUQTHJUIVCDPN+PFOPIZBNM@SFG@SFTPMWFS

    View full-size slide

  15. 4XBHHFS$PEFHFO
    $ docker pull swaggerapi/swagger-codegen-cli
    $ docker run -v ${PWD}:/local \
    swaggerapi/swagger-codegen-cli \
    generate -l nodejs-server -i /path/to/swagger.json \
    -o /local/stub_app
    4XBHHFS$PEFHFO W
    Ͱ͸ɺఆٛͨ͠αϯϓϧϨεϙϯεΛฦͤΔͷ͸
    /PEF 4QSJOH.7$ͳͲʹݶΒΕ͍ͯΔͷͰɺࠓճ͸nodejs-serverΛࢦఆ

    View full-size slide

  16. ελϒαʔόͷ্ཱͪ͛
    ʢNodeͷ؀ڥ͸DockerͳͲͰ࡞͓ͬͯ͘ʣ
    $ npm start
    ʢҎԼ͸ผλʔϛφϧͰʣ
    $ curl http://localhost:3000/products/1
    {
    "product": {
    "name": "͓ࡼ"
    }
    }
    ޷͖ͳϨεϙϯεฦ͠์୊

    View full-size slide

  17. ࢀߟʛ"1*ఆٛͷ։ൃϑϩʔ
    w0QFO"1*Ͱ"1*ఆٛΛ௥Ճ͢ΔQVMMSFRVFTUΛ࡞Δ
    w4XBHHFS&EJUPSΛ࢖͏ͱTZOUBYDIFDL΋૸ͬͯศར
    wॻ͖࣍ୈɺσεΫपลͰ։ൃਞΈΜͳ͕ू·ͬͯϨϏϡʔ
    wϑϩϯτΤϯυʗόοΫΤϯυ྆ํͷࢹ఺ͰϨϏϡʔͰ͖Δ
    w0,Ͱ͋Ε͹NBTUFS΁Ϛʔδ
    w͜ͷ࣌఺ͰελϒαʔόͰ͸ͦͷ"1*͕࢖͑ΔΑ͏ʹͳΔ

    View full-size slide

  18. ίʔσΟϯάن໿ͷࣗಈνΣοΫ

    View full-size slide

  19. ίʔσΟϯάن໿ͷࣗಈνΣοΫʛ8IZ
    wίʔυϨϏϡʔͰ͸ઃܭ΍ϩδοΫͷϨϏϡʔʹूத͍ͨ͠ͷͰɺ

    ίʔσΟϯάελΠϧΛܾΊͯɺࣗಈͰ௚ͤΔΑ͏ʹ͍ͨ͠
    wͨͩ͠ɺ3VCZʹ͸1)1ͷ143 ΍1ZUIPOͷ1&1΄Ͳཱ֬ͨ͠

    ίʔσΟϯάن໿ඪ४͸ͳ͍

    View full-size slide

  20. ίʔσΟϯάن໿ʢϧʔϧʣͷઃܭ
    w3VCP$PQͰνΣοΫ͢Δ
    wϧʔϧͷݫີ͞ΛܾΊΔ
    wࣾ಺طଘϓϩδΣΫτͷϧʔϧ΍!POL͞ΜͷهࣄΛࢀߟʹ

    ͠ͳ͕Βɺ؇Ίͷ΋ͷ͔Β࢝ΊΔ͜ͱʹͨ͠
    w!ITCU͞ΜͷΞυόΠε΋͋ͬͯɺؾܰʹ.rubocop.ymlʹରͯ͠
    QVMMSFRVFTUΛग़ͯ͠΋Β͏ํ਑ʹͨ͠
    IUUQCMPHPOLOJOKBSVCPDPQHFUUJOHTUBSUFE

    View full-size slide

  21. 3VCP$PQͷϧʔϧྫ
    w3VCZJTUͳΒ΄΅͜͏ॻͩ͘Ζ͏ͱ͍͏΋ͷ͚ͩΛϧʔϧͱͯ͠

    ࢖͏
    w͓અհͳ΋ͷ΍޷Έ͕Θ͔Εͦ͏ͳ΋ͷ͸ͻͱ·ͣ࢖Θͳ͍
    w࢖͏ϧʔϧͷྫ
    wෆཁͳۭߦʗ൒ۭ֯നΛফ͢
    wίϩϯʗΧϯϚͷ͏͠ΖɺதΧοίͷ಺ଆͳͲ΁൒ۭ֯നΛೖΕΔ
    wҾ਺ͳ͠ϝιουͷݺͼग़࣌͠͸ΧοίΛ͚ͭͳ͍

    View full-size slide

  22. 3VCP$PQͷ࣮ߦ
    wRuboCop::CLI#runΛ࢖ͬͯ34QFDͷࣗಈςετͷͳ͔ͰνΣοΫ
    ͍ͯ͠Δ
    wࣗಈमਖ਼·Ͱ͸͍ͯ͠ͳ͍
    w3VCZͷίʔυ͢΂ͯΛόονͰνΣοΫ͢ΔͷͰ௕͘ͳΔ
    w;ͩΜ͸QSFDPNNJUIPPLͰνΣοΫͭͭ͠ɺNBTUFSʹରͯ͠
    OJHIUMZDIFDL͢Δͱ͔

    View full-size slide

  23. ·ͱΊ
    w։ൃ؀ڥ͸୭ͷ1$্Ͱ΋ಉ͡؀ڥͰಈ͘Α͏ʹ͢Δͱศར
    w։ൃ؀ڥ͸Ͱ͖Δ͚ͩগͳ͍εςοϓͰηοτΞοϓͰ͖Δͱศར
    wελϒαʔό࡞੒ͷ࢓૊ΈΛ࡞͓ͬͯ͘ͱϑϩϯτΤϯυʗόοΫΤ
    ϯυͰฒྻʹ։ൃͰ͖ͯศར
    w͞Βʹ"1*ఆٛΛνʔϜ಺ͰϨϏϡʔ͢Δػձ͕૿͑ΔͷͰศར
    wࣗಈͰίʔσΟϯάن໿ͷνΣοΫ͕૸ΔΑ͏ʹ͢Δͱศར

    View full-size slide

  24. Ҿ͖ଓ͖΍͍ͬͯ͘͜ͱ
    w"1*࢓༷ͱ࣮૷ͷ͍͋ͩͷڍಈͷဃ཭Λ๷͙
    w0QFO"1*34QFD"QJWPSF
    wػೳʗύϑΥʔϚϯεͷܧଓతվળͷޮ཰Խ
    w5BDIJLPNBHFNEJ⒎ͰHFNͷܧଓతΞοϓσʔτ
    w/ͷࣗಈݕग़
    w$*ͷ଎౓ҡ࣋

    View full-size slide