-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (80 loc) · 2.1 KB
/
main.go
File metadata and controls
92 lines (80 loc) · 2.1 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"github.com/streamcoreai/sip-server/internal/api"
"github.com/streamcoreai/sip-server/internal/call"
"github.com/streamcoreai/sip-server/internal/config"
"github.com/streamcoreai/sip-server/internal/provider"
sipserver "github.com/streamcoreai/sip-server/internal/sip"
)
func main() {
configPath := "config.toml"
if len(os.Args) > 1 {
configPath = os.Args[1]
}
cfg, err := config.Load(configPath)
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
// Register providers
registry := provider.NewRegistry()
for name, pc := range cfg.Providers {
if !pc.Enabled {
continue
}
switch name {
case "twilio":
registry.Register(&provider.Twilio{
TrunkDomain: pc.TrunkDomain,
Username: pc.Username,
Password: pc.Password,
})
case "ringcentral":
registry.Register(&provider.RingCentral{
OutboundProxy: pc.OutboundProxy,
Username: pc.Username,
Password: pc.Password,
})
case "asterisk":
registry.Register(&provider.Asterisk{
Host: pc.Host,
Port: pc.Port,
Username: pc.Username,
Password: pc.Password,
Context: pc.Context,
})
default:
log.Printf("unknown provider %q, skipping", name)
}
}
// Create call manager
callMgr := call.NewManager(cfg)
// Create SIP server
sipSrv, err := sipserver.New(cfg, callMgr, registry)
if err != nil {
log.Fatalf("failed to create SIP server: %v", err)
}
// Create API server
apiSrv := api.New(cfg.API.ListenAddr, callMgr, sipSrv, registry)
// Setup graceful shutdown
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
// Start API server
go func() {
if err := apiSrv.ListenAndServe(); err != nil {
log.Printf("[api] server stopped: %v", err)
}
}()
// Start SIP server (blocks until context is cancelled)
log.Println("SIP server starting...")
if err := sipSrv.ListenAndServe(ctx); err != nil {
log.Printf("[sip] server stopped: %v", err)
}
// Shutdown API
apiSrv.Shutdown(context.Background())
log.Println("shutdown complete")
}