MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / saneMain

Function saneMain

Examples/ApiServer/ApiServer.cpp:136–186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

134};
135
136Result saneMain(Span<const StringSpan> args)
137{
138 Console::tryAttachingToParentConsole();
139 Console console;
140
141 static ApiServerExample sample;
142 uint16_t port = static_cast<uint16_t>(sample.port);
143
144 CommandLineOption options[1];
145 options[0].longName = "port";
146 options[0].help = "Port to listen on";
147 options[0].valueName = "PORT";
148 options[0].shortName = 'p';
149 options[0].value = CommandLineValue::uint16(port);
150
151 CommandLineSpec spec;
152 spec.programName = "ApiServer";
153 spec.summary = "A small async JSON/text API server example.";
154 spec.options = options;
155
156 const CommandLineParseResult parseResult = spec.parse(args);
157 if (parseResult.status == CommandLineParseResult::Status::HelpRequested)
158 {
159 StringFormatOutput output(StringEncoding::Utf8, console, true);
160 SC_TRY(spec.writeHelp(output));
161 console.flush();
162 return Result(true);
163 }
164 if (parseResult.status == CommandLineParseResult::Status::Error)
165 {
166 StringFormatOutput output(StringEncoding::Utf8, console, false);
167 SC_TRY(spec.writeError(parseResult, output));
168 console.flushStdErr();
169 return Result(false);
170 }
171 if (port == 0)
172 {
173 console.printError("Invalid port value: 0\n");
174 return Result(false);
175 }
176
177 sample.port = static_cast<int32_t>(port);
178 SocketNetworking::initNetworking();
179
180 AsyncEventLoop loop;
181 SC_TRY(loop.create());
182 SC_TRY(sample.start(loop));
183 console.print("ApiServer listening on http://{}:{}\n", sample.interface, sample.port);
184 console.print("Routes: GET /health, GET /hello?name=SaneCpp, POST /echo\n");
185 return loop.run();
186}
187} // namespace SC
188
189template <typename CharType>

Callers 1

apiServerMainFunction · 0.70

Calls 7

flushMethod · 0.80
printMethod · 0.80
ResultClass · 0.50
parseMethod · 0.45
createMethod · 0.45
startMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected