winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ServerCommon.h (2007B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 
      4 #pragma once
      5 
      6 #include "Logging.h"
      7 #include "Result.h"
      8 
      9 #include <httplib.h>
     10 
     11 #include <chrono>
     12 #include <optional>
     13 #include <string>
     14 
     15 namespace SFS::test
     16 {
     17 class StatusCodeException : public std::exception
     18 {
     19   public:
     20     StatusCodeException(httplib::StatusCode status);
     21 
     22     const char* what() const noexcept override;
     23 
     24     httplib::StatusCode GetStatusCode() const;
     25 
     26   private:
     27     httplib::StatusCode m_status;
     28     std::string m_message;
     29 };
     30 
     31 #define BUILD_BUFFERED_LOG_DATA(message) BuildBufferedLogData(message, __FILE__, __LINE__, __FUNCTION__)
     32 
     33 #define BUFFER_LOG(message) BufferLog(BUILD_BUFFERED_LOG_DATA(message))
     34 
     35 struct BufferedLogData
     36 {
     37     std::string message;
     38     std::string file;
     39     unsigned line;
     40     std::string function;
     41     std::chrono::time_point<std::chrono::system_clock> time;
     42 };
     43 
     44 namespace details
     45 {
     46 class BaseServerImpl
     47 {
     48   public:
     49     BaseServerImpl() = default;
     50     ~BaseServerImpl() = default;
     51 
     52     BaseServerImpl(const BaseServerImpl&) = delete;
     53     BaseServerImpl& operator=(const BaseServerImpl&) = delete;
     54 
     55     void Start();
     56     Result Stop();
     57 
     58     std::string GetUrl() const;
     59 
     60   protected:
     61     void ConfigureServerSettings();
     62     virtual void ConfigureRequestHandlers() = 0;
     63 
     64     virtual std::string GetLogIdentifier() = 0;
     65 
     66     void BufferLog(const BufferedLogData& data);
     67     BufferedLogData BuildBufferedLogData(const std::string& message,
     68                                          const char* file,
     69                                          unsigned line,
     70                                          const char* function);
     71     void ProcessBufferedLogs();
     72 
     73     httplib::Server m_server;
     74     int m_port{-1};
     75 
     76     std::optional<Result> m_lastException;
     77 
     78     std::thread m_listenerThread;
     79 
     80     std::vector<BufferedLogData> m_bufferedLog;
     81     std::mutex m_logMutex;
     82 };
     83 } // namespace details
     84 } // namespace SFS::test