2023-09-04 11:13:21 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2023-09-05 11:37:03 +02:00
|
|
|
"log"
|
2023-09-04 11:13:21 +02:00
|
|
|
"net/http"
|
2023-09-11 08:08:24 +02:00
|
|
|
|
|
|
|
|
"gitlab.com/pactual1/backend/config"
|
|
|
|
|
"gitlab.com/pactual1/backend/models"
|
|
|
|
|
"gitlab.com/pactual1/backend/routes"
|
2023-09-21 11:59:08 +02:00
|
|
|
"gitlab.com/pactual1/backend/services/blockchain"
|
2023-09-18 12:27:40 +02:00
|
|
|
"gitlab.com/pactual1/backend/services/contract"
|
2023-09-11 12:01:30 +02:00
|
|
|
"gitlab.com/pactual1/backend/services/erp"
|
|
|
|
|
"gitlab.com/pactual1/backend/services/messaging"
|
2023-09-11 08:08:24 +02:00
|
|
|
"gitlab.com/pactual1/backend/shared"
|
2023-09-04 11:13:21 +02:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
"github.com/qor/admin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var DB *gorm.DB
|
|
|
|
|
|
|
|
|
|
func main() {
|
2023-09-05 11:37:03 +02:00
|
|
|
|
|
|
|
|
// LOAD APPLICATION CONFIGURATION
|
|
|
|
|
err := config.Load()
|
2023-09-04 11:13:21 +02:00
|
|
|
if err != nil {
|
2023-09-05 11:37:03 +02:00
|
|
|
log.Fatal(err)
|
2023-09-04 11:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-05 11:37:03 +02:00
|
|
|
// Db Connect and Close
|
2023-09-07 13:04:34 +02:00
|
|
|
if err := shared.Init(); err != nil {
|
|
|
|
|
log.Fatalf("Failed to initialize shared resources: %v", err)
|
2023-09-06 11:58:33 +02:00
|
|
|
}
|
2023-09-05 11:37:03 +02:00
|
|
|
defer shared.CloseDb()
|
2023-09-04 11:13:21 +02:00
|
|
|
|
|
|
|
|
// Initialize Admin interface
|
2023-09-05 11:37:03 +02:00
|
|
|
Admin := admin.New(&admin.AdminConfig{DB: shared.GetDb()})
|
2023-09-07 13:04:34 +02:00
|
|
|
Admin.RegisterViewPath("app/views/qor")
|
2023-09-06 11:58:33 +02:00
|
|
|
fmt.Printf("Admin instance: %+v\n", Admin)
|
2023-09-04 11:13:21 +02:00
|
|
|
// Allow Admin to manage User resource
|
2023-09-06 11:58:33 +02:00
|
|
|
company := Admin.AddResource(&models.Company{})
|
|
|
|
|
company.Meta(&admin.Meta{Name: "Users", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
|
|
|
|
company.Meta(&admin.Meta{Name: "Devices", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
2023-09-12 18:28:18 +02:00
|
|
|
// company.Meta(&admin.Meta{Name: "DeviceInfos", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
2023-09-18 12:27:40 +02:00
|
|
|
|
2023-09-06 11:58:33 +02:00
|
|
|
// Add User and Device resources
|
2023-09-04 11:13:21 +02:00
|
|
|
Admin.AddResource(&models.User{})
|
2023-09-06 11:58:33 +02:00
|
|
|
Admin.AddResource(&models.Device{})
|
2023-10-02 12:59:19 +02:00
|
|
|
Admin.AddResource(&models.Buyer{})
|
|
|
|
|
Admin.AddResource(&models.ProductTemplate{})
|
2023-09-04 11:13:21 +02:00
|
|
|
// Initialize HTTP request multiplexer
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
// Mount admin interface to mux
|
|
|
|
|
Admin.MountTo("/admin", mux)
|
|
|
|
|
|
|
|
|
|
// Start the admin server in a separate goroutine
|
|
|
|
|
go func() {
|
2023-09-18 12:27:40 +02:00
|
|
|
port := config.AppConfig.Service.Port
|
2023-09-05 11:37:03 +02:00
|
|
|
fmt.Println("Admin server listening on :" + port)
|
2023-09-18 12:27:40 +02:00
|
|
|
http.ListenAndServe(":"+port, mux)
|
2023-09-04 11:13:21 +02:00
|
|
|
}()
|
|
|
|
|
|
2023-09-11 12:01:30 +02:00
|
|
|
// Initialize channels
|
|
|
|
|
messagingChannel := make(chan string)
|
|
|
|
|
erpChannel := make(chan string)
|
2023-09-18 12:27:40 +02:00
|
|
|
contractChannel := make(chan string)
|
2023-09-11 12:01:30 +02:00
|
|
|
|
|
|
|
|
// Start services and pass the respective channels
|
|
|
|
|
go messaging.MessagingService(messagingChannel)
|
|
|
|
|
go erp.ERPService(erpChannel)
|
2023-09-18 12:27:40 +02:00
|
|
|
|
|
|
|
|
encryptionClient := shared.NewEncryptionClient(config.AppConfig.Service.BlockchainSecret)
|
2023-09-21 11:59:08 +02:00
|
|
|
blockchainClient := blockchain.NewService(config.AppConfig.Blockchain)
|
|
|
|
|
contractService := contract.NewService(contractChannel, shared.GetDb(), encryptionClient, blockchainClient)
|
2023-09-18 12:27:40 +02:00
|
|
|
go contractService.ContractService()
|
2023-09-11 12:01:30 +02:00
|
|
|
|
|
|
|
|
// Sending messages via channels
|
|
|
|
|
messagingChannel <- "Send an email"
|
|
|
|
|
erpChannel <- "Update ERP record"
|
2023-09-18 12:27:40 +02:00
|
|
|
contractChannel <- "Update contract info"
|
2023-09-11 12:01:30 +02:00
|
|
|
|
2023-09-04 11:13:21 +02:00
|
|
|
// Initialize Gin
|
|
|
|
|
r := gin.Default()
|
|
|
|
|
routes.InitRouter(r)
|
|
|
|
|
// Start the Gin server on another port
|
2023-09-18 12:27:40 +02:00
|
|
|
port := config.AppConfig.AdminService.Port
|
2023-09-05 11:37:03 +02:00
|
|
|
fmt.Println("Application server listening on :" + port)
|
|
|
|
|
r.Run(":" + port)
|
2023-09-04 11:13:21 +02:00
|
|
|
|
|
|
|
|
}
|