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

Clojureに入門してHTTPサーバーをつくってみた話

 Clojureに入門してHTTPサーバーをつくってみた話

第十八回 渋谷JavaのLT資料です。

9aba2147bb6e43333fcc42e2afc570f2?s=128

Shunsuke Tadokoro

January 31, 2017
Tweet

Transcript

  1. $MPKVSFʹೖ໳ͯ͠ )551αʔόʔΛͭͬͯ͘Έͨ ौ୩+BWB !UPEPLS ָ͘͠

  2. ా ॴ  ॣ ༎  ! U P E

    P L S  w ג ࣜ ձ ࣾ Ϗ ζ Ϧ ʔ ν  ελ ϯόΠ ࣄ ۀ ෦  
 α ʔ ν ί Ξ ν ʔ Ϝ  w 4 D B M B M J B O  w & N P K J O F F S
  3. 4DBMBº&NPKJͳϥΠϒϥϦΛͭ͘Γ·ͨ͠

  4. ΑΖ͓͘͠ئ͍͠·͢

  5. ͓͸ͳ͢͠Δ͜ͱ w ͜ͱͷൃ୺ w ͭͬͨ͘)551αʔόʔʹ͍ͭͯ w )551αʔόʔʁ w ϦΫΤετ͔ΒϨεϙϯε·Ͱ w

    ·ͱΊ w )551αʔόʔΛͭͬͯ͘ΈͯΑ͔ͬͨ͜ͱ w $MPKVSFʹೖ໳ͯ͠Α͔ͬͨ͜ͱ
  6. 
 ͜ͱͷൃ୺

  7. ʮ)551αʔόʔΛ࡞Δձʯ w ࣾ಺ͷ༗ࢤʹΑΔϥϯνษڧձ w ʮ8FCΞϓϦͭ͘Γ͍ͨʂʯ
 ʮͲͷϨϕϧͰͭ͘Δͷʁʯʮαʔόʔ͔Βͭ͘Δͷ͔ʁʯ w 2JJUBɺ4UBDL0WFSqPXFUD͸ݟͪΌͩΊ w ࢖͏ݴޠͷυΩϡϝϯτ΍3'$ɺଞݴޠͰͷ࣮૷͸ݟ͍͍ͯ

    w ʮ5̋,*0ܕΤϯδχΞཆ੒ॴʯ
  8. ཱͪ͸͔ͩΔ͍ͭ͘΋ͷน w ιέοτͬͯͳΜͰ͔͢ʁ
 ˠάάͬͯ΋ిٿ͔͠ग़ͯ͜ͳ͍ w ը૾͕දࣔͰ͖ͳ͍
 ˠόΠφϦͳΜ͔ͩΒ0VUQVU4USFBN8SJUFS࢖ͬͪΌμϝ w NVMUJQBSUGPSNEBUB
 ˠCPVOEBSZʢσʔλͷ۠੾Γจࣈྻʣ͸ΫϥΠΞϯτ͕ܾΊΔ

  9. 4DBMB൛4JNQMF)5514FSWFS

  10. ࠓ೥ͷ৽ଔ΋ࢀՃͯ͠
 γʔζϯ΁

  11. ࣗ෼΋࠶౓$MPKVSFͰͭ͘Δ͜ͱʹ w ϙʔϧɾάϨΞϜʹ᱐͞Εͨ w ʮ&NBDT࢖ͬͯΔͷʹ-JTQॻ͍ͨ͜ͱͳ͍ΜͰ͔͢ʁʯͱઞΒΕͨ w ΞϯαΠΫϩϖσΟΞ͕ͳΜ͔͔ͩͬͨ͢͝

  12. IUUQBOTBJLVSPQFEJBPSHXJLJ-*41

  13. IUUQBOTBJLVSPQFEJBPSHXJLJ-*41

  14. IUUQBOTBJLVSPQFEJBPSHXJLJ-*41

  15. 
 ͭͬͨ͘)551αʔόʔʹ͍ͭͯ

  16. None
  17. )551αʔόʔʁ w MPDBMIPTUͷ೚ҙͷϙʔτͰ଴ػ w 5$1઀ଓɺ)551ϦΫΤετΛड͚औΔ w )5.-΍ը૾ͳͲΛ)551Ϩεϙϯεͱͯ͠ฦ͢

  18. ϦΫΤετ͔ΒϨεϙϯε·Ͱ w DPSFDMK w SFRVFTUDMK w SFTQPOTFDMK

  19. DPSFDMK w ΞϓϦέʔγϣϯͷΤϯτϦʔϙΠϯτ w ೚ҙͷϙʔτͰ઀ଓΛ଴ͪड͚ɺϦΫΤετ͝ͱʹιέοτੜ੒ w ιέοτੜ੒Ҏ߱ͷॲཧ͸ผεϨουͰ࣮ߦ

  20. +BWB $MPKVSF (defn -main [] (let [address (InetSocketAddress. 3000) server-socket

    (ServerSocket.)] (.bind server-socket address (int backlog)) (while true (let [socket (.accept server-socket)] ... class SimpleHttpServer { public static void main(String[] args) { InetSocketAddress address = new InetSocketAddress(3000); ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(address); while (true) { Socket socket = serverSocket.accept(); ... ϦΫΤετ͝ͱʹιέοτੜ੒
  21. SFRVFTUDMK w ιέοτͷ*OQVU4USFBNΛSFBE͠ɺ
 )551ϦΫΤετͱͯ͠ύʔε w ϦΫΤετ͸ͨͩͷ.BQͱͯ͠දݱ

  22. $MPKVSF (let [stream (FileInputStream. file)] (take-while #(not= -1 %) (repeatedly

    #(.read readable)))) +BWB InputStream stream = new FileInputStream(file); try { int b = stream.read(); while(b != -1){ // Ωϟετͯ͠഑ྻʹ٧ΊΔͳͲ } } catch (IOException e) {} *OQVU4USFBNΛ࠷ޙ·ͰSFBE͢Δ
  23. SFTQPOTFDMK w ϦΫΤετΛݩʹɺϑΝΠϧΛόΠτྻ΁ w ιέοτͷ0VUQVU4USFBNʹ)551Ϩεϙϯεͱͯ͠XSJUF

  24. $MPKVSF (str "HTTP/1.1 200 OK" CRLF "Content-Length: " XXX CRLF

    "Content-Type: " YYY CRLF) +BWB String resTxt = "HTTP/1.1 200 OK" + CRLF + "Content-Length: " + XXX + CRLF + "Content-Type: " + YYY + CRLF; จࣈྻͷ݁߹
  25. ·ͱΊ

  26. )551αʔόʔΛͭ͘Δޮ༻ w Ұ࣍৘ใʹ͋ͨΕΔΑ͏ʹͳΔ
 +BWBEPDɺ3'$ɺ&$."ɺFUD w Πϯλʔωοτͷ࢓૊Έ͕ݟ͑ͯ͘Δ
 ιέοτɺΧʔωϧɺ5$1ɺ)551ɺFUD
 ʮे෼ʹൃୡͨ͠ςΫϊϩδ͸ຐ๏ͱݟ෼͚͕͔ͭͳ͍ʯ w ଞͷϛυϧ΢ΣΞͷத਎ʹڵຯΛ࣋ͭ


    ࠓ೥͸%#Λ࡞ͬͯΈ͍ͨ

  27. $MPKVSFʹೖ໳ͯ͠Α͔ͬͨ͜ͱ w ؔ਺ྗͷ޲্
 ߴ֊ؔ਺΍ແݶϦετɺ஗ԆධՁ͕਎ۙʹ w εϨουϓʔϧΛҙࣝ͢ΔΑ͏ʹ
 HPͱUISFBEͬͯԿ͕ҧ͏ʁˠछྨ΍ಛੑΛؾʹ͢ΔΑ͏ʹͳͬͨ w ঢ়ଶΛͲ͏ѻ͏ʁ00WT$MPKVSFXBZ
 $MPKVSFͰ͸ʮΞΠσϯςΟςΟʯͱʮঢ়ଶʯΛ੾Γ཭͢


    ൺֱ͢Δ͜ͱͰ͓ޓ͍͕ΑΓ໌֬ʹ

  28. IUUQTBZBUPQHJUIVCJPDMPKVSFCFHJOOFS

  29. ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