a mini social media app for small communities
1module main
2
3import db.pg
4import veb
5import auth
6import entity
7import os
8
9fn init_db(db pg.DB) ! {
10 sql db {
11 create table entity.Site
12 create table entity.User
13 create table entity.Post
14 create table entity.Like
15 create table entity.LikeCache
16 create table entity.Notification
17 }!
18}
19
20fn main() {
21 config := load_config_from(os.args[1])
22
23 println('-> connecting to db...')
24 mut db := pg.connect(pg.Config{
25 host: config.postgres.host
26 dbname: config.postgres.db
27 user: config.postgres.user
28 password: config.postgres.password
29 port: config.postgres.port
30 })!
31 println('<- connected')
32
33 defer {
34 db.close()
35 }
36
37 mut app := &App{
38 config: config
39 db: db
40 auth: auth.new(db)
41 }
42
43 // vfmt off
44 app.validators.username = StringValidator.new(config.user.username_min_len, config.user.username_max_len, config.user.username_pattern)
45 app.validators.password = StringValidator.new(config.user.username_min_len, config.user.username_max_len, config.user.username_pattern)
46 app.validators.nickname = StringValidator.new(config.user.nickname_min_len, config.user.nickname_max_len, config.user.nickname_pattern)
47 app.validators.user_bio = StringValidator.new(config.user.bio_min_len, config.user.bio_max_len, config.user.bio_pattern)
48 app.validators.pronouns = StringValidator.new(config.user.pronouns_min_len, config.user.pronouns_max_len, config.user.pronouns_pattern)
49 app.validators.post_title = StringValidator.new(config.post.title_min_len, config.post.title_max_len, config.post.title_pattern)
50 app.validators.post_body = StringValidator.new(config.post.body_min_len, config.post.body_max_len, config.post.body_pattern)
51 // vfmt on
52
53 app.mount_static_folder_at(app.config.static_path, '/static')!
54
55 println('-> initializing database...')
56 init_db(db)!
57 println('<- done')
58
59 // make the website config, if it does not exist
60 app.get_or_create_site_config()
61
62 if config.dev_mode {
63 println('\033[1;31mNOTE: YOU ARE IN DEV MODE\033[0m')
64 }
65
66 veb.run[App, Context](mut app, app.config.http.port)
67}