Created
November 24, 2010 14:35
-
-
Save kiy0taka/713740 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #33 BBS | |
// Usage: | |
// groovy -l 8080 BBS.groovy | |
// Open 'http://localhost:8080' | |
@Grapes([@Grab('com.h2database:h2:1.2.144'), @GrabConfig(systemClassLoader=true)]) | |
import groovy.xml.MarkupBuilder | |
import groovy.sql.Sql | |
sql = Sql.newInstance('jdbc:h2:.bbs/db', 'sa', '', 'org.h2.Driver') | |
sql.execute('create table if not exists bbs (id INT AUTO_INCREMENT, text VARCHAR)') | |
path = line.split(' ')[1] | |
if ((matcher = (path =~ /\/(.*?)\?(.*)/))) { | |
(path, query) = matcher[0][1..-1] | |
params = URLDecoder.decode(query, 'UTF-8').split('&').inject([:]) { r, p -> | |
def m = (p =~ /(.*?)=(.*)/)[0] | |
r[m[1]] = m[2]; r | |
} | |
} | |
if (path == 'send') { | |
sql.executeUpdate("insert into bbs (text) values (?)", params.t) | |
println 'HTTP/1.0 301 Moved Permanently\nLocation: /\n' | |
} else { | |
println 'HTTP/1.0 200 OK\n' | |
new MarkupBuilder(out).html { | |
head { | |
meta 'http-equiv':'Content-Type', content:'text/html; charset=UTF-8' | |
} | |
body { | |
form (action:'/send', method:'get') { | |
span 'Status : ' | |
input type:'text', name:'t' | |
input(value:'Send', type:'submit') | |
} | |
hr() | |
sql.eachRow('select * from bbs order by id desc') { | |
div it.text | |
hr() | |
} | |
} | |
} | |
} | |
'success' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment