-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
101 lines (82 loc) · 2.3 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "stdafx.h"
#include "PuushServer.h"
#include "PuushDatabase.h"
#include "Configuration.h"
#include <fstream>
bool writeDefaultConfig()
{
std::ofstream os;
os.open("puushd.conf", std::ios_base::out);
if (os.fail())
{
return false;
}
os << "# Default configuration generated by puushd" << std::endl;
os << std::endl;
os << "dbfile = puushd.db" << std::endl;
os << "port = 1200" << std::endl;
os << "puushurl = http://localhost:1200/" << std::endl;
os.flush();
os.close();
return true;
}
int main(int argc, char* argv[])
{
Configuration config;
if (!config.load("puushd.conf"))
{
std::cerr << "Unable to load puushd.conf" << std::endl;
if (!writeDefaultConfig())
{
std::cerr << "Unable to write default configuration to puushd.conf" << std::endl;
return 1;
}
else
{
std::cerr << "Default configuration written to puushd.conf" << std::endl;
if (!config.load("puushd.conf"))
{
std::cerr << "Unable to load newly written configuration" << std::endl;
return 1;
}
}
}
std::cout << std::endl;
std::string databaseFile = config.getString("dbfile", "puushd.db");
PuushDatabase db;
if (!db.load(databaseFile.c_str()))
{
std::cerr << "Unable to load database (" << databaseFile << ")" << std::endl;
return 1;
}
#ifdef WIN32
if (argc >= 4 && _stricmp(argv[1], "adduser") == 0)
#else
if (argc >= 4 && strcasecmp(argv[1], "adduser") == 0)
#endif
{
std::cout << "Adding user '" << argv[2] << "'/'" << argv[3] << "'..." << std::endl;
std::string apiKey = db.addUser(argv[2], argv[3]);
std::cout << "Done. API key: " << apiKey << std::endl;
return 1;
}
std::cout << "Loaded database from " << databaseFile << std::endl;
int userCount = db.getUserCount();
if (userCount > 0)
{
std::cout << "There are " << userCount << " users in the database" << std::endl;
}
else if (userCount == 0)
{
std::cout << "Notice: The database currently contains no users." << std::endl;
std::cout << "Create one with './puushd adduser [email] [password]" << std::endl;
}
int port = config.getInteger("port", 1200);
std::cout << std::endl << "Starting server on port " << port << "..." << std::endl;
PuushServer server(&config, &db);
server.start(port);
getchar();
std::cout << std::endl << "Stopping server..." << std::endl;
server.stop();
return 0;
}