-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (58 loc) · 1.48 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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
server "beacon-actor/api"
"beacon-actor/config"
"beacon-actor/datastore/postgresql"
eth2client "github.com/dbkbali/go-eth2-client"
"github.com/dbkbali/go-eth2-client/http"
"github.com/joho/godotenv"
"github.com/rs/zerolog"
)
var db *postgresql.DB
var eth2HttpClient *eth2client.Service
func init() {
// load .env file
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
os.Exit(1)
}
}
func main() {
config := config.New()
// startup database
ctx := context.Background()
var err error
db, err = postgresql.NewDb(ctx, config.DatabaseUrl)
if err != nil {
fmt.Println("unable to connect to database: %w", err)
os.Exit(1)
}
db.Ping(ctx)
client := connectToBeaconNode(config.BeaconNodeUrl)
eth2HttpClient = &client
fmt.Println("starting server")
server := server.NewServer(&server.ServerConfig{
ListenAddress: ":8080",
}, db, eth2HttpClient)
server.Start()
fmt.Printf("Server started listening on port %s\n", server.ListenAddress)
}
func connectToBeaconNode(beaconNodeUrl string) (client eth2client.Service) {
fmt.Printf("connecting to beacon node at %s\n", beaconNodeUrl)
client, err := http.New(context.Background(),
http.WithTimeout(15*time.Second),
http.WithAddress(beaconNodeUrl),
http.WithLogLevel(zerolog.DebugLevel),
)
if err != nil {
fmt.Println("unable to connect to beacon node: %w", err)
os.Exit(1)
}
fmt.Printf("Connected to %s\n", client.Name())
return client
}