Skip to content

Commit c145340

Browse files
committed
Added update accounts endpoint
1 parent 32ac58d commit c145340

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

main.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
"github.com/emersion/go-sasl"
1818
"github.com/emersion/go-smtp"
19-
"golang.org/x/exp/maps"
2019
)
2120

2221
var TERMINATED string
@@ -220,7 +219,39 @@ func ListemSmtps(tlss *smtp.Server) {
220219
}
221220
}
222221

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+
224255
http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
225256
w.WriteHeader(200)
226257
w.Write([]byte("OK"))
@@ -359,34 +390,5 @@ func main() {
359390

360391
go Listen(smtps)
361392

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)
392394
}

0 commit comments

Comments
 (0)