Stroika Library 3.0d23x
 
Loading...
Searching...
No Matches
Samples/WebService/Sources/Main.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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"
18
19#include "WSImpl.h"
20#include "WebServer.h"
21
22#include "AppVersion.h"
23
24using namespace std;
25
26using namespace Stroika::Foundation;
28using namespace Stroika::Foundation::Execution;
29
32using Memory::MakeSharedPtr;
33
34using namespace StroikaSample::WebServices;
35
36int main (int argc, const char* argv[])
37{
38 CommandLine cmdLine{argc, argv};
39 Debug::TraceContextBumper ctx{"main", "argv={}"_f, cmdLine};
41#if qStroika_Foundation_Common_Platform_POSIX
43#endif
44 uint16_t portNumber = 8080;
45 Time::DurationSeconds quitAfter = Time::kInfinity;
46
47 const CommandLine::Option kPortO_{.fLongName = "port"sv, .fSupportsArgument = true};
48 const CommandLine::Option kQuitAfterO_{.fLongName = "quit-after"sv, .fSupportsArgument = true};
49
50 try {
51 cmdLine.Validate ({kPortO_, kQuitAfterO_});
52
53 if (auto o = cmdLine.GetArgument (kPortO_)) {
54 portNumber = Characters::String2Int<uint16_t> (*o);
55 }
56 if (auto o = cmdLine.GetArgument (kQuitAfterO_)) {
57 quitAfter = Time::DurationSeconds{Characters::FloatConversion::ToFloat<Time::DurationSeconds::rep> (*o)};
58 }
59
60 WebServer myWebServer{portNumber, MakeSharedPtr<WSImpl> ()}; // listen and dispatch while this object exists
61 WaitableEvent{}.Wait (quitAfter); // wait quitAfter seconds, or til user hits ctrl-c
62 }
63 catch (const TimeOutException&) {
64 cerr << "Timed out - so - exiting..." << endl;
65 return EXIT_SUCCESS;
66 }
67 catch (...) {
68 cerr << "Error encountered: " << Characters::ToString (current_exception ()) << " - terminating..." << endl;
69 return EXIT_FAILURE;
70 }
71 return EXIT_SUCCESS;
72}
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.