-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.v
154 lines (116 loc) · 2.69 KB
/
main.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module main
import vweb
import db.pg
import net.http
import time
import log
const port = 8092
const db_name = 'vorum'
const db_user = 'vorum'
pub struct App {
vweb.Context
pub mut:
db pg.DB
user User
logged_in bool
logger log.Log @[vweb_global]
}
fn main() {
println('Running Vorum on http://localhost:${port}')
mut app := &App{}
app.init()
vweb.run(app, port)
}
pub fn (mut app App) init() {
app.db = pg.connect(pg.Config{ host: '127.0.0.1', dbname: db_name, user: db_user }) or {
panic(err)
}
app.user = User{}
app.setup_logger()
}
pub fn (mut app App) index() vweb.Result {
app.auth()
posts := app.find_all_posts() or {
app.warn(err.str())
return app.ok('')
}
return $vweb.html()
}
@['/post/:id']
pub fn (mut app App) post(id int) vweb.Result {
post := app.get_post(id) or { return app.text('Discussion not found.') }
app.inc_post_views(id) or { app.warn(err.str()) }
app.auth()
comments := app.find_comments(id) or {
app.warn(err.str())
[]Comment{}
}
return $vweb.html()
}
pub fn (mut app App) new() vweb.Result {
app.auth()
return $vweb.html()
}
@[post]
pub fn (mut app App) new_post() vweb.Result {
app.auth()
if app.user.name == '' {
// Not logged in
return app.redirect('/new')
}
title := app.form['title']
text := app.form['text']
if title == '' || text == '' {
return app.redirect('/new')
}
app.insert_post(title.replace('<', '<'), text) or { app.warn(err.str()) }
return app.redirect('/')
}
@['/comment/:post_id'; post]
fn (mut app App) comment(post_id int) vweb.Result {
app.auth()
if !app.logged_in {
return app.redirect('/')
}
comment_text := app.form['text']
if comment_text == '' {
return app.text('Empty message.')
}
app.insert_comment(post_id, Comment{
text: comment_text
name: app.user.name
}) or { app.warn(err.str()) }
return app.redirect('/post/${post_id}') // so that refreshing a page won't do a post again
}
@['/posts/:id'; delete]
pub fn (mut app App) deletepost(post_id int) vweb.Result {
app.auth()
if !app.user.is_admin {
return app.ok('')
}
sql app.db {
delete from Post where id == post_id
} or { app.warn(err.str()) }
return app.ok('')
}
pub fn (mut app App) logoff() vweb.Result {
app.set_cookie(http.Cookie{ name: 'id', value: '' })
return app.redirect('/')
}
fn (mut app App) setup_logger() {
app.logger.set_level(.debug)
app.logger.set_full_logpath('./logs/log_${time.now().ymmdd()}.log')
app.logger.log_to_console_too()
}
pub fn (mut app App) warn(msg string) {
app.logger.warn(msg)
app.logger.flush()
}
pub fn (mut app App) info(msg string) {
app.logger.info(msg)
app.logger.flush()
}
pub fn (mut app App) debug(msg string) {
app.logger.debug(msg)
app.logger.flush()
}