func Run()

in internal/app/synapse/synapse.go [41:111]


func Run(ctx context.Context) error {

	start := time.Now()
	PrintWelcomeMessage()

	var wg sync.WaitGroup
	ctx, cancel := context.WithCancel(ctx)
	ctx = context.WithValue(ctx, utils.WaitGroupKey, &wg)
	defer cancel()

	// create instace variables

	// Adding config context to the GO context
	conCtx := artifacts.GetConfigContext()
	ctx = context.WithValue(ctx, utils.ConfigContextKey, conCtx)

	exePath, err := os.Executable()
	if err != nil {
		log.Fatalf("Error getting executable path: %s", err.Error())
	}

	binDir := filepath.Dir(exePath)
	confPath := filepath.Join(binDir, "..", "conf")

	errConfig := config.InitializeConfig(ctx, confPath)
	if errConfig != nil {
		log.Fatalf("Initialization error: %s", errConfig.Error())
	}

	mediationEngine := mediation.NewMediationEngine()

	// Define default port
	httpServerPort := 8290
	var hostname string
	if serverConfig, ok := conCtx.DeploymentConfig["server"].(map[string]string); ok {
		hostname = serverConfig["hostname"]
		if offsetStr, offsetExists := serverConfig["offset"]; offsetExists {
			if offsetInt, err := strconv.Atoi(offsetStr); err == nil {
				httpServerPort += offsetInt
				log.Printf("Using port offset: %d, final port: %d", offsetInt, httpServerPort)
			} else {
				log.Printf("Warning: Invalid offset value '%s', using default port", offsetStr)
			}
		}
	}

	// Convert the port to a string format expected by the HTTP server
	listenPort := fmt.Sprintf(":%d", httpServerPort)

	// Initialize the router service with the calculated port
	routerService := router.NewRouterService(listenPort, hostname)

	artifactsPath := filepath.Join(binDir, "..", "artifacts")
	deployer := deployers.NewDeployer(artifactsPath, mediationEngine, routerService)
	err = deployer.Deploy(ctx)
	if err != nil {
		log.Printf("Error deploying artifacts: %v", err)
	}

	// Start HTTP Server
	routerService.StartServer(ctx)

	elapsed := time.Since(start)
	log.Printf("Server started in: %v", elapsed)

	<-ctx.Done()
	wg.Wait()
	routerService.StopServer()
	log.Println("HTTP server shutdown gracefully")
	return nil
}