# HG changeset patch # User Sebastien Jodogne # Date 1786461323 -7200 # Node ID 4f42a9f8e078bb29f65deb528ee9164a017d9f71 # Parent e1cce5515153bd745b7fb260a391f9e5e3795e26 refactoring diff -r e1cce5515153 -r 4f42a9f8e078 OrthancServer/Sources/main.cpp --- a/OrthancServer/Sources/main.cpp Tue Aug 11 17:01:21 2026 +0200 +++ b/OrthancServer/Sources/main.cpp Tue Aug 11 17:15:23 2026 +0200 @@ -1998,6 +1998,73 @@ } +static void ExportOpenApi(const std::string& target) +{ + Json::Value openapi; + + { + SQLiteDatabaseWrapper inMemoryDatabase; + inMemoryDatabase.Open(); + PluginStorageAreaAdapter inMemoryStorage(new MemoryStorageArea); + ServerContext context(inMemoryDatabase, inMemoryStorage, true /* unit testing */, 0 /* max completed jobs */, false /* readonly */); + OrthancRestApi restApi(context, false /* no Orthanc Explorer */); + restApi.GenerateOpenApiDocumentation(openapi); + context.Stop(); + } + + openapi["info"]["version"] = ORTHANC_VERSION; + openapi["info"]["title"] = "Orthanc API"; + openapi["info"]["description"] = + "This is the full documentation of the [REST API](https://orthanc.uclouvain.be/book/users/rest.html) " + "of Orthanc.

This reference is automatically generated from the source code of Orthanc. A " + "[shorter cheat sheet](https://orthanc.uclouvain.be/book/users/rest-cheatsheet.html) is part of " + "the Orthanc Book.

An earlier, manually crafted version from August 2019, is [still available]" + "(2019-08-orthanc-openapi.html), but is not up-to-date anymore ([source]" + "(https://groups.google.com/g/orthanc-users/c/NUiJTEICSl8/m/xKeqMrbqAAAJ))."; + + Json::Value server = Json::objectValue; + server["url"] = "https://orthanc.uclouvain.be/demo/"; + openapi["servers"].append(server); + + std::string s; + Toolbox::WriteStyledJson(s, openapi); + + if (target == "-") + { + std::cout << s; // Print to stdout + } + else + { + SystemToolbox::WriteFile(s, SystemToolbox::PathFromUtf8(target)); + } +} + + +static void ExportCheatSheet(const std::string& target) +{ + std::string cheatsheet; + + { + SQLiteDatabaseWrapper inMemoryDatabase; + inMemoryDatabase.Open(); + PluginStorageAreaAdapter inMemoryStorage(new MemoryStorageArea); + ServerContext context(inMemoryDatabase, inMemoryStorage, true /* unit testing */, 0 /* max completed jobs */, false /* readonly */); + OrthancRestApi restApi(context, false /* no Orthanc Explorer */); + restApi.GenerateReStructuredTextCheatSheet(cheatsheet, "https://orthanc.uclouvain.be/api/index.html"); + context.Stop(); + } + + if (target == "-") + { + std::cout << cheatsheet; // Print to stdout + } + else + { + SystemToolbox::WriteFile(cheatsheet, SystemToolbox::PathFromUtf8(target)); + } +} + + #if defined(_WIN32) && !defined(__MINGW32__) // arguments are passed as UTF-16 on Windows int wmain(int argc, wchar_t *argv[]) @@ -2171,47 +2238,11 @@ } else if (boost::starts_with(argument, "--openapi=")) { - std::string target = argument.substr(10); + const std::string target = argument.substr(argument.find('=') + 1); try { - Json::Value openapi; - - { - SQLiteDatabaseWrapper inMemoryDatabase; - inMemoryDatabase.Open(); - PluginStorageAreaAdapter inMemoryStorage(new MemoryStorageArea); - ServerContext context(inMemoryDatabase, inMemoryStorage, true /* unit testing */, 0 /* max completed jobs */, false /* readonly */); - OrthancRestApi restApi(context, false /* no Orthanc Explorer */); - restApi.GenerateOpenApiDocumentation(openapi); - context.Stop(); - } - - openapi["info"]["version"] = ORTHANC_VERSION; - openapi["info"]["title"] = "Orthanc API"; - openapi["info"]["description"] = - "This is the full documentation of the [REST API](https://orthanc.uclouvain.be/book/users/rest.html) " - "of Orthanc.

This reference is automatically generated from the source code of Orthanc. A " - "[shorter cheat sheet](https://orthanc.uclouvain.be/book/users/rest-cheatsheet.html) is part of " - "the Orthanc Book.

An earlier, manually crafted version from August 2019, is [still available]" - "(2019-08-orthanc-openapi.html), but is not up-to-date anymore ([source]" - "(https://groups.google.com/g/orthanc-users/c/NUiJTEICSl8/m/xKeqMrbqAAAJ))."; - - Json::Value server = Json::objectValue; - server["url"] = "https://orthanc.uclouvain.be/demo/"; - openapi["servers"].append(server); - - std::string s; - Toolbox::WriteStyledJson(s, openapi); - - if (target == "-") - { - std::cout << s; // Print to stdout - } - else - { - SystemToolbox::WriteFile(s, SystemToolbox::PathFromUtf8(target)); - } + ExportOpenApi(target); return 0; } catch (OrthancException&) @@ -2226,26 +2257,7 @@ try { - std::string cheatsheet; - - { - SQLiteDatabaseWrapper inMemoryDatabase; - inMemoryDatabase.Open(); - PluginStorageAreaAdapter inMemoryStorage(new MemoryStorageArea); - ServerContext context(inMemoryDatabase, inMemoryStorage, true /* unit testing */, 0 /* max completed jobs */, false /* readonly */); - OrthancRestApi restApi(context, false /* no Orthanc Explorer */); - restApi.GenerateReStructuredTextCheatSheet(cheatsheet, "https://orthanc.uclouvain.be/api/index.html"); - context.Stop(); - } - - if (target == "-") - { - std::cout << cheatsheet; // Print to stdout - } - else - { - SystemToolbox::WriteFile(cheatsheet, SystemToolbox::PathFromUtf8(target)); - } + ExportCheatSheet(target); return 0; } catch (OrthancException&)