Skip to content

Instantly share code, notes, and snippets.

@kiy0taka
Created November 24, 2010 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiy0taka/713740 to your computer and use it in GitHub Desktop.
Save kiy0taka/713740 to your computer and use it in GitHub Desktop.
// 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