Move run and redfish to compile units

Meson supports unity builds[1] natively.  There's no reason to continue
with the pseudo unity build we've been using by putting implementations
in header files.

This commit is the first in a long series of starting to break this up
into smaller compile units, in the hopes of dropping incremental compile
times for developers, and reduce the total per-core memory usage that
gcc requires.

This commit breaks out the run() function from main() and the
constructor of RedfishService from redfish.hpp into their own compile
units.  According to tracing, even after broken out, these are still by
far the two longest to compile units in the build.

Tested: Code compiles.  Debug build on a 24 core build server results in
a decrease in compile time for compiling just bmcweb from 1m38s to
1m22s.

[1] https://mesonbuild.com/Unity-builds.html

Change-Id: Ibf352e8aba61d64c9a41a7a76e94ab3b5a0dde4b
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/include/webserver_run.hpp b/include/webserver_run.hpp
new file mode 100644
index 0000000..300d68d
--- /dev/null
+++ b/include/webserver_run.hpp
@@ -0,0 +1,3 @@
+#pragma once
+
+int run();
diff --git a/meson.build b/meson.build
index 9e84fa9..1665946 100644
--- a/meson.build
+++ b/meson.build
@@ -387,11 +387,16 @@
 fs = import('fs')
 
 srcfiles_bmcweb = files(
+  # Begin large files, should be at the beginning
+  'redfish-core/src/redfish.cpp',
+  'src/webserver_run.cpp',
+  # end large files
+
   'redfish-core/src/error_messages.cpp',
   'redfish-core/src/registries.cpp',
   'redfish-core/src/utils/dbus_utils.cpp',
-  'redfish-core/src/utils/time_utils.cpp',
   'redfish-core/src/utils/json_utils.cpp',
+  'redfish-core/src/utils/time_utils.cpp',
   'src/boost_asio_ssl.cpp',
   'src/boost_asio.cpp',
   'src/boost_beast.cpp',
diff --git a/redfish-core/include/redfish.hpp b/redfish-core/include/redfish.hpp
index 89d5679..38a0296 100644
--- a/redfish-core/include/redfish.hpp
+++ b/redfish-core/include/redfish.hpp
@@ -1,62 +1,6 @@
-/*
-// Copyright (c) 2018-2019 Intel Corporation
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-*/
 #pragma once
 
-#include "account_service.hpp"
-#include "aggregation_service.hpp"
-#include "bios.hpp"
-#include "cable.hpp"
-#include "certificate_service.hpp"
-#include "chassis.hpp"
-#include "environment_metrics.hpp"
-#include "ethernet.hpp"
-#include "event_service.hpp"
-#include "eventservice_sse.hpp"
-#include "fabric_adapters.hpp"
-#include "fan.hpp"
-#include "hypervisor_system.hpp"
-#include "log_services.hpp"
-#include "manager_diagnostic_data.hpp"
-#include "managers.hpp"
-#include "memory.hpp"
-#include "message_registries.hpp"
-#include "metric_report.hpp"
-#include "metric_report_definition.hpp"
-#include "network_protocol.hpp"
-#include "pcie.hpp"
-#include "pcie_slots.hpp"
-#include "power.hpp"
-#include "power_subsystem.hpp"
-#include "power_supply.hpp"
-#include "processor.hpp"
-#include "redfish_sessions.hpp"
-#include "redfish_v1.hpp"
-#include "roles.hpp"
-#include "sensors.hpp"
-#include "service_root.hpp"
-#include "storage.hpp"
-#include "systems.hpp"
-#include "task.hpp"
-#include "telemetry_service.hpp"
-#include "thermal.hpp"
-#include "thermal_metrics.hpp"
-#include "thermal_subsystem.hpp"
-#include "trigger.hpp"
-#include "update_service.hpp"
-#include "virtual_media.hpp"
+#include "app.hpp"
 
 namespace redfish
 {
@@ -73,182 +17,7 @@
      *
      * @param[in] app   Crow app on which Redfish will initialize
      */
-    explicit RedfishService(App& app)
-    {
-        requestAccountServiceRoutes(app);
-#ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
-        requestRoutesAggregationService(app);
-        requestRoutesAggregationSourceCollection(app);
-        requestRoutesAggregationSource(app);
-#endif
-        requestRoutesRoles(app);
-        requestRoutesRoleCollection(app);
-        requestRoutesServiceRoot(app);
-        requestRoutesNetworkProtocol(app);
-        requestRoutesSession(app);
-        requestEthernetInterfacesRoutes(app);
-#ifdef BMCWEB_ALLOW_DEPRECATED_POWER_THERMAL
-        requestRoutesThermal(app);
-        requestRoutesPower(app);
-#endif
-#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
-        requestRoutesEnvironmentMetrics(app);
-        requestRoutesPowerSubsystem(app);
-        requestRoutesPowerSupply(app);
-        requestRoutesPowerSupplyCollection(app);
-        requestRoutesThermalMetrics(app);
-        requestRoutesThermalSubsystem(app);
-        requestRoutesFan(app);
-        requestRoutesFanCollection(app);
-#endif
-        requestRoutesManagerCollection(app);
-        requestRoutesManager(app);
-        requestRoutesManagerResetAction(app);
-        requestRoutesManagerResetActionInfo(app);
-        requestRoutesManagerResetToDefaultsAction(app);
-        requestRoutesManagerDiagnosticData(app);
-        requestRoutesChassisCollection(app);
-        requestRoutesChassis(app);
-        requestRoutesChassisResetAction(app);
-        requestRoutesChassisResetActionInfo(app);
-        requestRoutesChassisDrive(app);
-        requestRoutesChassisDriveName(app);
-        requestRoutesUpdateService(app);
-        requestRoutesStorageCollection(app);
-        requestRoutesStorage(app);
-        requestRoutesStorageControllerCollection(app);
-        requestRoutesStorageController(app);
-        requestRoutesDrive(app);
-        requestRoutesCable(app);
-        requestRoutesCableCollection(app);
-#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
-        requestRoutesUpdateServiceActionsSimpleUpdate(app);
-#endif
-        requestRoutesSoftwareInventoryCollection(app);
-        requestRoutesSoftwareInventory(app);
-
-        requestRoutesSystemLogServiceCollection(app);
-        requestRoutesEventLogService(app);
-        requestRoutesPostCodesEntryAdditionalData(app);
-
-        requestRoutesPostCodesLogService(app);
-        requestRoutesPostCodesClear(app);
-        requestRoutesPostCodesEntry(app);
-        requestRoutesPostCodesEntryCollection(app);
-
-#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
-        requestRoutesSystemDumpService(app);
-        requestRoutesSystemDumpEntryCollection(app);
-        requestRoutesSystemDumpEntry(app);
-        requestRoutesSystemDumpCreate(app);
-        requestRoutesSystemDumpClear(app);
-
-        requestRoutesBMCDumpService(app);
-        requestRoutesBMCDumpEntryCollection(app);
-        requestRoutesBMCDumpEntry(app);
-        requestRoutesBMCDumpEntryDownload(app);
-        requestRoutesBMCDumpCreate(app);
-        requestRoutesBMCDumpClear(app);
-
-        requestRoutesFaultLogDumpService(app);
-        requestRoutesFaultLogDumpEntryCollection(app);
-        requestRoutesFaultLogDumpEntry(app);
-        requestRoutesFaultLogDumpClear(app);
-#endif
-
-#ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
-        requestRoutesJournalEventLogEntryCollection(app);
-        requestRoutesJournalEventLogEntry(app);
-        requestRoutesJournalEventLogClear(app);
-#endif
-
-        requestRoutesBMCLogServiceCollection(app);
-#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
-        requestRoutesBMCJournalLogService(app);
-        requestRoutesBMCJournalLogEntryCollection(app);
-        requestRoutesBMCJournalLogEntry(app);
-#endif
-
-#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
-        requestRoutesCrashdumpService(app);
-        requestRoutesCrashdumpEntryCollection(app);
-        requestRoutesCrashdumpEntry(app);
-        requestRoutesCrashdumpFile(app);
-        requestRoutesCrashdumpClear(app);
-        requestRoutesCrashdumpCollect(app);
-#endif // BMCWEB_ENABLE_REDFISH_CPU_LOG
-
-        requestRoutesProcessorCollection(app);
-        requestRoutesProcessor(app);
-        requestRoutesOperatingConfigCollection(app);
-        requestRoutesOperatingConfig(app);
-        requestRoutesMemoryCollection(app);
-        requestRoutesMemory(app);
-
-        requestRoutesSystems(app);
-
-        requestRoutesBiosService(app);
-        requestRoutesBiosReset(app);
-
-#ifdef BMCWEB_ENABLE_VM_NBDPROXY
-        requestNBDVirtualMediaRoutes(app);
-#endif // BMCWEB_ENABLE_VM_NBDPROXY
-
-#ifdef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
-        requestRoutesDBusLogServiceActionsClear(app);
-        requestRoutesDBusEventLogEntryCollection(app);
-        requestRoutesDBusEventLogEntry(app);
-        requestRoutesDBusEventLogEntryDownload(app);
-#endif
-
-#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
-        requestRoutesSystemHostLogger(app);
-        requestRoutesSystemHostLoggerCollection(app);
-        requestRoutesSystemHostLoggerLogEntry(app);
-#endif
-
-        requestRoutesMessageRegistryFileCollection(app);
-        requestRoutesMessageRegistryFile(app);
-        requestRoutesMessageRegistry(app);
-
-        requestRoutesCertificateService(app);
-        requestRoutesHTTPSCertificate(app);
-        requestRoutesLDAPCertificate(app);
-        requestRoutesTrustStoreCertificate(app);
-
-        requestRoutesSystemPCIeFunctionCollection(app);
-        requestRoutesSystemPCIeFunction(app);
-        requestRoutesSystemPCIeDeviceCollection(app);
-        requestRoutesSystemPCIeDevice(app);
-
-        requestRoutesSensorCollection(app);
-        requestRoutesSensor(app);
-
-        requestRoutesTaskMonitor(app);
-        requestRoutesTaskService(app);
-        requestRoutesTaskCollection(app);
-        requestRoutesTask(app);
-        requestRoutesEventService(app);
-        requestRoutesEventServiceSse(app);
-        requestRoutesEventDestinationCollection(app);
-        requestRoutesEventDestination(app);
-        requestRoutesFabricAdapters(app);
-        requestRoutesFabricAdapterCollection(app);
-        requestRoutesSubmitTestEvent(app);
-
-        requestRoutesHypervisorSystems(app);
-
-        requestRoutesTelemetryService(app);
-        requestRoutesMetricReportDefinitionCollection(app);
-        requestRoutesMetricReportDefinition(app);
-        requestRoutesMetricReportCollection(app);
-        requestRoutesMetricReport(app);
-        requestRoutesTriggerCollection(app);
-        requestRoutesTrigger(app);
-
-        // Note, this must be the last route registered
-        requestRoutesRedfish(app);
-    }
+    explicit RedfishService(App& app);
 };
 
 } // namespace redfish
diff --git a/redfish-core/src/redfish.cpp b/redfish-core/src/redfish.cpp
new file mode 100644
index 0000000..58d7ddd
--- /dev/null
+++ b/redfish-core/src/redfish.cpp
@@ -0,0 +1,227 @@
+#include "redfish.hpp"
+
+#include "account_service.hpp"
+#include "aggregation_service.hpp"
+#include "app.hpp"
+#include "bios.hpp"
+#include "cable.hpp"
+#include "certificate_service.hpp"
+#include "chassis.hpp"
+#include "environment_metrics.hpp"
+#include "ethernet.hpp"
+#include "event_service.hpp"
+#include "eventservice_sse.hpp"
+#include "fabric_adapters.hpp"
+#include "fan.hpp"
+#include "hypervisor_system.hpp"
+#include "log_services.hpp"
+#include "manager_diagnostic_data.hpp"
+#include "managers.hpp"
+#include "memory.hpp"
+#include "message_registries.hpp"
+#include "metric_report.hpp"
+#include "metric_report_definition.hpp"
+#include "network_protocol.hpp"
+#include "pcie.hpp"
+#include "pcie_slots.hpp"
+#include "power.hpp"
+#include "power_subsystem.hpp"
+#include "power_supply.hpp"
+#include "processor.hpp"
+#include "redfish_sessions.hpp"
+#include "redfish_v1.hpp"
+#include "roles.hpp"
+#include "sensors.hpp"
+#include "service_root.hpp"
+#include "storage.hpp"
+#include "systems.hpp"
+#include "task.hpp"
+#include "telemetry_service.hpp"
+#include "thermal.hpp"
+#include "thermal_metrics.hpp"
+#include "thermal_subsystem.hpp"
+#include "trigger.hpp"
+#include "update_service.hpp"
+#include "virtual_media.hpp"
+
+namespace redfish
+{
+
+RedfishService::RedfishService(App& app)
+{
+    requestAccountServiceRoutes(app);
+#ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
+    requestRoutesAggregationService(app);
+    requestRoutesAggregationSourceCollection(app);
+    requestRoutesAggregationSource(app);
+#endif
+    requestRoutesRoles(app);
+    requestRoutesRoleCollection(app);
+    requestRoutesServiceRoot(app);
+    requestRoutesNetworkProtocol(app);
+    requestRoutesSession(app);
+    requestEthernetInterfacesRoutes(app);
+#ifdef BMCWEB_ALLOW_DEPRECATED_POWER_THERMAL
+    requestRoutesThermal(app);
+    requestRoutesPower(app);
+#endif
+#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
+    requestRoutesEnvironmentMetrics(app);
+    requestRoutesPowerSubsystem(app);
+    requestRoutesPowerSupply(app);
+    requestRoutesPowerSupplyCollection(app);
+    requestRoutesThermalMetrics(app);
+    requestRoutesThermalSubsystem(app);
+    requestRoutesFan(app);
+    requestRoutesFanCollection(app);
+#endif
+    requestRoutesManagerCollection(app);
+    requestRoutesManager(app);
+    requestRoutesManagerResetAction(app);
+    requestRoutesManagerResetActionInfo(app);
+    requestRoutesManagerResetToDefaultsAction(app);
+    requestRoutesManagerDiagnosticData(app);
+    requestRoutesChassisCollection(app);
+    requestRoutesChassis(app);
+    requestRoutesChassisResetAction(app);
+    requestRoutesChassisResetActionInfo(app);
+    requestRoutesChassisDrive(app);
+    requestRoutesChassisDriveName(app);
+    requestRoutesUpdateService(app);
+    requestRoutesStorageCollection(app);
+    requestRoutesStorage(app);
+    requestRoutesStorageControllerCollection(app);
+    requestRoutesStorageController(app);
+    requestRoutesDrive(app);
+    requestRoutesCable(app);
+    requestRoutesCableCollection(app);
+#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
+    requestRoutesUpdateServiceActionsSimpleUpdate(app);
+#endif
+    requestRoutesSoftwareInventoryCollection(app);
+    requestRoutesSoftwareInventory(app);
+
+    requestRoutesSystemLogServiceCollection(app);
+    requestRoutesEventLogService(app);
+    requestRoutesPostCodesEntryAdditionalData(app);
+
+    requestRoutesPostCodesLogService(app);
+    requestRoutesPostCodesClear(app);
+    requestRoutesPostCodesEntry(app);
+    requestRoutesPostCodesEntryCollection(app);
+
+#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
+    requestRoutesSystemDumpService(app);
+    requestRoutesSystemDumpEntryCollection(app);
+    requestRoutesSystemDumpEntry(app);
+    requestRoutesSystemDumpCreate(app);
+    requestRoutesSystemDumpClear(app);
+
+    requestRoutesBMCDumpService(app);
+    requestRoutesBMCDumpEntryCollection(app);
+    requestRoutesBMCDumpEntry(app);
+    requestRoutesBMCDumpEntryDownload(app);
+    requestRoutesBMCDumpCreate(app);
+    requestRoutesBMCDumpClear(app);
+
+    requestRoutesFaultLogDumpService(app);
+    requestRoutesFaultLogDumpEntryCollection(app);
+    requestRoutesFaultLogDumpEntry(app);
+    requestRoutesFaultLogDumpClear(app);
+#endif
+
+#ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
+    requestRoutesJournalEventLogEntryCollection(app);
+    requestRoutesJournalEventLogEntry(app);
+    requestRoutesJournalEventLogClear(app);
+#endif
+
+    requestRoutesBMCLogServiceCollection(app);
+#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
+    requestRoutesBMCJournalLogService(app);
+    requestRoutesBMCJournalLogEntryCollection(app);
+    requestRoutesBMCJournalLogEntry(app);
+#endif
+
+#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
+    requestRoutesCrashdumpService(app);
+    requestRoutesCrashdumpEntryCollection(app);
+    requestRoutesCrashdumpEntry(app);
+    requestRoutesCrashdumpFile(app);
+    requestRoutesCrashdumpClear(app);
+    requestRoutesCrashdumpCollect(app);
+#endif // BMCWEB_ENABLE_REDFISH_CPU_LOG
+
+    requestRoutesProcessorCollection(app);
+    requestRoutesProcessor(app);
+    requestRoutesOperatingConfigCollection(app);
+    requestRoutesOperatingConfig(app);
+    requestRoutesMemoryCollection(app);
+    requestRoutesMemory(app);
+
+    requestRoutesSystems(app);
+
+    requestRoutesBiosService(app);
+    requestRoutesBiosReset(app);
+
+#ifdef BMCWEB_ENABLE_VM_NBDPROXY
+    requestNBDVirtualMediaRoutes(app);
+#endif // BMCWEB_ENABLE_VM_NBDPROXY
+
+#ifdef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
+    requestRoutesDBusLogServiceActionsClear(app);
+    requestRoutesDBusEventLogEntryCollection(app);
+    requestRoutesDBusEventLogEntry(app);
+    requestRoutesDBusEventLogEntryDownload(app);
+#endif
+
+#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
+    requestRoutesSystemHostLogger(app);
+    requestRoutesSystemHostLoggerCollection(app);
+    requestRoutesSystemHostLoggerLogEntry(app);
+#endif
+
+    requestRoutesMessageRegistryFileCollection(app);
+    requestRoutesMessageRegistryFile(app);
+    requestRoutesMessageRegistry(app);
+
+    requestRoutesCertificateService(app);
+    requestRoutesHTTPSCertificate(app);
+    requestRoutesLDAPCertificate(app);
+    requestRoutesTrustStoreCertificate(app);
+
+    requestRoutesSystemPCIeFunctionCollection(app);
+    requestRoutesSystemPCIeFunction(app);
+    requestRoutesSystemPCIeDeviceCollection(app);
+    requestRoutesSystemPCIeDevice(app);
+
+    requestRoutesSensorCollection(app);
+    requestRoutesSensor(app);
+
+    requestRoutesTaskMonitor(app);
+    requestRoutesTaskService(app);
+    requestRoutesTaskCollection(app);
+    requestRoutesTask(app);
+    requestRoutesEventService(app);
+    requestRoutesEventServiceSse(app);
+    requestRoutesEventDestinationCollection(app);
+    requestRoutesEventDestination(app);
+    requestRoutesFabricAdapters(app);
+    requestRoutesFabricAdapterCollection(app);
+    requestRoutesSubmitTestEvent(app);
+
+    requestRoutesHypervisorSystems(app);
+
+    requestRoutesTelemetryService(app);
+    requestRoutesMetricReportDefinitionCollection(app);
+    requestRoutesMetricReportDefinition(app);
+    requestRoutesMetricReportCollection(app);
+    requestRoutesMetricReport(app);
+    requestRoutesTriggerCollection(app);
+    requestRoutesTrigger(app);
+
+    // Note, this must be the last route registered
+    requestRoutesRedfish(app);
+}
+
+} // namespace redfish
diff --git a/src/webserver_main.cpp b/src/webserver_main.cpp
index cc8e1bc..ee752b0 100644
--- a/src/webserver_main.cpp
+++ b/src/webserver_main.cpp
@@ -1,159 +1,12 @@
-#include "bmcweb_config.h"
 
