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

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

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

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

Shunsuke Tadokoro

January 31, 2017
Tweet

More Decks by Shunsuke Tadokoro

Other Decks in Technology

Transcript

  1. ా ॴ  ॣ ༎  ! 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
  2. ͓͸ͳ͢͠Δ͜ͱ w ͜ͱͷൃ୺ w ͭͬͨ͘)551αʔόʔʹ͍ͭͯ w )551αʔόʔʁ w ϦΫΤετ͔ΒϨεϙϯε·Ͱ w

    ·ͱΊ w )551αʔόʔΛͭͬͯ͘ΈͯΑ͔ͬͨ͜ͱ w $MPKVSFʹೖ໳ͯ͠Α͔ͬͨ͜ͱ
  3. +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(); ... ϦΫΤετ͝ͱʹιέοτੜ੒
  4. $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͢Δ
  5. $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; จࣈྻͷ݁߹