-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (90 loc) · 2.71 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
package main
import (
"context"
"log"
"net"
"net/http"
"github.com/decentrio/sorobook-api/app/contract"
"github.com/decentrio/sorobook-api/app/event"
"github.com/decentrio/sorobook-api/app/ledger"
"github.com/decentrio/sorobook-api/app/transaction"
_ "github.com/decentrio/sorobook-api/docs/statik"
"github.com/rakyll/statik/fs"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/decentrio/sorobook-api/app"
"github.com/decentrio/sorobook-api/database"
contracttypes "github.com/decentrio/sorobook-api/types/contract"
eventtypes "github.com/decentrio/sorobook-api/types/event"
ledgertypes "github.com/decentrio/sorobook-api/types/ledger"
txtypes "github.com/decentrio/sorobook-api/types/transaction"
)
func initModule() []app.AppModule {
dbHandler := database.NewDBHandler()
contractKeeper := contract.NewKeeper(dbHandler)
eventKeeper := event.NewKeeper(dbHandler)
ledgerKeeper := ledger.NewKeeper(dbHandler)
transactionKeeper := transaction.NewKeeper(dbHandler)
modules := []app.AppModule{
contract.NewAppModule(*contractKeeper),
event.NewAppModule(*eventKeeper),
ledger.NewAppModule(*ledgerKeeper),
transaction.NewAppModule(*transactionKeeper),
}
return modules
}
func runGRPCServer() error {
lis, err := net.Listen("tcp", ":9090")
if err != nil {
return err
}
s := grpc.NewServer()
modules := initModule()
bookApp := app.NewApp(s, modules)
bookApp.RegisterServices()
return s.Serve(lis)
}
func runHTTPServer() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := contracttypes.RegisterContractQueryHandlerFromEndpoint(ctx, mux, ":9090", opts)
if err != nil {
return err
}
err = eventtypes.RegisterEventQueryHandlerFromEndpoint(ctx, mux, ":9090", opts)
if err != nil {
return err
}
err = ledgertypes.RegisterLedgerQueryHandlerFromEndpoint(ctx, mux, ":9090", opts)
if err != nil {
return err
}
err = txtypes.RegisterTransactionQueryHandlerFromEndpoint(ctx, mux, ":9090", opts)
if err != nil {
return err
}
http.Handle("/", mux)
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
// Serve Swagger UI
http.Handle("/public/", http.StripPrefix("/public/", staticServer))
log.Println("HTTP server listening on :8080")
return http.ListenAndServe(":8080", nil)
}
func main() {
go func() {
if err := runGRPCServer(); err != nil {
log.Fatalf("failed to run gRPC server: %v", err)
}
}()
if err := runHTTPServer(); err != nil {
log.Fatalf("failed to run HTTP server: %v", err)
}
}