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 } 9import beep_sql 10import util 11 12pub const version = '25.01.0' 13 14@[inline] 15fn connect(mut app App) { 16 println('-> connecting to database...') 17 app.db = pg.connect(pg.Config{ 18 host: app.config.postgres.host 19 dbname: app.config.postgres.db 20 user: app.config.postgres.user 21 password: app.config.postgres.password 22 port: app.config.postgres.port 23 }) or { 24 panic('failed to connect to database: ${err}') 25 } 26} 27 28@[inline] 29fn init_db(mut app App) { 30 println('-> initializing database') 31 sql app.db { 32 create table entity.Site 33 create table entity.User 34 create table entity.Post 35 create table entity.Like 36 create table entity.LikeCache 37 create table entity.Notification 38 create table entity.SavedPost 39 } or { 40 panic('failed to initialize database: ${err}') 41 } 42} 43 44@[inline] 45fn load_validators(mut app App) { 46 app.validators.username = StringValidator.new(app.config.user.username_min_len,app.config.user.username_max_len,app.config.user.username_pattern) 47 app.validators.password = StringValidator.new(app.config.user.password_min_len,app.config.user.password_max_len,app.config.user.password_pattern) 48 app.validators.nickname = StringValidator.new(app.config.user.nickname_min_len,app.config.user.nickname_max_len,app.config.user.nickname_pattern) 49 app.validators.user_bio = StringValidator.new(app.config.user.bio_min_len,app.config.user.bio_max_len,app.config.user.bio_pattern) 50 app.validators.pronouns = StringValidator.new(app.config.user.pronouns_min_len,app.config.user.pronouns_max_len,app.config.user.pronouns_pattern) 51 app.validators.post_title = StringValidator.new(app.config.post.title_min_len,app.config.post.title_max_len,app.config.post.title_pattern) 52 app.validators.post_body = StringValidator.new(app.config.post.body_min_len,app.config.post.body_max_len,app.config.post.body_pattern) 53} 54 55fn main() { 56 mut stopwatch := util.Stopwatch.new() 57 58 config := webapp.load_config_from(os.args[1]) 59 mut app := &App{ config: config } 60 61 // connect to database 62 util.time_it( 63 it: fn [mut app] () { 64 connect(mut app) 65 } 66 name: 'connect to db' 67 log: true 68 ) 69 70 defer { app.db.close() } 71 72 // add authenticator 73 app.auth = auth.new(app.db) 74 75 // load sql files kept in beep_sql/ 76 util.time_it( 77 it: fn [mut app] () { 78 beep_sql.load(mut app.db) 79 } 80 name: 'load beep_sql' 81 log: true 82 ) 83 84 // load validators 85 load_validators(mut app) 86 87 // mount static things 88 app.mount_static_folder_at(app.config.static_path, '/static')! 89 90 // initialize database 91 util.time_it(it: fn [mut app] () { 92 init_db(mut app) 93 }, name: 'init db', log: true) 94 95 // make the website config, if it does not exist 96 app.get_or_create_site_config() 97 98 if config.dev_mode { 99 println('\033[1;31mNOTE: YOU ARE IN DEV MODE\033[0m') 100 } 101 102 stop := stopwatch.stop() 103 println('-> took ${stop} to start app') 104 105 veb.run[App, Context](mut app, app.config.http.port) 106}