Skip to content

Commit 4a27148

Browse files
committed
Update
1 parent 34382b3 commit 4a27148

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

drogon_ctl/create_swagger.cc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,75 @@
3030
#include <fstream>
3131

3232
using namespace drogon_ctl;
33+
34+
static void forEachControllerHeaderIn(
35+
const std::string &path,
36+
const std::function<void(const std::string &)> &cb)
37+
{
38+
DIR *dp;
39+
struct dirent *dirp;
40+
struct stat st;
41+
42+
/* open dirent directory */
43+
if ((dp = opendir(path.c_str())) == NULL)
44+
{
45+
// perror("opendir:");
46+
LOG_ERROR << "can't open dir,path:" << path;
47+
return;
48+
}
49+
50+
/**
51+
* read all files in this dir
52+
**/
53+
while ((dirp = readdir(dp)) != NULL)
54+
{
55+
/* ignore hidden files */
56+
if (dirp->d_name[0] == '.')
57+
continue;
58+
/* get dirent status */
59+
std::string filename = dirp->d_name;
60+
if (filename.find(".h") != filename.length() - 2)
61+
continue;
62+
std::string fullname = path;
63+
fullname.append("/").append(filename);
64+
if (stat(fullname.c_str(), &st) == -1)
65+
{
66+
perror("stat");
67+
closedir(dp);
68+
return;
69+
}
70+
71+
/* if dirent is a directory, find files recursively */
72+
if (S_ISDIR(st.st_mode))
73+
{
74+
forEachControllerHeaderIn(fullname, cb);
75+
}
76+
else
77+
{
78+
cb(fullname);
79+
}
80+
}
81+
closedir(dp);
82+
return;
83+
}
84+
static void parseControllerHeader(const std::string &headerFile,
85+
Json::Value &docs)
86+
{
87+
std::ifstream infile(headerFile);
88+
89+
for (std::string buffer; std::getline(infile, buffer);)
90+
{
91+
}
92+
}
3393
static std::string makeSwaggerDocument(const Json::Value &config)
3494
{
3595
Json::Value ret;
3696
ret["swagger"] = "2.0";
3797
ret["info"] = config.get("info", {});
98+
forEachControllerHeaderIn("controllers", [&ret](const std::string &header) {
99+
std::cout << "Parsing " << header << " ...\n";
100+
parseControllerHeader(header, ret);
101+
});
38102
return ret.toStyledString();
39103
}
40104

0 commit comments

Comments
 (0)