clojure 스터디 luminus routing
TRANSCRIPT
Routing클로저 스터디 2015-07-15한철희
自己紹介• Java/Web, iOS Programmer• 6502 Assembly, FORTH, Haskell…• and Clojure !
Agenda
• Routing 이란 무엇인가• Luminus 의 Routing• HTTP Request 구조 ( 대충 )• Routing 예제 : URL 매칭패턴• 도움될 링크
Routing: 물류센터
Luminus 의 Routing
• Ring : Web Application Library• Compojure : a small routing library for Ring• HTTP method & URL 패턴 매칭 기반으로 동작
HTTP Request 구조• method : GET, POST, HEAD, PUT, DELETE…• URL : host, path, query• http://clojure.or.kr/wiki/doku.php?id=lecture:cl
ojure: 시작• clojure.or.kr -> host• /wiki/doku.php -> path• id=lecture:clojure: 시작 -> query
Routing 예제 : query
• (GET “/terminator” [name message] (str name " said, \"" message "\""))
• http://localhost:3000/terminator?name=T-800&message=Hasta%20la%20vista,%20baby.
Routing 예제 : path
• (GET "/quotes/:words/:author" [words author] (str "\"" words "\" <br/> - " author))
• http://localhost:3000/quotes/I'll%20be%20back./T-800
Routing 예제 : path 2
• (GET ["/file/:name.:ext" :name #".*", :ext #"(?i)jpe?g"] [name ext] (str "JPEG File: " name "." ext))
• (GET ["/file/:name.:ext" :name #".*", :ext #"(?i)png"] [name ext] (str "PNG File: " name "." ext))
• http://localhost:3000/file/hello.JPEG • http://localhost:3000/file/hello.png
Routing 예제 : 아몰랑
• (compojure.route/not-found "<h1>Page Not Found</h1>”)
• http://localhost:3000/who/am/i/and/where/am/i
리턴 밸류 : Response
• 간단하게는 그냥 String “ 만” 리턴한다 : text/html, utf-8 로 인식
• 응답코드 , Content-Type 을 지정하려면 map 을 리턴한다• (GET "/request-as-json" request
{:status 200 :headers {"Content-Type" "application/json"} :body (to-json request)} )
링크
• request , response 명세 • https://github.com/mmcgrana/ring/blob/master/SPEC• Compojure 는 다음 사이트 참고 • https://github.com/weavejester/compojure/wiki• 잠깐 봤으나 , 별로 참고는 안될 듯한 예제 . • https://github.com/weavejester/compojure-example