const std = @import("std"); const endpoints = @import("endpoints"); pub const ctx = @import("context"); const sec = @import("security"); const services = @import("services"); const HttpService = services.HttpService; const server = @import("server.zig"); const PaymentIntegrator = @import("payment_integrator.zig").PaymentIntegrator; const lb = @import("load_balancer.zig"); pub fn main() !void { std.debug.print("Init context\n", .{}); try ctx.initConfig(); std.debug.print("Context: OK\n", .{}); if (ctx.config.isLoadBalancer()) { try startLoadBalancer(); } else { try startServer(); } } fn startServer() !void { try ctx.intiRepository(100_000); std.debug.print("Starting server...\n", .{}); var get_endpoints = endpoints.EndpointsManager{}; var post_endpoints = endpoints.EndpointsManager{}; var put_endpoints = endpoints.EndpointsManager{}; sec.updateToken(ctx.config.initial_token); try endpoints.payments.registerEndpoints(&get_endpoints, &post_endpoints); std.debug.print("Endpoints: OK\n", .{}); std.debug.print("default_pool: {d}\n", .{ctx.config.default_pool}); std.debug.print("fallback_pool: {d}\n", .{ctx.config.fallback_pool}); var default_service = try HttpService.init(1, "default", ctx.config.default_host, ctx.config.default_port, ctx.config.default_pool, std.heap.page_allocator); var fallback_service = try HttpService.init(2, "fallback", ctx.config.fallback_host, ctx.config.fallback_port, ctx.config.fallback_pool, std.heap.page_allocator); var service_pool = services.ServicePool{}; defer default_service.deinit(); defer fallback_service.deinit(); try service_pool.add(&default_service); try service_pool.add(&fallback_service); var integrator = PaymentIntegrator.init(&service_pool); try default_service.start(); try fallback_service.start(); try integrator.start(); std.debug.print("Service And Integrator: OK\n", .{}); ctx.payments_repository.subscribeInsert(&PaymentIntegrator.newPaymentEvent); var ip_map: []const u8 = "127.0.0.1"; if (ctx.config.env == .PROD) ip_map = "0.0.0.0"; std.log.info("Server Enviroment: {}\n", .{ctx.config.env}); var myserver = try server.HttpServer.init(ip_map, ctx.config.port, &get_endpoints, &post_endpoints, &put_endpoints); std.Thread.sleep(1_000_000 * 1000 * 1); try myserver.start(); } fn startLoadBalancer() !void { std.Thread.sleep(1_000_000 * 1000 * 2); std.debug.print("Starting load balancer...\n", .{}); var ip_map: []const u8 = "127.0.0.1"; if (ctx.config.env == .PROD) ip_map = "0.0.0.0"; std.log.info("LB Enviroment: {}\n", .{ctx.config.env}); const server1 = lb.UpstreamServer.init(ctx.config.default_host, ctx.config.default_port); const server2 = lb.UpstreamServer.init(ctx.config.fallback_host, ctx.config.fallback_port); var servers = [_]lb.UpstreamServer{ server1, server2 }; var mylb = try lb.LoadBalancer.init(ip_map, ctx.config.port, &servers); try mylb.start(); } test { std.testing.refAllDecls(@This()); }