-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (144 loc) · 4.39 KB
/
main.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (C) 2024, Nuklai. All rights reserved.
// See the file LICENSE for licensing terms.
package main
import (
"context"
"database/sql"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/hypersdk/server"
"github.com/ava-labs/hypersdk/utils"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/nuklai/nuklai-feed/config"
"github.com/nuklai/nuklai-feed/manager"
frpc "github.com/nuklai/nuklai-feed/rpc"
"go.uber.org/zap"
)
var (
httpConfig = server.HTTPConfig{
ReadTimeout: 60 * time.Second,
ReadHeaderTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
}
)
func fatal(l logging.Logger, msg string, fields ...zap.Field) {
l.Fatal(msg, fields...)
os.Exit(1)
}
// HealthHandler responds with a simple health check status
func HealthHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func main() {
err := godotenv.Overload() // Overload the environment variables with those from the .env file
if err != nil {
utils.Outf("{{red}}Error loading .env file{{/}}: %v\n", err)
os.Exit(1)
}
fmt.Println("Loaded environment variables from .env file")
logFactory := logging.NewFactory(logging.Config{
DisplayLevel: logging.Info,
})
l, err := logFactory.Make("main")
if err != nil {
utils.Outf("{{red}}unable to initialize logger{{/}}: %v\n", err)
os.Exit(1)
}
log := l
log.Info("Logger initialized")
// Load config from environment variables
config, err := config.LoadConfigFromEnv()
if err != nil {
fatal(log, "cannot load config from environment variables", zap.Error(err))
}
log.Info("Config loaded from environment variables")
// Load recipient
if _, err := config.RecipientAddress(); err != nil {
fatal(log, "cannot parse recipient address", zap.Error(err))
}
log.Info("Loaded feed recipient", zap.String("address", config.Recipient))
// Create server
listenAddress := net.JoinHostPort(config.HTTPHost, fmt.Sprintf("%d", config.HTTPPort))
listener, err := net.Listen("tcp", listenAddress)
if err != nil {
fatal(log, "cannot create listener", zap.Error(err))
}
log.Info("Created listener", zap.String("address", listenAddress))
mux := http.NewServeMux()
srv := &http.Server{
Addr: listenAddress,
Handler: mux,
ReadTimeout: httpConfig.ReadTimeout,
WriteTimeout: httpConfig.WriteTimeout,
IdleTimeout: httpConfig.IdleTimeout,
}
// Add health check handler
mux.HandleFunc("/health", HealthHandler)
log.Info("Health handler added")
// Retry mechanism for PostgreSQL connection
var db *sql.DB
for i := 0; i < 10; i++ {
connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
config.PostgresHost, config.PostgresPort, config.PostgresUser, config.PostgresPassword, config.PostgresDBName, config.PostgresSSLMode)
db, err = sql.Open("postgres", connStr)
if err != nil {
log.Warn("Error opening database", zap.Error(err))
time.Sleep(5 * time.Second)
continue
}
err = db.Ping()
if err == nil {
break
}
log.Warn("Database not ready, retrying...", zap.Error(err))
time.Sleep(5 * time.Second)
}
if err != nil {
fatal(log, "could not connect to the database", zap.Error(err))
}
log.Info("Database connection established")
// Start manager with context handling
manager, err := manager.New(log, config, db)
if err != nil {
fatal(log, "cannot create manager", zap.Error(err))
}
log.Info("Manager created")
ctx, cancel := context.WithCancel(context.Background())
go func() {
log.Info("Starting manager")
if err := manager.Run(ctx); err != nil {
log.Error("Manager error", zap.Error(err))
}
}()
// Add feed handler
feedServer := frpc.NewJSONRPCServer(manager)
handler, err := server.NewHandler(feedServer, "feed")
if err != nil {
fatal(log, "cannot create handler", zap.Error(err))
}
mux.Handle("/", handler)
log.Info("Feed handler added")
// Start server
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Info("Triggering server shutdown", zap.Any("signal", sig))
cancel() // Ensure context cancellation cascades down
_ = srv.Shutdown(ctx)
}()
log.Info("Server starting")
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Fatal("Server failed", zap.Error(err))
}
log.Info("Server exited")
}