comparison OrthancServer/Sources/main.cpp @ 4399:80fd140b12ba

New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Dec 2020 12:21:03 +0100
parents 3aacd2bd8bbc
children ad646ff506d0
comparison
equal deleted inserted replaced
4398:38c22715bb56 4399:80fd140b12ba
37 #include "../../OrthancFramework/Sources/Compatibility.h" 37 #include "../../OrthancFramework/Sources/Compatibility.h"
38 #include "../../OrthancFramework/Sources/DicomFormat/DicomArray.h" 38 #include "../../OrthancFramework/Sources/DicomFormat/DicomArray.h"
39 #include "../../OrthancFramework/Sources/DicomNetworking/DicomAssociationParameters.h" 39 #include "../../OrthancFramework/Sources/DicomNetworking/DicomAssociationParameters.h"
40 #include "../../OrthancFramework/Sources/DicomNetworking/DicomServer.h" 40 #include "../../OrthancFramework/Sources/DicomNetworking/DicomServer.h"
41 #include "../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h" 41 #include "../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h"
42 #include "../../OrthancFramework/Sources/FileStorage/MemoryStorageArea.h"
42 #include "../../OrthancFramework/Sources/HttpServer/FilesystemHttpHandler.h" 43 #include "../../OrthancFramework/Sources/HttpServer/FilesystemHttpHandler.h"
43 #include "../../OrthancFramework/Sources/HttpServer/HttpServer.h" 44 #include "../../OrthancFramework/Sources/HttpServer/HttpServer.h"
44 #include "../../OrthancFramework/Sources/Logging.h" 45 #include "../../OrthancFramework/Sources/Logging.h"
45 #include "../../OrthancFramework/Sources/Lua/LuaFunctionCall.h" 46 #include "../../OrthancFramework/Sources/Lua/LuaFunctionCall.h"
46 #include "../Plugins/Engine/OrthancPlugins.h" 47 #include "../Plugins/Engine/OrthancPlugins.h"
48 #include "Database/SQLiteDatabaseWrapper.h"
47 #include "EmbeddedResourceHttpHandler.h" 49 #include "EmbeddedResourceHttpHandler.h"
48 #include "OrthancConfiguration.h" 50 #include "OrthancConfiguration.h"
49 #include "OrthancFindRequestHandler.h" 51 #include "OrthancFindRequestHandler.h"
50 #include "OrthancGetRequestHandler.h" 52 #include "OrthancGetRequestHandler.h"
51 #include "OrthancInitialization.h" 53 #include "OrthancInitialization.h"
653 << " --verbose\t\tbe verbose in logs" << std::endl 655 << " --verbose\t\tbe verbose in logs" << std::endl
654 << " --trace\t\thighest verbosity in logs (for debug)" << std::endl 656 << " --trace\t\thighest verbosity in logs (for debug)" << std::endl
655 << " --upgrade\t\tallow Orthanc to upgrade the version of the" << std::endl 657 << " --upgrade\t\tallow Orthanc to upgrade the version of the" << std::endl
656 << "\t\t\tdatabase (beware that the database will become" << std::endl 658 << "\t\t\tdatabase (beware that the database will become" << std::endl
657 << "\t\t\tincompatible with former versions of Orthanc)" << std::endl 659 << "\t\t\tincompatible with former versions of Orthanc)" << std::endl
658 << " --no-jobs\t\tDon't restart the jobs that were stored during" << std::endl 660 << " --no-jobs\t\tdon't restart the jobs that were stored during" << std::endl
659 << "\t\t\tthe last execution of Orthanc" << std::endl 661 << "\t\t\tthe last execution of Orthanc" << std::endl
662 << " --openapi=[file]\twrite the OpenAPI documentation and exit" << std::endl
663 << "\t\t\t(if \"file\" is \"-\", dumps to stdout)" << std::endl
660 << " --version\t\toutput version information and exit" << std::endl 664 << " --version\t\toutput version information and exit" << std::endl
661 << std::endl 665 << std::endl
662 << "Fine-tuning of log categories:" << std::endl; 666 << "Fine-tuning of log categories:" << std::endl;
663 667
664 for (size_t i = 0; i < Logging::GetCategoriesCount(); i++) 668 for (size_t i = 0; i < Logging::GetCategoriesCount(); i++)
1743 { 1747 {
1744 LOG(ERROR) << "Cannot write sample configuration as file \"" << target << "\""; 1748 LOG(ERROR) << "Cannot write sample configuration as file \"" << target << "\"";
1745 return -1; 1749 return -1;
1746 } 1750 }
1747 } 1751 }
1752 else if (boost::starts_with(argument, "--openapi="))
1753 {
1754 std::string target = argument.substr(10);
1755
1756 try
1757 {
1758 Json::Value openapi;
1759
1760 {
1761 SQLiteDatabaseWrapper inMemoryDatabase;
1762 inMemoryDatabase.Open();
1763 MemoryStorageArea inMemoryStorage;
1764 ServerContext context(inMemoryDatabase, inMemoryStorage, true /* unit testing */, 0 /* max completed jobs */);
1765 OrthancRestApi restApi(context, false /* no Orthanc Explorer */);
1766 restApi.GenerateOpenApiDocumentation(openapi);
1767 context.Stop();
1768 }
1769
1770 std::string s;
1771 Toolbox::WriteStyledJson(s, openapi);
1772
1773 if (target == "-")
1774 {
1775 std::cout << s; // Print to stdout
1776 }
1777 else
1778 {
1779 SystemToolbox::WriteFile(s, target);
1780 }
1781 return 0;
1782 }
1783 catch (OrthancException&)
1784 {
1785 LOG(ERROR) << "Cannot export OpenAPI documentation as file \"" << target << "\"";
1786 return -1;
1787 }
1788 }
1748 else 1789 else
1749 { 1790 {
1750 LOG(WARNING) << "Option unsupported by the core of Orthanc: " << argument; 1791 LOG(WARNING) << "Option unsupported by the core of Orthanc: " << argument;
1751 } 1792 }
1752 } 1793 }