-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.go
executable file
·78 lines (62 loc) · 1.9 KB
/
daemon.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
package daemon
import (
"fmt"
"github.com/Encedeus/module-daemon-go/module"
"github.com/filecoin-project/go-jsonrpc"
"github.com/labstack/echo/v4"
"github.com/stealthrocket/net/wasip1"
"log"
"net/http"
"os"
"strconv"
"time"
)
func InitModule(run module.RunFunction) {
rpcPort, _ := strconv.Atoi(os.Getenv("MODULE_RPC_PORT"))
mainPort, _ := strconv.Atoi(os.Getenv("MODULE_MAIN_PORT"))
mod := new(module.Module)
//go SetupEchoServer(mod)
InitRPCServer(module.Port(rpcPort), module.Port(mainPort), mod, run)
}
func SetupEchoServer(mod *module.Module) {
e := echo.New()
listener, err := wasip1.Listen("tcp", fmt.Sprintf("127.0.0.1:%v", mod.Port))
if err != nil {
log.Fatal(err)
}
e.Listener = listener
mod.Echo = e
err = module.StartCraterEchoServer(mod)
if err != nil {
log.Fatal(err)
}
}
func InitRPCServer(rpcPort, mainPort module.Port, mod *module.Module, run module.RunFunction) {
rpcServer := jsonrpc.NewServer()
handshakeHandler := new(module.HandshakeHandler)
handshakeHandler.RPCPort = rpcPort
handshakeHandler.MainPort = mainPort
handshakeHandler.Module = mod
handshakeHandler.Run = run
hostInvokeHandler := new(module.HostInvokeHandler)
hostInvokeHandler.Module = mod
cratersHandler := new(module.CraterHandler)
cratersHandler.RegisteredCraters = &mod.Craters
rpcServer.Register("HandshakeHandler", handshakeHandler)
rpcServer.Register("HostInvokeHandler", hostInvokeHandler)
rpcServer.Register("CratersHandler", cratersHandler)
rpcListener, err := wasip1.Listen("tcp", fmt.Sprintf("127.0.0.1:%v", rpcPort))
if err != nil {
log.Fatalf("Failed creating TCP rpcListener: %v", err)
}
server := http.Server{
Handler: rpcServer,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
Addr: fmt.Sprintf(":%v", rpcPort),
}
fmt.Println("Starting")
if err = server.Serve(rpcListener); err != nil {
log.Fatalf("Failed starting RPC server: %v", err)
}
}