vweb: fix get_cookie() and redirect()
parent
faf2f9920e
commit
b2874f1200
|
@ -0,0 +1,25 @@
|
||||||
|
//import pg
|
||||||
|
|
||||||
|
struct Mod {
|
||||||
|
id int
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
nr_downloads int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_orm() {
|
||||||
|
/*
|
||||||
|
db := pg.connect('vpm', 'alex')
|
||||||
|
nr_modules := select count from db.modules
|
||||||
|
mod := select from db.modules where id = 1 limit 1
|
||||||
|
println(mod.name)
|
||||||
|
top_mods := select from db.modules where nr_downloads > 1000 order by nr_downloads desc limit 10
|
||||||
|
top_mods := db.select from modules where nr_downloads > 1000 order by nr_downloads desc limit 10
|
||||||
|
top_mods := db.select<Module>(m => m.nr_downloads > 1000).order_by(m => m.nr_downloads).desc().limit(10)
|
||||||
|
names := select name from db.modules // []string
|
||||||
|
|
||||||
|
|
||||||
|
n := db.q_int('select count(*) from modules')
|
||||||
|
println(n)
|
||||||
|
*/
|
||||||
|
}
|
|
@ -38,8 +38,7 @@ $s
|
||||||
|
|
||||||
pub fn (ctx Context) redirect(url string) {
|
pub fn (ctx Context) redirect(url string) {
|
||||||
h := ctx.headers.join('\n')
|
h := ctx.headers.join('\n')
|
||||||
ctx.conn.write('
|
ctx.conn.write('HTTP/1.1 302 Found
|
||||||
HTTP/1.1 302 Found
|
|
||||||
Location: $url
|
Location: $url
|
||||||
$h
|
$h
|
||||||
')
|
')
|
||||||
|
@ -55,9 +54,9 @@ pub fn (ctx mut Context) set_cookie(key, val string) {
|
||||||
|
|
||||||
pub fn (ctx Context) get_cookie(key string) string {
|
pub fn (ctx Context) get_cookie(key string) string {
|
||||||
for h in ctx.req.headers2 {
|
for h in ctx.req.headers2 {
|
||||||
if h.starts_with('Cookie:') {
|
if h.starts_with('Cookie:') || h.starts_with('cookie:') {
|
||||||
cookie := h.right(7)
|
cookie := h.right(7)
|
||||||
return cookie.find_between('$key=', ';')
|
return cookie.find_between(' $key=', ';')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
|
@ -88,8 +87,8 @@ $html
|
||||||
pub fn run<T>(port int) {
|
pub fn run<T>(port int) {
|
||||||
println('Running vweb app on http://localhost:$port ...')
|
println('Running vweb app on http://localhost:$port ...')
|
||||||
l := net.listen(port) or { panic('failed to listen') return }
|
l := net.listen(port) or { panic('failed to listen') return }
|
||||||
mut app := T{}
|
mut app_init := T{}
|
||||||
app.init()
|
app_init.init()
|
||||||
for {
|
for {
|
||||||
conn := l.accept() or {
|
conn := l.accept() or {
|
||||||
panic('accept() failed')
|
panic('accept() failed')
|
||||||
|
|
Loading…
Reference in New Issue