-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
155 lines (127 loc) · 3.31 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
package main
import (
"context"
"embed"
"fmt"
"io/fs"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
_ "github.com/Khan/genqlient/generate"
"github.com/alecsavvy/clockwise/core"
"github.com/alecsavvy/clockwise/core/db"
"github.com/alecsavvy/clockwise/graph"
"github.com/alecsavvy/clockwise/utils"
"github.com/gorilla/websocket"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
//go:embed templates/*
var embeddedFiles embed.FS
func run() error {
// app level context
ctx := context.Background()
// logger setup
logger := utils.NewLogger(nil)
logger.Info("good morning")
// db setup
pgConnectionString := os.Getenv("pgConnectionString")
err := db.RunMigrations(logger, pgConnectionString)
if err != nil {
return utils.AppError("could not complete database migrations", err)
}
pool, err := pgxpool.New(ctx, pgConnectionString)
if err != nil {
return utils.AppError("failure to create db pool", err)
}
defer pool.Close()
// config setup
homeDir := "./cmt-home"
// only keep last 100 blocks after successful indexing
// move this to a .env config or node type "archive", "service", or something
coreApp := core.NewCore(logger, pool, 100)
node, err := core.NewNode(logger, homeDir, pgConnectionString, coreApp)
if err != nil {
return utils.AppError("failure to init chain", err)
}
// graphql setup
gqlResolver := graph.NewResolver(logger, coreApp, pool)
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: gqlResolver}))
queryHandler := func(c echo.Context) error {
srv.ServeHTTP(c.Response(), c.Request())
return nil
}
// Add WebSocket support for subscriptions
srv.AddTransport(transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
KeepAlivePingInterval: 10 * time.Second,
})
// HTTP transport for queries and mutations
srv.AddTransport(transport.POST{})
gqlPlayground := playground.Handler("GraphQL playground", "/query")
graphiqlHandler := func(c echo.Context) error {
gqlPlayground.ServeHTTP(c.Response(), c.Request())
return nil
}
htmlFS, err := fs.Sub(embeddedFiles, "templates")
if err != nil {
return err
}
htmlTemplates := echo.WrapHandler(http.FileServerFS(htmlFS))
// web server setup
e := echo.New()
e.HideBanner = true
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/graphiql", graphiqlHandler)
e.Any("/query", queryHandler)
e.GET("/", htmlTemplates)
// run all the processes
var wg sync.WaitGroup
wg.Add(3)
// run chain
go func() {
defer wg.Done()
node.Start()
defer func() {
node.Stop()
node.Wait()
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
}()
// run web server
go func() {
defer wg.Done()
err = e.Start("0.0.0.0:26659")
if err != nil {
logger.Error("web server crashed", err)
}
}()
// // run pubsub listener
go func() {
defer wg.Done()
if err := coreApp.Run(node); err != nil {
logger.Error("core app crashed", err)
}
}()
wg.Wait()
return nil
}
func main() {
if err := run(); err != nil {
fmt.Println("fatal error: ", err)
}
}