Gauche で Webアプリが作れないかと思い調査してみました。

いろいろな資料が見つかったので、半分以上写経してWebサーバが動作しました。

ｽｺﾞｲ！

web-server.scm

( use gauche.net ) ( use srfi-13 ) ( use rfc.uri ) ( use www.cgi ) ( use gauche.reload ) ( use gauche.collection ) ( add-load-path "./" ) ( use application ) ( define error-page "<html><body><h2>Error</h2></body></html>" ) ( define ( web-server ) ( let1 server-socket ( make-server-socket ' inet 8080 :reuse-addr? #t ) ( guard ( e ( else ( socket-close server-socket ) ( raise e ))) TODO ( while #t ( let1 client-socket ( socket-accept server-socket ) ( receive ( path params ) ( get-request ( socket-input-port client-socket )) ( reload-modified-modules ) ( put-response ( guard ( e ( else ( report-error e ) error-page )) ( render path params )) ( socket-output-port client-socket ))) ( socket-close client-socket )))))) ( define ( get-request in-port ) ( cond (( rxmatch #/^GET\s+ ( \S+ ) \s/ ( read-line in-port )) => ( lambda ( m ) ( parse-uri ( rxmatch-substring m 1 )))) ( else ( values "" ())))) ( define ( parse-uri uri ) ( receive ( host path query fragment ) ( uri-decompose-hierarchical uri ) ( values path ( cgi-parse-parameters :query-string ( or query "" ))))) ( define ( put-response content out-port ) ( display "HTTP/1.1 200 OK\r

" out-port ) ( display "Content-type: text/html; charset=utf-8\r

" out-port ) ( display #` "Content-Length: ,(string-length content)\r

" out-port ) ( display "\r

" out-port ) ( display content out-port )) ( web-server )

application.scm

( define-module application ( use srfi-13 ) ( use www.cgi ) ( use dbi ) ( use gauche.collection ) ( export render )) ( select-module application ) ( define ( render path params ) ( cond (( string= path "/edit" ) "edit" ) (( string= path "/update" ) "update" ) ( else "index" ))) ( provide "application" )



以下の２ファイルを同一ディレクトリ上において以下のコマンドで起動します。

gosh web-server.scm & curl -vvv http://localhost:8080/ index

さて、ここからどうしようかな。

■参考

https://thinkit.co.jp/article/74/5

http://d.hatena.ne.jp/SumiTomohiko/20080317/1205781109

http://www.atmarkit.co.jp/ait/articles/0902/20/news141_2.html

http://mmshr.blog.fc2.com/blog-entry-270.html

http://route477.net/d/?date=20060522