Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Samples/WebService/Sources/Main.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
6#include <iostream>
7
8#include "Stroika/Foundation/Characters/String2Int.h"
10#include "Stroika/Foundation/Containers/Sequence.h"
12#include "Stroika/Foundation/Execution/CommandLine.h"
13#include "Stroika/Foundation/Execution/Module.h"
14#include "Stroika/Foundation/Execution/SignalHandlers.h"
15#include "Stroika/Foundation/Execution/TimeOutException.h"
16#include "Stroika/Foundation/Execution/WaitableEvent.h"
17
18#include "WSImpl.h"
19#include "WebServer.h"
20
21#include "AppVersion.h"
22
23using namespace std;
24
25using namespace Stroika::Foundation;
27
30
31using namespace StroikaSample::WebServices;
32
33int main (int argc, const char* argv[])
34{
35 Execution::CommandLine cmdLine{argc, argv};
36 Debug::TraceContextBumper ctx{"main", "argv={}"_f, cmdLine};
38#if qStroika_Foundation_Common_Platform_POSIX
40#endif
41 uint16_t portNumber = 8080;
42 Time::DurationSeconds quitAfter = Time::kInfinity;
43
44 const Execution::CommandLine::Option kPortO_{.fLongName = "port"sv, .fSupportsArgument = true};
45 const Execution::CommandLine::Option kQuitAfterO_{.fLongName = "quit-after"sv, .fSupportsArgument = true};
46
47 try {
48 cmdLine.Validate ({kPortO_, kQuitAfterO_});
49
50 if (auto o = cmdLine.GetArgument (kPortO_)) {
51 portNumber = Characters::String2Int<uint16_t> (*o);
52 }
53 if (auto o = cmdLine.GetArgument (kQuitAfterO_)) {
54 quitAfter = Time::DurationSeconds{Characters::FloatConversion::ToFloat<Time::DurationSeconds::rep> (*o)};
55 }
56
57 WebServer myWebServer{portNumber, make_shared<WSImpl> ()}; // listen and dispatch while this object exists
58 Execution::WaitableEvent{}.Wait (quitAfter); // wait quitAfter seconds, or til user hits ctrl-c
59 }
60 catch (const Execution::TimeOutException&) {
61 cerr << "Timed out - so - exiting..." << endl;
62 return EXIT_SUCCESS;
63 }
64 catch (...) {
65 cerr << "Error encountered: " << Characters::ToString (current_exception ()).AsNarrowSDKString () << " - terminating..." << endl;
66 return EXIT_FAILURE;
67 }
68 return EXIT_SUCCESS;
69}
chrono::duration< double > DurationSeconds
chrono::duration<double> - a time span (length of time) measured in seconds, but high precision.
Definition Realtime.h:57
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
nonvirtual void Wait(Time::DurationSeconds timeout=Time::kInfinity)
Create a format-string (see std::wformat_string or Stroika FormatString, or python 'f' strings.
STL namespace.