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