-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (66 loc) · 1.45 KB
/
Copy pathmain.go
File metadata and controls
76 lines (66 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/mujib77/pgstream/config"
"github.com/mujib77/pgstream/internal/connector"
"github.com/mujib77/pgstream/internal/decoder"
"github.com/mujib77/pgstream/internal/handler"
)
func main() {
cfg := config.DefaultConfig()
fmt.Println("connecting to postgres...")
ctx := context.Background()
conn, err := connector.New(ctx, cfg.DatabaseURL, cfg.SlotName, cfg.PublicationName)
if err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
defer conn.Close(ctx)
fmt.Println("connected!")
fmt.Println("creating replication slot...")
err = conn.CreateSlot(ctx)
if err != nil {
fmt.Println("slot may already exist, continuing...")
}
fmt.Println("starting replication...")
err = conn.Start(ctx)
if err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
dec := decoder.New(conn.GetRawConn())
han := handler.New()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
fmt.Println("\nshutting down...")
cancel()
}()
fmt.Println("listening for changes...")
for {
select {
case <-ctx.Done():
return
default:
event, err := dec.NextEvent(ctx)
if err != nil {
if ctx.Err() != nil {
return
}
fmt.Println("error:", err)
return
}
err = han.Handle(event)
if err != nil {
fmt.Println("error:", err)
}
}
}
}