@@ -16,7 +16,6 @@ import (
16
16
17
17
"github.com/emersion/go-sasl"
18
18
"github.com/emersion/go-smtp"
19
- "golang.org/x/exp/maps"
20
19
)
21
20
22
21
var TERMINATED string
@@ -220,7 +219,39 @@ func ListemSmtps(tlss *smtp.Server) {
220
219
}
221
220
}
222
221
223
- func ListenHealthcheck () {
222
+ func ListenHealthcheck (cfg * Config ) {
223
+ updateToken := os .Getenv ("X_UPDATE_TOKEN" )
224
+
225
+ http .HandleFunc ("/accounts.json" , func (w http.ResponseWriter , r * http.Request ) {
226
+ if updateToken == "" {
227
+ w .WriteHeader (500 )
228
+ w .Write ([]byte ("X_UPDATE_TOKEN not set" ))
229
+ return
230
+ }
231
+
232
+ header := w .Header ().Get ("X-Update-Token" )
233
+ if header != updateToken {
234
+ w .WriteHeader (403 )
235
+ w .Write ([]byte ("Invalid token" ))
236
+ return
237
+ }
238
+
239
+ var updatedAccounts map [string ]Credential
240
+ err := json .NewDecoder (r .Body ).Decode (& updatedAccounts )
241
+
242
+ if err != nil {
243
+ fmt .Println ("Failed updating accounts" )
244
+ fmt .Println (err .Error ())
245
+ w .WriteHeader (500 )
246
+ w .Write ([]byte ("Failed updating accounts" ))
247
+ return
248
+ }
249
+
250
+ cfg .Credentials = updatedAccounts
251
+ fmt .Println ("Updated accounts: " )
252
+ fmt .Println (cfg .Credentials )
253
+ })
254
+
224
255
http .ListenAndServe (":8080" , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
225
256
w .WriteHeader (200 )
226
257
w .Write ([]byte ("OK" ))
@@ -359,34 +390,5 @@ func main() {
359
390
360
391
go Listen (smtps )
361
392
362
- go ListenHealthcheck ()
363
-
364
- for {
365
- if TERMINATED != "" {
366
- log .Fatal (TERMINATED )
367
- panic (fmt .Errorf (TERMINATED ))
368
- }
369
- time .Sleep (time .Duration (10 ) * time .Second )
370
-
371
- // fileInfo, err := os.Stat("credentials.json")
372
- // if err != nil {
373
- // TERMINATED = err.Error()
374
- // }
375
-
376
- // if LASTMOD < fileInfo.ModTime().Unix() {
377
- updatedCredentials := & map [string ]Credential {}
378
- credentialFile , err := os .ReadFile ("credentials.json" )
379
- if err != nil {
380
- TERMINATED = err .Error ()
381
- }
382
-
383
- json .Unmarshal (credentialFile , & updatedCredentials )
384
-
385
- config .Credentials = * updatedCredentials
386
- // LASTMOD = fileInfo.ModTime().Unix()
387
-
388
- fmt .Println ("Updated credentials" )
389
- fmt .Println (maps .Keys (* credentials ))
390
- // }
391
- }
393
+ ListenHealthcheck (config )
392
394
}
0 commit comments