Skip to content

Commit 81d1439

Browse files
author
c-cesar
committed
set headers with a middleware to reduce code repetition
1 parent 4d8219a commit 81d1439

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

frameworks/V/veb/fortunes.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><title>Fortunes</title></head>
4+
<body>
5+
<table>
6+
<tr><th>id</th><th>message</th></tr>
7+
@for m in fortunes
8+
<tr><td>@m.id</td><td>@m.message</td></tr>
9+
@end
10+
</table>
11+
</body>
12+
</html>

frameworks/V/veb/main.v

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,14 @@ import time
33
import rand
44
import db.pg
55

6-
pub struct Context {
7-
veb.Context
8-
}
9-
10-
pub struct App {
11-
pub mut:
12-
db pg.DB
13-
}
14-
156
pub fn (app &App) plaintext(mut ctx Context) veb.Result {
16-
ctx.set_header(.date, time.now().as_utc().custom_format('ddd, DD MMM YYYY HH:MM:ss') + ' GMT')
17-
ctx.set_header(.server, 'veb')
18-
return ctx.text('Hello, World!')
7+
s := 'Hello, World!'
8+
return ctx.text(s)
199
}
2010

2111
pub fn (app &App) json(mut ctx Context) veb.Result {
2212
obj := {'message': 'Hello, World!'}
23-
ctx.set_header(.date, time.now().as_utc().custom_format('ddd, DD MMM YYYY HH:MM:ss') + ' GMT')
24-
ctx.set_header(.server, 'veb')
25-
return ctx.json(obj)
13+
return ctx.json(obj)
2614
}
2715

2816
struct World {
@@ -36,8 +24,6 @@ pub fn (app &App) db(mut ctx Context) veb.Result {
3624
mut world := sql app.db {
3725
select from World where id == r
3826
} or { return ctx.text('db error') }
39-
ctx.set_header(.date, time.now().as_utc().custom_format('ddd, DD MMM YYYY HH:MM:ss') + ' GMT')
40-
ctx.set_header(.server, 'veb')
4127
return ctx.json(world.first())
4228
}
4329

@@ -51,8 +37,6 @@ pub fn (app &App) queries(mut ctx Context) veb.Result {
5137
select from World where id == r
5238
} or { return ctx.text('db error') }
5339
}
54-
ctx.set_header(.date, time.now().as_utc().custom_format('ddd, DD MMM YYYY HH:MM:ss') + ' GMT')
55-
ctx.set_header(.server, 'veb')
5640
return ctx.json(world)
5741
}
5842

@@ -70,8 +54,6 @@ pub fn (app &App) update(mut ctx Context) veb.Result {
7054
update World set randomnumber = world.last().randomnumber where id == world.last().id
7155
} or { return ctx.text('db error') }
7256
}
73-
ctx.set_header(.date, time.now().as_utc().custom_format('ddd, DD MMM YYYY HH:MM:ss') + ' GMT')
74-
ctx.set_header(.server, 'veb')
7557
return ctx.json(world)
7658
}
7759

@@ -86,12 +68,26 @@ pub fn (app &App) fortunes(mut ctx Context) veb.Result {
8668
} or { return ctx.text('db error') }
8769
fortunes.insert(0, Fortune{id: 0, message: 'Additional fortune added at request time.'})
8870
fortunes.sort(a.message < b.message)
89-
ctx.set_header(.date, time.now().as_utc().custom_format('ddd, DD MMM YYYY HH:MM:ss') + ' GMT')
90-
ctx.set_header(.server, 'veb')
9171
ctx.content_type = 'text/html; charset=utf-8'
9272
return $veb.html()
9373
}
9474

75+
pub struct Context {
76+
veb.Context
77+
}
78+
79+
pub struct App {
80+
veb.Middleware[Context]
81+
pub mut:
82+
db pg.DB
83+
}
84+
85+
pub fn header(mut ctx Context) bool {
86+
ctx.set_header(.date, time.now().as_utc().custom_format('ddd, DD MMM YYYY HH:MM:ss') + ' GMT')
87+
ctx.set_header(.server, 'veb')
88+
return true
89+
}
90+
9591
fn main() {
9692
mut app := &App{
9793
db: pg.connect(pg.Config{
@@ -102,5 +98,6 @@ fn main() {
10298
dbname: 'hello_world'
10399
}) !
104100
}
101+
app.use(handler: header)
105102
veb.run[App, Context](mut app, 8080)
106103
}

frameworks/V/veb/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
for i in $(seq 0 $(nproc --ignore=1)); do
3+
for i in $(seq 0 $(nproc)); do
44
taskset -c $i ./main &
55
done
66

0 commit comments

Comments
 (0)