Stroika Library 3.0d25
 
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/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;
27using namespace Stroika::Foundation::Execution;
28
31using Memory::MakeSharedPtr;
32
33using namespace StroikaSample::WebServices;
34
35int main (int argc, const char* argv[])
36{
37 CommandLine cmdLine{argc, argv};
38 Debug::TraceContextBumper ctx{"main", "argv={}"_f, cmdLine};
40#if qStroika_Platform_POSIX
42#endif
43 uint16_t portNumber = 8080;
44 Time::DurationSeconds quitAfter = Time::kInfinity;
45
46 const CommandLine::Option kPortO_{.fLongName = "port"sv, .fSupportsArgument = true};
47 const CommandLine::Option kQuitAfterO_{.fLongName = "quit-after"sv, .fSupportsArgument = true};
48
49 try {
50 cmdLine.Validate ({kPortO_, kQuitAfterO_});
51
52 if (auto o = cmdLine.GetArgument (kPortO_)) {
53 portNumber = Characters::String2Int<uint16_t> (*o);
54 }
55 if (auto o = cmdLine.GetArgument (kQuitAfterO_)) {
56 quitAfter = Time::DurationSeconds{Characters::FloatConversion::ToFloat<Time::DurationSeconds::rep> (*o)};
57 }
58
59 WebServer myWebServer{portNumber, MakeSharedPtr<WSImpl> ()}; // listen and dispatch while this object exists
60 WaitableEvent{}.Wait (quitAfter); // wait quitAfter seconds, or til user hits ctrl-c
61 }
62 catch (const system_error& e) {
63 if (Execution::IsA (e, errc::timed_out)) {
64 cerr << "Timed out - so - exiting..." << endl;
65 return EXIT_SUCCESS;
66 }
67 cerr << "Exception - " << Characters::ToString (e) << " - terminating..." << endl;
68 return EXIT_FAILURE;
69 }
70 catch (...) {
71 cerr << "Error encountered: " << Characters::ToString (current_exception ()) << " - terminating..." << endl;
72 return EXIT_FAILURE;
73 }
74 return EXIT_SUCCESS;
75}
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) - a F...
STL namespace.