6
6
"encoding/json"
7
7
"errors"
8
8
"fmt"
9
- "log"
10
9
"math/rand"
11
10
"net/http"
12
11
"os"
@@ -23,6 +22,7 @@ import (
23
22
"github.com/staticbackendhq/core/email"
24
23
"github.com/staticbackendhq/core/function"
25
24
"github.com/staticbackendhq/core/internal"
25
+ "github.com/staticbackendhq/core/logger"
26
26
"github.com/staticbackendhq/core/middleware"
27
27
"github.com/staticbackendhq/core/realtime"
28
28
"github.com/staticbackendhq/core/storage"
@@ -53,19 +53,21 @@ func init() {
53
53
}
54
54
55
55
// Start starts the web server and all dependencies services
56
- func Start (c config.AppConfig ) {
56
+ func Start (c config.AppConfig , log * logger.Logger ) {
57
+ log .Info ().Str ("Addr" , c .AppURL ).Msg ("server started" )
58
+
57
59
config .Current = c
58
60
59
61
stripe .Key = config .Current .StripeKey
60
62
61
63
if err := loadTemplates (); err != nil {
62
64
// if we're running from the CLI, no need to load templates
63
65
if len (config .Current .FromCLI ) == 0 {
64
- log .Fatal ("error loading templates: " , err )
66
+ log .Fatal (). Err ( err ). Msg ( "error loading templates" )
65
67
}
66
68
}
67
69
68
- initServices (c .DatabaseURL )
70
+ initServices (c .DatabaseURL , log )
69
71
70
72
// websockets
71
73
hub := newHub (volatile )
@@ -112,10 +114,11 @@ func Start(c config.AppConfig) {
112
114
}
113
115
114
116
return key , nil
115
- }, volatile )
117
+ }, volatile , log )
116
118
117
119
database := & Database {
118
120
cache : volatile ,
121
+ log : log ,
119
122
}
120
123
121
124
stdPub := []middleware.Middleware {
@@ -138,7 +141,7 @@ func Start(c config.AppConfig) {
138
141
middleware .RequireRoot (datastore ),
139
142
}
140
143
141
- m := & membership {}
144
+ m := & membership {log : log }
142
145
143
146
http .Handle ("/login" , middleware .Chain (http .HandlerFunc (m .login ), pubWithDB ... ))
144
147
http .Handle ("/register" , middleware .Chain (http .HandlerFunc (m .register ), pubWithDB ... ))
@@ -149,7 +152,7 @@ func Start(c config.AppConfig) {
149
152
http .Handle ("/me" , middleware .Chain (http .HandlerFunc (m .me ), stdAuth ... ))
150
153
151
154
// oauth handlers
152
- el := & ExternalLogins {}
155
+ el := & ExternalLogins {log : log }
153
156
http .Handle ("/oauth/login" , middleware .Chain (el .login (), pubWithDB ... ))
154
157
http .Handle ("/oauth/callback/" , middleware .Chain (el .callback (), stdPub ... ))
155
158
http .Handle ("/oauth/get-user" , middleware .Chain (http .HandlerFunc (el .getUser ), pubWithDB ... ))
@@ -179,19 +182,19 @@ func Start(c config.AppConfig) {
179
182
http .Handle ("/sudo/cache" , middleware .Chain (http .HandlerFunc (sudoCache ), stdRoot ... ))
180
183
181
184
// account
182
- acct := & accounts {membership : m }
185
+ acct := & accounts {membership : m , log : log }
183
186
http .Handle ("/account/init" , middleware .Chain (http .HandlerFunc (acct .create ), stdPub ... ))
184
187
http .Handle ("/account/auth" , middleware .Chain (http .HandlerFunc (acct .auth ), stdRoot ... ))
185
188
http .Handle ("/account/portal" , middleware .Chain (http .HandlerFunc (acct .portal ), stdRoot ... ))
186
189
187
190
// stripe webhooks
188
- swh := stripeWebhook {}
191
+ swh := stripeWebhook {log : log }
189
192
http .HandleFunc ("/stripe" , swh .process )
190
193
191
194
http .HandleFunc ("/ping" , ping )
192
195
193
196
http .HandleFunc ("/ws" , func (w http.ResponseWriter , r * http.Request ) {
194
- serveWs (hub , w , r )
197
+ serveWs (log , hub , w , r )
195
198
})
196
199
197
200
http .Handle ("/sse/connect" , middleware .Chain (http .HandlerFunc (b .Accept ), pubWithDB ... ))
@@ -219,7 +222,7 @@ func Start(c config.AppConfig) {
219
222
http .Handle ("/fn" , middleware .Chain (http .HandlerFunc (f .list ), stdRoot ... ))
220
223
221
224
// extras routes
222
- ex := & extras {}
225
+ ex := & extras {log : log }
223
226
http .Handle ("/extra/resizeimg" , middleware .Chain (http .HandlerFunc (ex .resizeImage ), stdAuth ... ))
224
227
http .Handle ("/extra/sms" , middleware .Chain (http .HandlerFunc (ex .sudoSendSMS ), stdRoot ... ))
225
228
http .Handle ("/extra/htmltox" , middleware .Chain (http .HandlerFunc (ex .htmlToX ), stdAuth ... ))
@@ -233,7 +236,7 @@ func Start(c config.AppConfig) {
233
236
}
234
237
235
238
// ui routes
236
- webUI := ui {}
239
+ webUI := ui {log : log }
237
240
http .HandleFunc ("/ui/login" , webUI .auth )
238
241
http .Handle ("/ui/logins" , middleware .Chain (http .HandlerFunc (webUI .logins ), stdRoot ... ))
239
242
http .Handle ("/ui/enable-login" , middleware .Chain (http .HandlerFunc (webUI .enableExternalLogin ), stdRoot ... ))
@@ -278,16 +281,16 @@ func Start(c config.AppConfig) {
278
281
})
279
282
280
283
if err := g .Wait (); err != nil {
281
- fmt . Printf ( "exit reason: %s \n " , err )
284
+ log . Error (). Err ( err ). Msg ( "exit reason" )
282
285
}
283
286
}
284
287
285
- func initServices (dbHost string ) {
288
+ func initServices (dbHost string , log * logger. Logger ) {
286
289
287
290
if strings .EqualFold (dbHost , "mem" ) {
288
291
volatile = cache .NewDevCache ()
289
292
} else {
290
- volatile = cache .NewCache ()
293
+ volatile = cache .NewCache (log )
291
294
}
292
295
293
296
persister := config .Current .DataStore
@@ -296,16 +299,16 @@ func initServices(dbHost string) {
296
299
} else if strings .EqualFold (persister , "mongo" ) {
297
300
cl , err := openMongoDatabase (dbHost )
298
301
if err != nil {
299
- log .Fatal (err )
302
+ log .Fatal (). Err ( err ). Msg ( "failed to create connection with mongodb" )
300
303
}
301
- datastore = mongo .New (cl , volatile .PublishDocument )
304
+ datastore = mongo .New (cl , volatile .PublishDocument , log )
302
305
} else {
303
306
cl , err := openPGDatabase (dbHost )
304
307
if err != nil {
305
- log .Fatal (err )
308
+ log .Fatal (). Err ( err ). Msg ( "failed to create connection with postgres" )
306
309
}
307
310
308
- datastore = postgresql .New (cl , volatile .PublishDocument , "./sql/" )
311
+ datastore = postgresql .New (cl , volatile .PublishDocument , "./sql/" , log )
309
312
}
310
313
311
314
mp := config .Current .MailProvider
@@ -322,7 +325,7 @@ func initServices(dbHost string) {
322
325
storer = storage.Local {}
323
326
}
324
327
325
- sub := & function.Subscriber {}
328
+ sub := & function.Subscriber {Log : log }
326
329
sub .PubSub = volatile
327
330
sub .GetExecEnv = func (token string ) (function.ExecutionEnvironment , error ) {
328
331
var exe function.ExecutionEnvironment
@@ -332,19 +335,19 @@ func initServices(dbHost string) {
332
335
if strings .HasPrefix (token , "__tmp__experimental_public" ) {
333
336
pk := strings .Replace (token , "__tmp__experimental_public_" , "" , - 1 )
334
337
pairs := strings .Split (pk , "_" )
335
- fmt . Println ( "checking for base in cache: " , pairs [0 ])
338
+ log . Info (). Msgf ( "checking for base in cache: %d " , pairs [0 ])
336
339
if err := volatile .GetTyped (pairs [0 ], & conf ); err != nil {
337
- log .Println ("cannot find base for public websocket" )
340
+ log .Error (). Err ( err ). Msg ("cannot find base for public websocket" )
338
341
return exe , err
339
342
}
340
343
} else if err := volatile .GetTyped ("base:" + token , & conf ); err != nil {
341
- log .Println ("cannot find base" )
344
+ log .Error (). Err ( err ). Msg ("cannot find base" )
342
345
return exe , err
343
346
}
344
347
345
348
var auth internal.Auth
346
349
if err := volatile .GetTyped (token , & auth ); err != nil {
347
- log .Println ("cannot find auth" )
350
+ log .Error (). Err ( err ). Msg ("cannot find auth" )
348
351
return exe , err
349
352
}
350
353
@@ -359,6 +362,7 @@ func initServices(dbHost string) {
359
362
// start system events subscriber
360
363
go sub .Start ()
361
364
}
365
+
362
366
func openMongoDatabase (dbHost string ) (* mongodrv.Client , error ) {
363
367
uri := dbHost
364
368
0 commit comments