matrix-pretix/main.go

75 lines
1.2 KiB
Go
Raw Normal View History

2022-07-22 19:52:41 +02:00
package main
import (
2022-07-24 02:40:44 +02:00
"context"
"log"
2022-07-23 00:36:28 +02:00
"os"
2022-07-24 02:40:44 +02:00
"os/signal"
"sync"
"syscall"
2022-07-22 19:52:41 +02:00
"git.luj0ga.de/franconian/matrix-pretix/internal/config"
2022-07-24 02:40:44 +02:00
"git.luj0ga.de/franconian/matrix-pretix/internal/database"
2022-07-22 19:52:41 +02:00
"git.luj0ga.de/franconian/matrix-pretix/internal/matrix"
2022-07-24 02:40:44 +02:00
"git.luj0ga.de/franconian/matrix-pretix/internal/pretix"
2022-07-22 19:52:41 +02:00
)
func main() {
if len(os.Args) != 2 {
log.Fatal("missing config file path")
2022-07-22 19:52:41 +02:00
}
config, err := config.ParseFromFile(os.Args[1])
2022-07-22 19:52:41 +02:00
if err != nil {
log.Fatal(err)
2022-07-22 19:52:41 +02:00
}
2022-07-24 02:40:44 +02:00
db, err := database.Open(&config.DB)
2022-07-22 19:52:41 +02:00
if err != nil {
log.Fatal(err)
2022-07-22 19:52:41 +02:00
}
2022-07-24 02:40:44 +02:00
defer db.Close()
client, err := matrix.NewClient(&config.Matrix, db)
if err != nil {
log.Fatal(err)
}
err = client.Login()
if err != nil {
log.Fatal(err)
}
err = client.Encrypt()
if err != nil {
log.Fatal(err)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
var wg sync.WaitGroup
go func() {
wg.Add(1)
defer wg.Done()
<-ctx.Done()
stop()
}()
go client.Sync(ctx, stop, &wg)
2022-07-24 02:40:44 +02:00
2022-07-24 21:41:01 +02:00
server := pretix.NewServer(&config.Server, db, client)
2022-07-24 02:40:44 +02:00
2022-07-24 21:41:01 +02:00
err = server.ListenAndServe(ctx, &wg)
2022-07-24 02:40:44 +02:00
if err != nil {
stop()
wg.Wait()
log.Fatal(err)
}
wg.Wait()
log.Print("done")
2022-07-22 19:52:41 +02:00
}