Discovery Nats Related
This commit is contained in:
58
main.go
58
main.go
@@ -1,20 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"oc-catalog/conf"
|
||||
"oc-catalog/infrastructure"
|
||||
_ "oc-catalog/routers"
|
||||
|
||||
oclib "cloud.o-forge.io/core/oc-lib"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
"github.com/beego/beego/v2/server/web/filter/cors"
|
||||
"github.com/libp2p/go-libp2p"
|
||||
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
)
|
||||
|
||||
const appname = "oc-catalog"
|
||||
@@ -53,58 +46,7 @@ func main() {
|
||||
ExposeHeaders: []string{"Content-Length", "Content-Type"},
|
||||
AllowCredentials: true,
|
||||
}))
|
||||
InitDTH()
|
||||
infrastructure.Singleton.InitSubscribeEvents(context.Background())
|
||||
beego.Run()
|
||||
}
|
||||
|
||||
func InitDTH() error {
|
||||
priv, err := infrastructure.LoadKeyFromFile(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
psk, err := infrastructure.LoadPSKFromFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
h, err := libp2p.New(
|
||||
libp2p.PrivateNetwork(psk),
|
||||
libp2p.Identity(priv),
|
||||
libp2p.ListenAddrStrings(
|
||||
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", conf.GetConfig().DHTEndpointPort),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pi, err := peer.AddrInfoFromString(conf.GetConfig().BootstrapAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger := oclib.GetLogger()
|
||||
if err := h.Connect(context.Background(), *pi); err != nil {
|
||||
logger.Err(fmt.Errorf("Failed to connect to MAIN bootstrap peer %s: %s", pi.ID, err))
|
||||
} else {
|
||||
logger.Info().Msg(fmt.Sprintf("Connected to MAIN bootstrap peer %s", pi.ID))
|
||||
}
|
||||
|
||||
ps, err := pubsub.NewGossipSub(context.Background(), h,
|
||||
pubsub.WithMessageSigning(true),
|
||||
pubsub.WithStrictSignatureVerification(true),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
infrastructure.Singleton = &infrastructure.PubSubService{
|
||||
PS: ps,
|
||||
Subscription: []string{},
|
||||
SearchStream: make(map[string]chan resources.ResourceInterface),
|
||||
// mutex field is ready without explicit literal
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO VERIFY IF LOOP OF GET VALUE is ENOUGH TO REFRESH
|
||||
|
||||
Reference in New Issue
Block a user