a mini social media app for small communities
1module webapp
2
3import emmathemartian.maple
4
5// Config stores constant site-wide configuration data.
6pub struct Config {
7pub mut:
8 dev_mode bool
9 static_path string
10 instance struct {
11 pub mut:
12 name string
13 welcome string
14 default_theme string
15 default_css string
16 allow_changing_theme bool
17 version string
18 source string
19 v_source string
20 invite_only bool
21 invite_code string
22 public_data bool
23 owner_username string
24 }
25 http struct {
26 pub mut:
27 port int
28 }
29 postgres struct {
30 pub mut:
31 host string
32 port int
33 user string
34 password string
35 db string
36 }
37 hcaptcha struct {
38 pub mut:
39 enabled bool
40 secret string
41 site_key string
42 }
43 post struct {
44 pub mut:
45 title_min_len int
46 title_max_len int
47 title_pattern string
48 body_min_len int
49 body_max_len int
50 body_pattern string
51 allow_nsfw bool
52 }
53 user struct {
54 pub mut:
55 username_min_len int
56 username_max_len int
57 username_pattern string
58 nickname_min_len int
59 nickname_max_len int
60 nickname_pattern string
61 password_min_len int
62 password_max_len int
63 password_pattern string
64 pronouns_min_len int
65 pronouns_max_len int
66 pronouns_pattern string
67 bio_min_len int
68 bio_max_len int
69 bio_pattern string
70 }
71 welcome struct {
72 pub mut:
73 summary string
74 body string
75 }
76}
77
78pub fn load_config_from(file_path string) Config {
79 loaded := maple.load_file(file_path) or { panic(err) }
80 mut config := Config{}
81
82 config.dev_mode = loaded.get('dev_mode').to_bool()
83 config.static_path = loaded.get('static_path').to_str()
84
85 loaded_instance := loaded.get('instance')
86 config.instance.name = loaded_instance.get('name').to_str()
87 config.instance.welcome = loaded_instance.get('welcome').to_str()
88 config.instance.default_theme = loaded_instance.get('default_theme').to_str()
89 config.instance.default_css = loaded_instance.get('default_css').to_str()
90 config.instance.allow_changing_theme = loaded_instance.get('allow_changing_theme').to_bool()
91 config.instance.version = loaded_instance.get('version').to_str()
92 config.instance.source = loaded_instance.get('source').to_str()
93 config.instance.v_source = loaded_instance.get('v_source').to_str()
94 config.instance.invite_only = loaded_instance.get('invite_only').to_bool()
95 config.instance.invite_code = loaded_instance.get('invite_code').to_str()
96 config.instance.public_data = loaded_instance.get('public_data').to_bool()
97 config.instance.owner_username = loaded_instance.get('owner_username').to_str()
98
99 loaded_http := loaded.get('http')
100 config.http.port = loaded_http.get('port').to_int()
101
102 loaded_postgres := loaded.get('postgres')
103 config.postgres.host = loaded_postgres.get('host').to_str()
104 config.postgres.port = loaded_postgres.get('port').to_int()
105 config.postgres.user = loaded_postgres.get('user').to_str()
106 config.postgres.password = loaded_postgres.get('password').to_str()
107 config.postgres.db = loaded_postgres.get('db').to_str()
108
109 loaded_hcaptcha := loaded.get('hcaptcha')
110 config.hcaptcha.enabled = loaded_hcaptcha.get('enabled').to_bool()
111 config.hcaptcha.secret = loaded_hcaptcha.get('secret').to_str()
112 config.hcaptcha.site_key = loaded_hcaptcha.get('site_key').to_str()
113
114 loaded_post := loaded.get('post')
115 config.post.title_min_len = loaded_post.get('title_min_len').to_int()
116 config.post.title_max_len = loaded_post.get('title_max_len').to_int()
117 config.post.title_pattern = loaded_post.get('title_pattern').to_str()
118 config.post.body_min_len = loaded_post.get('body_min_len').to_int()
119 config.post.body_max_len = loaded_post.get('body_max_len').to_int()
120 config.post.body_pattern = loaded_post.get('body_pattern').to_str()
121 config.post.allow_nsfw = loaded_post.get('allow_nsfw').to_bool()
122
123 loaded_user := loaded.get('user')
124 config.user.username_min_len = loaded_user.get('username_min_len').to_int()
125 config.user.username_max_len = loaded_user.get('username_max_len').to_int()
126 config.user.username_pattern = loaded_user.get('username_pattern').to_str()
127 config.user.nickname_min_len = loaded_user.get('nickname_min_len').to_int()
128 config.user.nickname_max_len = loaded_user.get('nickname_max_len').to_int()
129 config.user.nickname_pattern = loaded_user.get('nickname_pattern').to_str()
130 config.user.password_min_len = loaded_user.get('password_min_len').to_int()
131 config.user.password_max_len = loaded_user.get('password_max_len').to_int()
132 config.user.password_pattern = loaded_user.get('password_pattern').to_str()
133 config.user.pronouns_min_len = loaded_user.get('pronouns_min_len').to_int()
134 config.user.pronouns_max_len = loaded_user.get('pronouns_max_len').to_int()
135 config.user.pronouns_pattern = loaded_user.get('pronouns_pattern').to_str()
136 config.user.bio_min_len = loaded_user.get('bio_min_len').to_int()
137 config.user.bio_max_len = loaded_user.get('bio_max_len').to_int()
138 config.user.bio_pattern = loaded_user.get('bio_pattern').to_str()
139
140 loaded_welcome := loaded.get('welcome')
141 config.welcome.summary = loaded_welcome.get('summary').to_str()
142 config.welcome.body = loaded_welcome.get('body').to_str()
143
144 return config
145}
146
147pub struct BuildInfo {
148pub mut:
149 commit string
150}
151
152pub fn load_buildinfo_from(file_path string) BuildInfo {
153 loaded := maple.load_file(file_path) or { panic(err) }
154 mut buildinfo := BuildInfo{}
155
156 buildinfo.commit = loaded.get('commit').to_str()
157
158 return buildinfo
159}