-#include "app.hpp"
-#include "cors_preflight.hpp"
-#include "dbus_monitor.hpp"
-#include "dbus_singleton.hpp"
-#include "event_service_manager.hpp"
-#include "google/google_service_root.hpp"
-#include "hostname_monitor.hpp"
-#include "ibm/management_console_rest.hpp"
-#include "image_upload.hpp"
-#include "kvm_websocket.hpp"
-#include "login_routes.hpp"
-#include "nbd_proxy.hpp"
-#include "obmc_console.hpp"
-#include "openbmc_dbus_rest.hpp"
-#include "redfish.hpp"
-#include "redfish_aggregator.hpp"
-#include "security_headers.hpp"
-#include "ssl_key_handler.hpp"
-#include "user_monitor.hpp"
-#include "vm_websocket.hpp"
-#include "webassets.hpp"
 
-#include <systemd/sd-daemon.h>
-
-#include <boost/asio/io_context.hpp>
-#include <sdbusplus/asio/connection.hpp>
-#include <sdbusplus/bus.hpp>
-#include <sdbusplus/server.hpp>
+#include "logging.hpp"
+#include "webserver_run.hpp"
 
 #include <exception>
 #include <memory>
 #include <string>
 
-constexpr int defaultPort = 18080;
-
-inline void setupSocket(crow::App& app)
-{
-    int listenFd = sd_listen_fds(0);
-    if (1 == listenFd)
-    {
-        BMCWEB_LOG_INFO("attempting systemd socket activation");
-        if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
-                              0) != 0)
-        {
-            BMCWEB_LOG_INFO("Starting webserver on socket handle {}",
-                            SD_LISTEN_FDS_START);
-            app.socket(SD_LISTEN_FDS_START);
-        }
-        else
-        {
-            BMCWEB_LOG_INFO(
-                "bad incoming socket, starting webserver on port {}",
-                defaultPort);
-            app.port(defaultPort);
-        }
-    }
-    else
-    {
-        BMCWEB_LOG_INFO("Starting webserver on port {}", defaultPort);
-        app.port(defaultPort);
-    }
-}
-
-static int run()
-{
-    auto io = std::make_shared<boost::asio::io_context>();
-    App app(io);
-
-    sdbusplus::asio::connection systemBus(*io);
-    crow::connections::systemBus = &systemBus;
-
-    // Static assets need to be initialized before Authorization, because auth
-    // needs to build the whitelist from the static routes
-
-#ifdef BMCWEB_ENABLE_STATIC_HOSTING
-    crow::webassets::requestRoutes(app);
-#endif
-
-#ifdef BMCWEB_ENABLE_KVM
-    crow::obmc_kvm::requestRoutes(app);
-#endif
-
-#ifdef BMCWEB_ENABLE_REDFISH
-    redfish::RedfishService redfish(app);
-
-    // Create EventServiceManager instance and initialize Config
-    redfish::EventServiceManager::getInstance(&*io);
-
-#ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
-    // Create RedfishAggregator instance and initialize Config
-    redfish::RedfishAggregator::getInstance(&*io);
-#endif
-#endif
-
-#ifdef BMCWEB_ENABLE_DBUS_REST
-    crow::dbus_monitor::requestRoutes(app);
-    crow::image_upload::requestRoutes(app);
-    crow::openbmc_mapper::requestRoutes(app);
-#endif
-
-#ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
-    crow::obmc_console::requestRoutes(app);
-#endif
-
-#ifdef BMCWEB_ENABLE_VM_WEBSOCKET
-    crow::obmc_vm::requestRoutes(app);
-#endif
-
-#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
-    crow::ibm_mc::requestRoutes(app);
-    crow::ibm_mc_lock::Lock::getInstance();
-#endif
-
-#ifdef BMCWEB_ENABLE_GOOGLE_API
-    crow::google_api::requestRoutes(app);
-#endif
-
-    if (bmcwebInsecureDisableXssPrevention != 0)
-    {
-        cors_preflight::requestRoutes(app);
-    }
-
-    crow::login_routes::requestRoutes(app);
-
-    setupSocket(app);
-
-#ifdef BMCWEB_ENABLE_VM_NBDPROXY
-    crow::nbd_proxy::requestRoutes(app);
-#endif
-
-#ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
-    int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
-    if (rc != 0)
-    {
-        BMCWEB_LOG_ERROR("Redfish event handler setup failed...");
-        return rc;
-    }
-#endif
-
-#ifdef BMCWEB_ENABLE_SSL
-    BMCWEB_LOG_INFO("Start Hostname Monitor Service...");
-    crow::hostname_monitor::registerHostnameSignal();
-#endif
-
-    bmcweb::registerUserRemovedSignal();
-
-    app.run();
-    io->run();
-
-    crow::connections::systemBus = nullptr;
-
-    return 0;
-}
-
 int main(int /*argc*/, char** /*argv*/)
 {
     try
diff --git a/src/webserver_run.cpp b/src/webserver_run.cpp
new file mode 100644
index 0000000..e5d272e
--- /dev/null
+++ b/src/webserver_run.cpp
@@ -0,0 +1,153 @@
+#include "webserver_run.hpp"
+
+#include "bmcweb_config.h"
+
+#include "app.hpp"
+#include "cors_preflight.hpp"
+#include "dbus_monitor.hpp"
+#include "dbus_singleton.hpp"
+#include "event_service_manager.hpp"
+#include "google/google_service_root.hpp"
+#include "hostname_monitor.hpp"
+#include "ibm/management_console_rest.hpp"
+#include "image_upload.hpp"
+#include "kvm_websocket.hpp"
+#include "login_routes.hpp"
+#include "nbd_proxy.hpp"
+#include "obmc_console.hpp"
+#include "openbmc_dbus_rest.hpp"
+#include "redfish.hpp"
+#include "redfish_aggregator.hpp"
+#include "security_headers.hpp"
+#include "ssl_key_handler.hpp"
+#include "user_monitor.hpp"
+#include "vm_websocket.hpp"
+#include "webassets.hpp"
+
+#include <systemd/sd-daemon.h>
+
+#include <boost/asio/io_context.hpp>
+#include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server.hpp>
+
+constexpr int defaultPort = 18080;
+
+static void setupSocket(crow::App& app)
+{
+    int listenFd = sd_listen_fds(0);
+    if (1 == listenFd)
+    {
+        BMCWEB_LOG_INFO("attempting systemd socket activation");
+        if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
+                              0) != 0)
+        {
+            BMCWEB_LOG_INFO("Starting webserver on socket handle {}",
+                            SD_LISTEN_FDS_START);
+            app.socket(SD_LISTEN_FDS_START);
+        }
+        else
+        {
+            BMCWEB_LOG_INFO(
+                "bad incoming socket, starting webserver on port {}",
+                defaultPort);
+            app.port(defaultPort);
+        }
+    }
+    else
+    {
+        BMCWEB_LOG_INFO("Starting webserver on port {}", defaultPort);
+        app.port(defaultPort);
+    }
+}
+
+int run()
+{
+    auto io = std::make_shared<boost::asio::io_context>();
+    App app(io);
+
+    sdbusplus::asio::connection systemBus(*io);
+    crow::connections::systemBus = &systemBus;
+
+    // Static assets need to be initialized before Authorization, because auth
+    // needs to build the whitelist from the static routes
+
+#ifdef BMCWEB_ENABLE_STATIC_HOSTING
+    crow::webassets::requestRoutes(app);
+#endif
+
+#ifdef BMCWEB_ENABLE_KVM
+    crow::obmc_kvm::requestRoutes(app);
+#endif
+
+#ifdef BMCWEB_ENABLE_REDFISH
+    redfish::RedfishService redfish(app);
+
+    // Create EventServiceManager instance and initialize Config
+    redfish::EventServiceManager::getInstance(&*io);
+
+#ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
+    // Create RedfishAggregator instance and initialize Config
+    redfish::RedfishAggregator::getInstance(&*io);
+#endif
+#endif
+
+#ifdef BMCWEB_ENABLE_DBUS_REST
+    crow::dbus_monitor::requestRoutes(app);
+    crow::image_upload::requestRoutes(app);
+    crow::openbmc_mapper::requestRoutes(app);
+#endif
+
+#ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
+    crow::obmc_console::requestRoutes(app);
+#endif
+
+#ifdef BMCWEB_ENABLE_VM_WEBSOCKET
+    crow::obmc_vm::requestRoutes(app);
+#endif
+
+#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
+    crow::ibm_mc::requestRoutes(app);
+    crow::ibm_mc_lock::Lock::getInstance();
+#endif
+
+#ifdef BMCWEB_ENABLE_GOOGLE_API
+    crow::google_api::requestRoutes(app);
+#endif
+
+    if (bmcwebInsecureDisableXssPrevention != 0)
+    {
+        cors_preflight::requestRoutes(app);
+    }
+
+    crow::login_routes::requestRoutes(app);
+
+    setupSocket(app);
+
+#ifdef BMCWEB_ENABLE_VM_NBDPROXY
+    crow::nbd_proxy::requestRoutes(app);
+#endif
+
+#ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
+    int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
+    if (rc != 0)
+    {
+        BMCWEB_LOG_ERROR("Redfish event handler setup failed...");
+        return rc;
+    }
+#endif
+
+#ifdef BMCWEB_ENABLE_SSL
+    BMCWEB_LOG_INFO("Start Hostname Monitor Service...");
+    crow::hostname_monitor::registerHostnameSignal();
+#endif
+
+    bmcweb::registerUserRemovedSignal();
+
+    app.run();
+    io->run();
+
+    crow::connections::systemBus = nullptr;
+
+    return 0;
+}