2024-04-21 21:46:40 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"git.simponic.xyz/simponic/backup-notify/api"
|
|
|
|
"git.simponic.xyz/simponic/backup-notify/args"
|
|
|
|
"git.simponic.xyz/simponic/backup-notify/database"
|
|
|
|
"git.simponic.xyz/simponic/backup-notify/ntfy"
|
|
|
|
"git.simponic.xyz/simponic/backup-notify/scheduler"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
|
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("could not load .env file:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
argv, err := args.GetArgs()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbConn := database.MakeConn(&argv.DatabasePath)
|
|
|
|
defer dbConn.Close()
|
|
|
|
|
|
|
|
if argv.Migrate {
|
|
|
|
_, err = database.Migrate(dbConn)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Println("database migrated successfully")
|
|
|
|
}
|
|
|
|
|
|
|
|
if argv.Scheduler {
|
2024-04-22 00:14:58 -04:00
|
|
|
scheduler.StartScheduler(dbConn, argv)
|
2024-04-21 21:46:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if argv.NtfyEndpoint != "" {
|
2024-04-22 00:14:58 -04:00
|
|
|
ntfy := ntfy.MakeNtfyWatcher(argv.NtfyEndpoint, argv.NtfyBackupTopics)
|
2024-04-21 21:46:40 -04:00
|
|
|
notifications := ntfy.Watch()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for notification := range notifications {
|
|
|
|
message := notification.Text
|
|
|
|
messageStruct := struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Time int `json:"time"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Event string `json:"event"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(message), &messageStruct)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("could not unmarshal message:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if messageStruct.Event == "keepalive" {
|
|
|
|
log.Println("received keepalive message")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if messageStruct.Event != "message" {
|
|
|
|
log.Println("received unknown event:", messageStruct.Event)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("received backup host:", messageStruct.Message)
|
|
|
|
err = database.ReceivedBackup(dbConn, messageStruct.Message)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("could not record backup:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if argv.Server {
|
|
|
|
server := api.MakeServer(argv, dbConn)
|
|
|
|
log.Println("🚀🚀 API listening on port", argv.Port)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err = server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {} // block forever
|
|
|
|
}
|