2024-07-26 13:07:25 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-27 15:39:53 +01:00
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"oc-catalog/conf"
|
|
|
|
|
"oc-catalog/infrastructure"
|
2024-07-26 13:07:25 +02:00
|
|
|
_ "oc-catalog/routers"
|
|
|
|
|
|
|
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
2026-01-27 15:39:53 +01:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/resources"
|
2024-10-30 10:51:18 +01:00
|
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
2024-07-26 13:07:25 +02:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
2025-01-15 16:34:41 +01:00
|
|
|
"github.com/beego/beego/v2/server/web/filter/cors"
|
2026-01-27 15:39:53 +01:00
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2024-07-26 13:07:25 +02:00
|
|
|
)
|
|
|
|
|
|
2024-09-04 15:00:43 +02:00
|
|
|
const appname = "oc-catalog"
|
2024-07-26 13:07:25 +02:00
|
|
|
|
|
|
|
|
func main() {
|
2024-09-04 15:30:29 +02:00
|
|
|
// Init the oc-lib
|
2024-09-04 15:00:43 +02:00
|
|
|
oclib.Init(appname)
|
2024-09-04 15:30:29 +02:00
|
|
|
|
|
|
|
|
// Load the right config file
|
2024-09-04 15:00:43 +02:00
|
|
|
o := oclib.GetConfLoader()
|
|
|
|
|
|
2024-09-05 09:22:06 +02:00
|
|
|
// feed the library with the loaded config
|
2024-09-04 15:00:43 +02:00
|
|
|
oclib.SetConfig(
|
2024-07-26 13:07:25 +02:00
|
|
|
o.GetStringDefault("MONGO_URL", "mongodb://127.0.0.1:27017"),
|
|
|
|
|
o.GetStringDefault("MONGO_DATABASE", "DC_myDC"),
|
2024-10-30 10:51:18 +01:00
|
|
|
o.GetStringDefault("NATS_URL", "nats://localhost:4222"),
|
2024-10-02 14:48:59 +02:00
|
|
|
o.GetStringDefault("LOKI_URL", ""),
|
|
|
|
|
o.GetStringDefault("LOG_LEVEL", "info"),
|
2024-07-26 13:07:25 +02:00
|
|
|
)
|
2026-01-27 15:39:53 +01:00
|
|
|
conf.GetConfig().PSKPath = o.GetStringDefault("PSK_PATH", "./psk/psk")
|
|
|
|
|
conf.GetConfig().DHTEndpointPort = o.GetInt64Default("DHT_ENDPOINT_PORT", 4002)
|
|
|
|
|
conf.GetConfig().PublicKeyPath = o.GetStringDefault("CATALOG_PUBLIC_KEY_PATH", "./pem/public.pem")
|
|
|
|
|
conf.GetConfig().PrivateKeyPath = o.GetStringDefault("CATALOG_PRIVATE_KEY_PATH", "./pem/private.pem")
|
2024-10-30 10:51:18 +01:00
|
|
|
// Beego initialization
|
2024-09-04 15:00:43 +02:00
|
|
|
beego.BConfig.AppName = appname
|
2024-10-15 12:13:20 +02:00
|
|
|
beego.BConfig.Listen.HTTPPort = o.GetIntDefault("port", 8080)
|
2024-07-26 13:07:25 +02:00
|
|
|
beego.BConfig.WebConfig.DirectoryIndex = true
|
|
|
|
|
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
|
2024-10-30 10:51:18 +01:00
|
|
|
api := &tools.API{}
|
|
|
|
|
api.Discovered(beego.BeeApp.Handlers.GetAllControllerInfo())
|
2025-01-15 16:34:41 +01:00
|
|
|
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
|
|
|
|
|
AllowAllOrigins: true,
|
|
|
|
|
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
|
|
|
AllowHeaders: []string{"Origin", "Authorization", "Content-Type"},
|
|
|
|
|
ExposeHeaders: []string{"Content-Length", "Content-Type"},
|
|
|
|
|
AllowCredentials: true,
|
|
|
|
|
}))
|
2026-01-27 15:39:53 +01:00
|
|
|
InitDTH()
|
2024-07-26 13:07:25 +02:00
|
|
|
beego.Run()
|
|
|
|
|
}
|
2026-01-27 15:39:53 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|