Juliaãã¯ãããžãŒã«ã€ããŠã¯åŒãç¶ãæ€èšããŸãã ä»æ¥ã¯ãWebãµãŒãã¹ãæ§ç¯ããããã«èšèšãããããã±ãŒãžã«ã€ããŠèª¬æããŸãã ãžã¥ãªã¢èšèªã®äž»ãªããããé«æ§èœã³ã³ãã¥ãŒãã£ã³ã°ã§ããããšã¯ç§å¯ã§ã¯ãããŸããã ãããã£ãŠãããªãè«ççãªã¹ãããã¯ããããã®èšç®ããªã³ããã³ãã§å®è¡ã§ããWebãµãŒãã¹ãçŽæ¥äœæããããšã§ãã ãã¡ããããããã¯ãŒã¯ç°å¢ã§éä¿¡ããæ¹æ³ã¯WebãµãŒãã¹ã ãã§ã¯ãããŸããã ãã ããçŸåšã¯åæ£ã·ã¹ãã ã§æãåºã䜿çšãããŠãããããHTTPèŠæ±ã«å¯Ÿå¿ãããµãŒãã¹ã®äœæãæ€èšããŸãã
Juliaã®è¥ãã®ããã«ã競åããããã±ãŒãžã®ã»ãããããããšã«æ³šæããŠãã ããã ãããã£ãŠãããããã©ã®ããã«ããããŠãªã䜿çšããã®ããç解ããããšããŸãã éäžã§ãåãJSON WebãµãŒãã¹ã®å®è£
ãšãããã®ãã«ããæ¯èŒããŸãã
ãžã¥ãªã¢ã®ã€ã³ãã©ã¹ãã©ã¯ãã£ã¯ããã1ã2幎ã§ç©æ¥µçã«éçºãããŠããŸãã ãããŠããã®å Žåãããã¯ããã¹ãã®çŸããå§ãŸãã«å»ãŸãããªã³ã©ã€ã³ã®ãã¬ãŒãºã§ã¯ãªãããã¹ãŠãéäžçã«å€åããŠãããšããäºå®ã«éç¹ã眮ããŠãããæ°å¹Žåã«é¢é£ããŠãããã®ã¯çŸåšå€ããªã£ãŠããŸãã ãã ããå®å®ããããã±ãŒãžã匷調衚瀺ããWebãµãŒãã¹ãå®è£
ããæ¹æ³ã«ã€ããŠã®æšå¥šäºé
ãæäŸããããšããŸãã æ確ã«ããããã«ã次ã®åœ¢åŒã®JSONããŒã¿ã§POSTãªã¯ãšã¹ããåãå
¥ããWebãµãŒãã¹ãäœæããŸãã
{ "title": "something", "body": "something" }
äœæãããµãŒãã¹ã¯RESTfulã§ã¯ãªããšæ³å®ããŠããŸãã ç§ãã¡ã®äž»ãªã¿ã¹ã¯ã¯ãã«ãŒããšãªã¯ãšã¹ããã³ãã©ãèšè¿°ããããã®ã¡ãœãããæ£ç¢ºã«æ€èšããããšã§ãã
HTTP.jlããã±ãŒãž
ãã®ããã±ãŒãžã¯ããžã¥ãªã¢ã®HTTPãããã³ã«ã®äž»èŠãªå®è£
ã§ããã次第ã«æ°ããæ©èœãå¢ããŠããŠããŸãã ãã®ããã±ãŒãžã¯ãã¯ã©ã€ã¢ã³ãHTTPèŠæ±ãå®è¡ããããã®å
žåçãªæ§é ãšæ©èœãå®è£
ããããšã«å ããŠãHTTPãµãŒããŒãäœæããããã®æ©èœãå®è£
ããŸãã åæã«ãéçºäžã®ããã±ãŒãžã«ã¯ãããã°ã©ããŒããã³ãã©ãŒãç»é²ããŠå
žåçãªãµãŒãã¹ãæ§ç¯ããã®ãéåžžã«å¿«é©ã«ããé¢æ°ãçšæãããŠããŸãã ãŸããææ°ããŒãžã§ã³ã§ã¯ãWebSocketãããã³ã«ã®ãµããŒããçµã¿èŸŒãŸããŠããŸãããã®å®è£
ã¯ã以åã¯å¥ã®ããã±ãŒãžWebSocket.jlã®äžéšãšããŠäœæãããŠããŸããã ã€ãŸããHTTP.jlã¯çŸåšãããã°ã©ããŒã®ã»ãšãã©ã®ããŒãºãæºããããšãã§ããŸãã ããã€ãã®äŸã詳ããèŠãŠã¿ãŸãããã
HTTPã¯ã©ã€ã¢ã³ã
ã¯ã©ã€ã¢ã³ãã³ãŒãã䜿çšããŠå®è£
ãéå§ããããã䜿çšããŠæäœæ§ã確èªããŸãã
HTTPããã±ãŒãžã¯ãHTTPãããã³ã«ã³ãã³ãã®ååã«äžèŽããã¡ãœãããæäŸããŸãã ãã®å Žåã get
ããã³post
ã䜿çšãget
ã ãªãã·ã§ã³ã®ååä»ãåŒæ°verbose
䜿çšãããšãåºåãããããã°æ
å ±ã®éãèšå®ã§ããŸãã ãããã£ãŠãããšãã°ã verbose=1
ã¯æ¬¡ã®ããã«ãªããŸã
GET /user/Jemand HTTP/1.1 HTTP/1.1 200 OK <= (GET /user/Jemand HTTP/1.1)
ãŸãã verbose=3
å Žåãéä¿¡ããŒã¿ãšåä¿¡ããŒã¿ã®å®å
šãªã»ããããã§ã«ååŸãããŠããŸãã
DEBUG: 2019-04-21T22:40:40.961 eb4f ïž-> "POST /resource/process HTTP/1.1\r\n" (write) DEBUG: 2019-04-21T22:40:40.961 eb4f ïž-> "Content-Type: application/json\r\n" (write) DEBUG: 2019-04-21T22:40:40.961 eb4f ïž-> "Host: 127.0.0.1\r\n" (write) DEBUG: 2019-04-21T22:40:40.961 eb4f ïž-> "Content-Length: 67\r\n" (write) DEBUG: 2019-04-21T22:40:40.961 eb4f ïž-> "\r\n" (write) DEBUG: 2019-04-21T22:40:40.961 e1c6 ïž-> "{\"title\":\"Some document\",\"body\":\"Test document with some content.\"}" (unsafe_write) DEBUG: 2019-04-21T22:40:40.963 eb4f ïž<- "HTTP/1.1 200 OK\r\n" (readuntil) DEBUG: "Content-Type: application/json\r\n" DEBUG: "Transfer-Encoding: chunked\r\n" DEBUG: "\r\n" DEBUG: 2019-04-21T22:40:40.963 eb4f ïž<- "5d\r\n" (readuntil) DEBUG: 2019-04-21T22:40:40.963 eb4f ïž<- "{\"body\":\"Test document with some content.\",\"server_mark\":\"confirmed\",\"title\":\"Some document\"}" (unsafe_read) DEBUG: 2019-04-21T22:40:40.968 eb4f ïž<- "\r\n" (readuntil) DEBUG: "0\r\n" DEBUG: 2019-04-21T22:40:40.968 eb4f ïž<- "\r\n" (readuntil)
å°æ¥ãäœãèµ·ãã£ãŠãããã«ã€ããŠæå°éã®æ
å ±ã®ã¿ãèŠãããã«ã verbose=1
ã®ã¿ã䜿çšããŸãã
ã³ãŒãã«é¢ããããã€ãã®ã³ã¡ã³ãã
doc = Document("Some document", "Test document with some content.")
以åã«Documentæ§é ïŒããã³äžå€ïŒã宣èšãããããããã©ã«ãã§ã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšã§ããŸãããã®åŒæ°ã¯ãæ§é ã®å®£èšããããã£ãŒã«ãã«å¯Ÿå¿ããŠããŸãã JSONã«å€æããã«ã¯ã JSON.jl
ããã±ãŒãžãšãã®json(doc)
ã¡ãœããã䜿çšããŸãã
ãã©ã°ã¡ã³ãã«æ³šæããŠãã ããïŒ
r = HTTP.post( "http://$(HOST):$(PORT)/resource/process", [("Content-Type" => "application/json")], json(doc); verbose=3)
JSONãæž¡ãããã Content-Type
ããããŒã§application/json
ã¿ã€ããæ瀺çã«æå®ããå¿
èŠããããŸãã ããããŒã¯ãããããŒåãšå€ã®ãã¢ãå«ãé
åïŒãã¯ãã«ã§ã¯ãªããDictã§ã¯ãªãïŒã䜿çšããŠã HTTP.post
ã¡ãœããã«æž¡ãããŸãïŒãã ããä»ã®ãã¹ãŠãšåæ§ïŒã
ãã«ã¹ãã¹ãã§ã¯ã3ã€ã®ã¯ãšãªãå®è¡ããŸãã
- ã«ãŒãã«ãŒããžã®GETãªã¯ãšã¹ãã
- /ãŠãŒã¶ãŒ/ååã®åœ¢åŒã®GETãªã¯ãšã¹ããããã§ãååã¯éä¿¡ãããååã§ãã
- JSONãªããžã§ã¯ããæž¡ãããPOSTèŠæ±/ãªãœãŒã¹/ããã»ã¹ã åãããã¥ã¡ã³ããåãåãããšãæ³å®ããŠããŸããã
server_mark
ãã£ãŒã«ããè¿œå ãããŠããŸãã
ãã®ã¯ã©ã€ã¢ã³ãã³ãŒãã䜿çšããŠããã¹ãŠã®ãµãŒããŒå®è£
ãªãã·ã§ã³ããã¹ãããŸãã
HTTPãµãŒããŒ
ã¯ã©ã€ã¢ã³ããç解ãããããµãŒããŒã®å®è£
ãéå§ããŸãã ãããããä»ã®ããã±ãŒãžã®ã€ã³ã¹ããŒã«ãå¿
èŠãšããªãåºæ¬ããŒãžã§ã³ãšããŠä¿æããããã«ã HTTP.jl
ã䜿çšããŠã®ã¿ãµãŒãã¹ãäœæããŸãã HTTP.jl
ãä»ã®ãã¹ãŠã®ããã±ãŒãžã¯HTTP.jl
䜿çšãHTTP.jl
ãã®äŸã§ã¯ã次ã®ã³ãŒãã«æ³šæããå¿
èŠããããŸãã
dump(req)
ãªããžã§ã¯ããèªèããŠãããã¹ãŠãã³ã³ãœãŒã«ã«åºåããŸãã ããŒã¿åãå€ããã¹ãŠã®ãã¹ãããããã£ãŒã«ããšãã®å€ãå«ã¿ãŸãã ãã®æ¹æ³ã¯ãã©ã€ãã©ãªã®èª¿æ»ãšãããã°ã®äž¡æ¹ã«åœ¹ç«ã¡ãŸãã
ã²ã
(m = match( r".*/user/([[:alpha:]]+)", req.target))
ãã³ãã©ãŒãç»é²ãããŠããã«ãŒãã解æããæ£èŠè¡šçŸã§ãã HTTP.jl
ããã±ãŒãžHTTP.jl
ãã«ãŒãå
ã®ãã¿ãŒã³ãèªåçã«èå¥ããæ¹æ³ãæäŸHTTP.jl
ãã
process_resource
ãã³ãã©ãŒå
ã§ããµãŒãã¹ã«ãã£ãŠåãå
¥ããããJSONã解æããŸãã
message = JSON.parse(String(req.body))
ããŒã¿ã«ã¯ã req.body
ãã£ãŒã«ããä»ããŠã¢ã¯ã»ã¹ããŸãã ããŒã¿ã¯ãã€ãé
å圢åŒã§ããããšã«æ³šæããŠãã ããã ãããã£ãŠãããããæååãšããŠäœ¿çšããã«ã¯ãæååãžã®æ瀺çãªå€æãå®è¡ãããŸãã JSON.parse
ã¡ãœããã¯ãããŒã¿ãéã·ãªã¢ã«åãããªããžã§ã¯ããæ§ç¯ããJSON.jl
ããã±ãŒãžã¡ãœããã§ãã ãã®å Žåã®ãªããžã§ã¯ãã¯é£æ³é
åïŒDictïŒã§ãããããæ°ããããŒãç°¡åã«è¿œå ã§ããŸãã ã²ã
message["server_mark"] = "confirmed"
å€ãconfirmed
ãserver_mark
ããŒãè¿œå ããŸãã
HTTP.serve(ROUTER, Sockets.localhost, 8080)
è¡ãHTTP.serve(ROUTER, Sockets.localhost, 8080)
ããµãŒãã¹HTTP.serve(ROUTER, Sockets.localhost, 8080)
ã
HTTP.jlã«åºã¥ããµãŒãã¹ã®å¶åŸ¡å¿çïŒ verbose=1
ã¯ã©ã€ã¢ã³ãã³ãŒããå®è¡ãããšãã«ååŸïŒïŒ
GET / HTTP/1.1 HTTP/1.1 200 OK <= (GET / HTTP/1.1) Hello World GET /user/Jemand HTTP/1.1 HTTP/1.1 200 OK <= (GET /user/Jemand HTTP/1.1) Hello Jemand POST /resource/process HTTP/1.1 HTTP/1.1 200 OK <= (POST /resource/process HTTP/1.1) {"body":"Test document with some content.","server_mark":"confirmed","title":"Some document"}
verbose=1
ã®ãããã°æ
å ±ã®èæ¯ã«å¯ŸããŠã Hello World
ã®è¡ãæ確ã«èŠãããšãã§ããŸãïŒ Hello World
ã Hello Jemand
ã "server_mark":"confirmed"
ã
ãµãŒãã¹ã³ãŒãã衚瀺ããåŸãèªç¶ãªçåãçããŸããHTTPã§ãã¹ãŠãéåžžã«åçŽãªå Žåããªãä»ã®ãã¹ãŠã®ããã±ãŒãžãå¿
èŠãªã®ã§ããããã ããã«ã¯éåžžã«ç°¡åãªçãããããŸãã HTTP-åçãã³ãã©ãŒãç»é²ã§ããŸããããã£ã¬ã¯ããªããéçç»åãã¡ã€ã«ãèªã¿èŸŒãåºæ¬çãªå®è£
ã§ãããå¥åã®å®è£
ãå¿
èŠã§ãã ãããã£ãŠãWebã¢ããªã±ãŒã·ã§ã³ã®äœæã«çŠç¹ãåœãŠãããã±ãŒãžãæ€èšããŸãã
Mux.jlããã±ãŒãž
ãã®ããã±ãŒãžã¯ããžã¥ãªã¢ã«å®è£
ãããWebã¢ããªã±ãŒã·ã§ã³ã®äžéå±€ãšããŠäœçœ®ä»ããããŠããŸãã ãã®å®è£
ã¯éåžžã«è»œéã§ãã äž»ãªç®çã¯ããã³ãã©ãŒãç°¡åã«èšè¿°ããæ¹æ³ãæäŸããããšã§ãã ããã¯ããããžã§ã¯ããéçºäžã§ã¯ãªããšããããšã§ã¯ãªãããã£ãããšéçºããŠãããšããããšã§ãã ãã ããåãã«ãŒããæäŸãããµãŒãã¹ã®ã³ãŒããèŠãŠãã ããã
ããã§ã¯ã page
ã¡ãœããã䜿çšããŠã«ãŒãã説æãpage
ã Webã¢ããªã±ãŒã·ã§ã³ã¯ã @app
ãã¯ãã䜿çšããŠå®£èšãããŸãã page
ã¡ãœããã®åŒæ°ã¯ãã«ãŒããšãã³ãã©ãŒã§ãã ãã³ãã©ãŒã¯ãèŠæ±ãå
¥åãšããŠåãå
¥ããé¢æ°ãšããŠæå®ããããšããã©ã ãé¢æ°ãšããŠæå®ããããšãã§ããŸãã è¿œå ã®äŸ¿å©ãªé¢æ°ã®ãã¡ã Mux.notfound()
ãæå®ãããNot found
å¿çãéä¿¡ããããã«ååšããŸãã ãããŠãã¯ã©ã€ã¢ã³ãã«éä¿¡ãããã¹ãçµæã¯ãMuxãèªåã§è¡ããããåã®äŸã§è¡ã£ãããã«HTTP.Response
ã«ããã±ãŒãžåããå¿
èŠã¯ãããŸããã ãã ããå¿ççšã®ãªããžã§ã¯ãJSON.json(message)
ã·ãªã¢ã«åãšåæ§ã«ãJSONã®è§£æãèªåã§è¡ãå¿
èŠããããŸãã
message = JSON.parse(String(req[:data])) message["server_mark"] = "confirmed" return Dict( :body => JSON.json(message), :headers => [("Content-Type" => "application/json")] )
å¿çã¯ããã£ãŒã«ã:body
:headers
æã€é£æ³é
åãšããŠéä¿¡ãããŸãã
serve(test, 8080)
ã¡ãœããã䜿çšããŠãµãŒããŒãèµ·åããã®ã¯éåæã§ãããããJuliaã®å®äºåŸ
ã¡ãæŽçãããªãã·ã§ã³ã®1ã€ã¯ã³ãŒããåŒã³åºãããšã§ãã
Base.JLOptions().isinteractive == 0 && wait()
ãã以å€ã®å ŽåããµãŒãã¹ã¯HTTP.jl
以åã®ããŒãžã§ã³ãšåãããã«HTTP.jl
ãµãŒãã¹ã®å¶åŸ¡å¿çïŒ
GET / HTTP/1.1 HTTP/1.1 200 OK <= (GET / HTTP/1.1) <h1>Hello World!</h1> GET /user/Jemand HTTP/1.1 HTTP/1.1 200 OK <= (GET /user/Jemand HTTP/1.1) <h1>Hello, Jemand!</h1> POST /resource/process HTTP/1.1 HTTP/1.1 200 OK <= (POST /resource/process HTTP/1.1) {"body":"Test document with some content.","server_mark":"confirmed","title":"Some document"}
ããã±ãŒãžBukdu.jl
ãã®ããã±ãŒãžã¯ãPhoenixãã¬ãŒã ã¯ãŒã¯ã®åœ±é¿äžã§éçºãããŸããããã®ãã¬ãŒã ã¯ãŒã¯ã¯ãElixirã«å®è£
ãããElixirã§ã®ãããžã§ã¯ã·ã§ã³ã§ã®Rubyã³ãã¥ããã£ããã®Webæ§ç¯ã¢ã€ãã¢ã®å®è£
ã§ãã ãã®ãããžã§ã¯ãã¯éåžžã«æŽ»çºã«éçºãããŠãããRESTful APIãšè»œéWebã¢ããªã±ãŒã·ã§ã³ãäœæããããã®ããŒã«ãšããŠäœçœ®ä»ããããŠããŸãã JSONã®ã·ãªã¢ã«åãšéã·ãªã¢ã«åãç°¡çŽ åããé¢æ°ããããŸãã ããã¯HTTP.jl
ããã³Mux.jl
ããMux.jl
WebãµãŒãã¹ã®å®è£
ãèŠãŠã¿ãŸãããã
æåã«æ³šæãã¹ãããšã¯ãã³ã³ãããŒã©ãŒã®ç¶æ
ãä¿åããããã®æ§é äœã®å®£èšã§ãã
struct WelcomeController <: ApplicationController conn::Conn end
ãã®å Žåãæœè±¡åApplicationController
åå«ãšããŠäœæãããå
·è±¡åã§ãã
ã³ã³ãããŒã©ãŒã®ã¡ãœããã¯ã以åã®å®è£
ãšåæ§ã®æ¹æ³ã§å®£èšãããŸãã JSONãªããžã§ã¯ãã®ãã³ãã©ãŒã«ã¯ããããªéãããããŸãã
function process_resource(c::WelcomeController) message = JSON.parse(String(c.conn.request.body)) @info message message["server_mark"] = "confirmed" render(JSON, message) end
ã芧ã®ãšããããã·ãªã¢ã©ã€ãºãJSON.parse
ã¡ãœããã䜿çšããŠJSON.parse
å®è¡JSON.parse
ãJSON.parse
ãJSON.parse
render(JSON, message)
ã¡ãœããã䜿çšããŠå¿çãã·ãªã¢ã©ã€ãºããŸãã
ã«ãŒãã®å®£èšã¯ã do...end
ãããã¯ã®äœ¿çšãå«ããã«ãŒãã¹ãã®äŒçµ±çãªã¹ã¿ã€ã«ã§è¡ãããŸãã
routes() do get("/", WelcomeController, index) get("/user/:user", WelcomeController, welcome_user, :user => String) post("/resource/process", WelcomeController, process_resource) end
ãŸããã«ãŒãã¹ãã®åŸæ¥ã®æ¹æ³ã§ã¯ãã»ã°ã¡ã³ãã¯ã«ãŒãã©ã€ã³/user/:user
宣èšãã/user/:user
ã ã€ãŸããåŒã®å€æ°éšåã§ããããã³ãã¬ãŒãã§æå®ãããååã§ã¢ã¯ã»ã¹ã§ããŸãã ã¿ã€ãSymbol
代衚ãšããŠæ§æçã«æå®ãããŠããŸãã ã¡ãªã¿ã«ãJuliaã®å Žåã Symbol
åã¯ãæ¬è³ªçã«ã¯Rubyã®å Žåãšåãããšãæå³ããŸããããã¯ãã¡ã¢ãªå
ã§åäžã®ã€ã³ã¹ã¿ã³ã¹ã§è¡šãããäžå€ã®æååã§ãã
ãããã£ãŠãå€æ°éšåã§ã«ãŒãã宣èšãããã®å€æ°éšåã®ã¿ã€ãã瀺ããåŸãå²ãåœãŠãããååã§æ¢ã«è§£æãããããŒã¿ãåç
§ã§ããŸãã èŠæ±ãåŠçããã¡ãœããã§ã¯ã c.params.user
ã®åœ¢åŒã®ããããä»ããŠãã£ãŒã«ãã«ã¢ã¯ã»ã¹ããã ãã§ãã
welcome_user(c::WelcomeController) = render(JSON, "Hello " * c.params.user)
ãµãŒãã¹ã®å¶åŸ¡å¿çïŒ
GET / HTTP/1.1 HTTP/1.1 200 OK <= (GET / HTTP/1.1) "Hello World" GET /user/Jemand HTTP/1.1 HTTP/1.1 200 OK <= (GET /user/Jemand HTTP/1.1) "Hello Jemand" POST /resource/process HTTP/1.1 HTTP/1.1 200 OK <= (POST /resource/process HTTP/1.1) {"body":"Test document with some content.","server_mark":"confirmed","title":"Some document"}
ã³ã³ãœãŒã«ãžã®ãµãŒãã¹ã®çµè«ïŒ
>./bukdu_json.jl INFO: Bukdu Listening on 127.0.0.1:8080 INFO: GET WelcomeController index 200 / INFO: GET WelcomeController welcome_user 200 /user/Jemand INFO: Dict{String,Any}("body"=>"Test document with some content.","title"=>"Some document") INFO: POST WelcomeController process_resource200 /resource/process
ããã±ãŒãžGenie.jl
MVC Webãã¬ãŒã ã¯ãŒã¯ãšããŠäœçœ®ã¥ããããéå¿çãªãããžã§ã¯ãã 圌ã®ã¢ãããŒãã§ã¯ããžã§ãã¬ãŒã¿ã«ãã£ãŠäœæããããã£ã¬ã¯ããªæ§é ãå«ãããžã¥ãªã¢ã®ãã¬ãŒã«ããéåžžã«ã¯ã£ãããšèŠããŸãã ãããžã§ã¯ãã¯éçºäžã§ãããçç±ã¯äžæã§ããããã®ããã±ãŒãžã¯Juliaããã±ãŒãžãªããžããªã«å«ãŸããŠããŸããã ã€ãŸãã次ã®ã³ãã³ãã䜿çšããŠgitãªããžããªããã®ã¿ã€ã³ã¹ããŒã«ã§ããŸãã
julia>]
Genieã§ã®ãµãŒãã¹ã®ã³ãŒãã¯æ¬¡ã®ãšããã§ãïŒãžã§ãã¬ãŒã¿ãŒã¯äœ¿çšããŸããïŒã
ããã§ã¯ã宣èšã®åœ¢åŒã«æ³šæãæãå¿
èŠããããŸãã
route("/") do "Hello World!" end
ãã®ã³ãŒãã¯ãRubyããã°ã©ããŒã«ãšã£ãŠéåžžã«ããç¥ãããŠããŸãã do...end
ãããã¯ã¯ãã³ãã©ãŒãšããŠãã«ãŒãã¯ã¡ãœããã®åŒæ°ãšããŠäœ¿çšããŸãã Juliaã®å Žåããã®ã³ãŒãã¯æ¬¡ã®åœ¢åŒã§æžãæããããããšã«æ³šæããŠãã ããã
route(req -> "Hello World!", "/")
ã€ãŸãããã³ãã©ãŒé¢æ°ãæåã«ãããã«ãŒãã2çªç®ã«ãããŸãã ããããç§ãã¡ã®å Žåã¯ãã«ããŒã¹ã¿ã€ã«ã®ãŸãŸã«ããŸãã
Genieã¯ãå®è¡çµæãHTTPå¿çã«èªåçã«ããã¯ããŸãã æå°éã®å Žåãæ£ããåã®çµæãããšãã°Stringãè¿ãã ãã§æžã¿ãŸãã è¿œå ã®ã¢ã¡ããã£ã®ãã¡ãå
¥å圢åŒã®èªåãã§ãã¯ãšãã®åæãå®è£
ãããŠããŸãã ããšãã°ãJSONã®å Žåã jsonpayload()
ã¡ãœãããåŒã³åºãã ãã§ãã
route("/resource/process", method = POST) do message = jsonpayload()
ããã§ã³ã¡ã³ãã¢ãŠããããŠããã³ãŒããã©ã°ã¡ã³ãã«æ³šæããŠãã ããã äœããã®çç±ã§å
¥å圢åŒãJSONãšããŠèªèããnothing
å Žåã jsonpayload()
ã¡ãœããã¯nothing
è¿ãnothing
ã ããã ãã®ããã«ãããããŒ[("Content-Type" => "application/json")]
HTTPã¯ã©ã€ã¢ã³ãã«è¿œå ãããããšã«æ³šæããŠãã ãããããããªããšãGenieã¯ããŒã¿ãJSONãšããŠè§£æãããããŸããã ç解ã§ããªãäœããæ¥ãå Žåã rawpayload()
ãèŠãŠãããäœã§ããããèŠããšäŸ¿å©ã§ãã ãã ããããã¯åãªããããã°æ®µéã§ãããããã³ãŒãå
ã«æ®ããŠã¯ãªããŸããã
ãŸãããã©ãŒãããmessage |> json!
çµæãè¿ãããšã«æ³šæããå¿
èŠãããmessage |> json!
ã ããã§ã¯ã json!(str)
ã¡ãœããããã€ãã©ã€ã³ã®æåŸã«é
眮ãããŠããŸãã JSON圢åŒãžã®ããŒã¿ã®ã·ãªã¢ã«åãæäŸããGenieãæ£ããContent-Type
è¿œå ããããšãä¿èšŒããŸãã ãŸããäžèšã®äŸã®ã»ãšãã©ã®å Žåãåèªã®return
ã¯åé·ã§ããããšã«æ³šæããŠãã ããã ããšãã°ãJuliaã¯Rubyãšåæ§ã«ãåžžã«æåŸã®æäœã®çµæãŸãã¯æåŸã«æå®ãããåŒã®å€ãè¿ããŸãã ã€ãŸããåèªreturn
ã¯ãªãã·ã§ã³ã§ãã
Genieã®æ©èœã¯ããã§çµããã§ã¯ãããŸããããWebãµãŒãã¹ãå®è£
ããããã«å¿
èŠãªããã§ã¯ãããŸããã
ãµãŒãã¹ã®å¶åŸ¡å¿çïŒ
GET / HTTP/1.1 HTTP/1.1 200 OK <= (GET / HTTP/1.1) Hello World! GET /user/Jemand HTTP/1.1 HTTP/1.1 200 OK <= (GET /user/Jemand HTTP/1.1) Hello Jemand POST /resource/process HTTP/1.1 HTTP/1.1 200 OK <= (POST /resource/process HTTP/1.1) {"body":"Test document with some content.","server_mark":"confirmed","title":"Some document"}
ã³ã³ãœãŒã«ãžã®ãµãŒãã¹ã®çµè«ïŒ
>./genie_json.jl [ Info: Ready! 2019-04-24 17:18:51:DEBUG:Main: Web Server starting at http://127.0.0.1:8080 2019-04-24 17:18:51:DEBUG:Main: Web Server running at http://127.0.0.1:8080 2019-04-24 17:19:21:INFO:Main: / 200 2019-04-24 17:19:21:INFO:Main: /user/Jemand 200 2019-04-24 17:19:22:INFO:Main: /resource/process 200
ããã±ãŒãžJuliaWebAPI.jl
ãã®ããã±ãŒãžã¯ãHTTP.jlããããã³ã«ãå®è£
ããã©ã€ãã©ãªã«éããªãã£ãåœæãWebã¢ããªã±ãŒã·ã§ã³ãäœæããããã®äžéå±€ãšããŠäœçœ®ä»ããããŠããŸããã ãã®ããã±ãŒãžã®äœæè
ã¯ãSwaggerä»æ§ïŒOpenAPIããã³http://editor.swagger.io/ ïŒã«åºã¥ããµãŒããŒã³ãŒããžã§ãã¬ãŒã¿ãŒãå®è£
ããŸããããããžã§ã¯ãhttps://github.com/JuliaComputing/Swagger.jlãåç
§ããŠãã ãã ããã®ãžã§ãã¬ãŒã¿ãŒã¯JuliaWebAPIã䜿çšããŸãã.jlã ãã ããJuliaWebAPI.jlã®åé¡ã¯ãPOSTèŠæ±ãéããŠãµãŒããŒã«éä¿¡ãããèŠæ±ã®æ¬æïŒJSONãªã©ïŒãåŠçããæ©èœãå®è£
ããŠããªãããšã§ãã èè
ã¯ãããŒãšå€ã®åœ¢åŒã§ãã©ã¡ãŒã¿ãæž¡ãããšã¯ãã¹ãŠã®å Žåã«é©ããŠãããšä¿¡ããŠããŸãã...ãã®ããã±ãŒãžã®å°æ¥ã¯æ確ã§ã¯ãããŸããã ãã®æ©èœã¯ãã¹ãŠãHTTP.jlãå«ãä»ã®å€ãã®ããã±ãŒãžã«æ¢ã«å®è£
ãããŠããŸãã Swagger.jlããã±ãŒãžã䜿çšããªããªããŸããã
WebSockets.jl
WebSocketãããã³ã«ã®åæå®è£
ã ãã®ããã±ãŒãžã¯ããã®ãããã³ã«ã®äž»èŠãªå®è£
ãšããŠé·ãé䜿çšãããŠããŸããããçŸåšã®ãšããããã®å®è£
ã¯HTTP.jlããã±ãŒãžã«å«ãŸããŠããŸãã WebSockets.jlããã±ãŒãžèªäœã¯HTTP.jlã䜿çšããŠæ¥ç¶ã確ç«ããŸãããçŸåšã§ã¯æ°ããéçºã§äœ¿çšãã䟡å€ã¯ãããŸããã äºææ§ã®ããã®ããã±ãŒãžãšèŠãªãå¿
èŠããããŸãã
ãããã«
ãã®ã¬ãã¥ãŒã§ã¯ãJuliaã«WebãµãŒãã¹ãå®è£
ããããŸããŸãªæ¹æ³ã瀺ããŸãã æãç°¡åã§æãæ®éçãªæ¹æ³ã¯ãHTTP.jlããã±ãŒãžãçŽæ¥äœ¿çšããããšã§ãã ãŸããBukdu.jlããã³Genie.jlããã±ãŒãžã¯éåžžã«äŸ¿å©ã§ãã å°ãªããšãããããã®éçºãç£èŠããå¿
èŠããããŸãã Mux.jlããã±ãŒãžã«é¢ããŠã¯ãHTTP.jlã®èæ¯ãããã®å©ç¹ã解æ¶ãããŠããŸãã ãããã£ãŠãå人çãªæèŠã¯ããã䜿çšããªãããšã§ãã Genie.jlã¯éåžžã«ææãªãã¬ãŒã ã¯ãŒã¯ã§ãã ãã ãã䜿çšãéå§ããåã«ãå°ãªããšãäœæè
ãå
¬åŒããã±ãŒãžãšããŠç»é²ããªãçç±ãç解ããå¿
èŠããããŸãã
äŸã®JSONãã·ãªã¢ã©ã€ãŒãŒã·ã§ã³ã³ãŒãã¯ãšã©ãŒåŠçãªãã§äœ¿çšãããŠããããšã«æ³šæããŠãã ããã Genieãé€ããã¹ãŠã®ã±ãŒã¹ã§ã解æãšã©ãŒãåŠçããããã«ã€ããŠãŠãŒã¶ãŒã«éç¥ããå¿
èŠããããŸãã HTTP.jlã®ãã®ãããªã³ãŒãã®äŸïŒ
local message = nothing local body = IOBuffer(HTTP.payload(req)) try message = JSON.parse(body) catch err @error err.msg return HTTP.Response(400, string(err.msg)) end
äžè¬ã«ããžã¥ãªã¢ã§WebãµãŒãã¹ãäœæããããã®è³éã¯ãã§ã«ååã§ãããšèšããŸãã ã€ãŸããå·çã®ããã®ãè»èŒªã®åçºæãã¯å¿
èŠãããŸããã 次ã®ã¹ãããã¯ã誰ãããããåãå
¥ããæºåãã§ããŠããå Žåããžã¥ãªã¢ãæ¢åã®ãã³ãããŒã¯ã®è² è·ã«ã©ã®ããã«èããããšãã§ããããè©äŸ¡ããããšã§ãã ãã ããåœé¢ã¯ãã®ã¬ãã¥ãŒã«ã€ããŠèª¬æããŸãã
åç
§è³æ