-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_server.cpp
More file actions
119 lines (105 loc) · 4.63 KB
/
api_server.cpp
File metadata and controls
119 lines (105 loc) · 4.63 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// api_server.cpp
// REST API Server for Blockchain Voting System
// Run: ./api_server
// Test: curl -X POST http://localhost:8080/addVote -H "Content-Type: application/json" -d '{"voterTempID":"VOTER-001","candidate":"Alice"}'
#include "VotingAPI.h"
#include "Blockchain.h"
#include <crow.h>
#include <iostream>
int main() {
// Initialize blockchain
Blockchain votingChain;
// Initialize API
VotingAPI api(votingChain);
// Create Crow app
crow::SimpleApp app;
std::cout << "\n╔══════════════════════════════════════════════════════════╗" << std::endl;
std::cout << "║ Blockchain Voting System - REST API Server ║" << std::endl;
std::cout << "╚══════════════════════════════════════════════════════════╝\n" << std::endl;
// ============================================================
// POST /addVote - Submit a vote
// ============================================================
CROW_ROUTE(app, "/addVote")
.methods("POST"_method)
([&api](const crow::request& req) {
return api.handleAddVote(req);
});
// ============================================================
// GET /stats - Get voting statistics
// ============================================================
CROW_ROUTE(app, "/stats")
.methods("GET"_method)
([&api](const crow::request& req) {
return api.handleGetStats(req);
});
// ============================================================
// GET /block/:index - Get specific block
// ============================================================
CROW_ROUTE(app, "/block/<int>")
.methods("GET"_method)
([&api](const crow::request& req, int blockIndex) {
return api.handleGetBlock(req, blockIndex);
});
// ============================================================
// GET /deadblock - Get rejected votes
// ============================================================
CROW_ROUTE(app, "/deadblock")
.methods("GET"_method)
([&api](const crow::request& req) {
return api.handleGetDeadBlock(req);
});
// ============================================================
// GET /validate - Validate blockchain integrity
// ============================================================
CROW_ROUTE(app, "/validate")
.methods("GET"_method)
([&api](const crow::request& req) {
return api.handleValidateChain(req);
});
// ============================================================
// GET / - API Documentation
// ============================================================
CROW_ROUTE(app, "/")
([](const crow::request& req) {
crow::json::wvalue response;
response["name"] = "Blockchain Voting System API";
response["version"] = "1.0.0";
response["endpoints"] = crow::json::wvalue::list({
crow::json::wvalue{
{"method", "POST"},
{"path", "/addVote"},
{"description", "Submit a vote to the blockchain"}
},
crow::json::wvalue{
{"method", "GET"},
{"path", "/stats"},
{"description", "Get voting statistics"}
},
crow::json::wvalue{
{"method", "GET"},
{"path", "/block/:index"},
{"description", "Get specific block by index"}
},
crow::json::wvalue{
{"method", "GET"},
{"path", "/deadblock"},
{"description", "Get rejected votes"}
},
crow::json::wvalue{
{"method", "GET"},
{"path", "/validate"},
{"description", "Validate blockchain integrity"}
}
});
return response;
});
// Start server
std::cout << "🚀 Server starting on http://localhost:8080" << std::endl;
std::cout << "📝 API Documentation: http://localhost:8080/" << std::endl;
std::cout << "📊 Statistics: http://localhost:8080/stats" << std::endl;
std::cout << "\n⚡ Ready to accept votes!\n" << std::endl;
app.port(8080)
.multithreaded()
.run();
return 0;
}