Repository navigation
Expand file tree
/
Copy pathwp_routes.cpp
More file actions
154 lines (126 loc) · 4.41 KB
/
Copy pathwp_routes.cpp
File metadata and controls
154 lines (126 loc) · 4.41 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "wp_routes.h"
#include <esp_http_server.h>
#include <SPIFFS.h>
#include <esp_log.h>
// === API-Module einbinden ===
#include "web/api_core.h"
#include "web/console_api.h"
#include "web/api_combined.h"
#include "web/pwr_api.h"
#include "web/bat_api.h"
#include "web/stat_api.h"
#include "web/wp_health_api.h"
#include "web/framedump_api.h"
#include "web/wp_connect_api.h"
#include "web/service_api.h"
#include "web/filemanager_api.h"
static const char *TAG = "HTTPD";
httpd_handle_t server = NULL;
// ------------------------------------------------------------
// MIME-Type bestimmen
// ------------------------------------------------------------
static const char* getContentType(const char *path) {
if (strstr(path, ".html")) return "text/html";
if (strstr(path, ".css")) return "text/css";
if (strstr(path, ".js")) return "application/javascript";
if (strstr(path, ".json")) return "application/json";
if (strstr(path, ".png")) return "image/png";
if (strstr(path, ".jpg")) return "image/jpeg";
return "application/octet-stream";
}
// ------------------------------------------------------------
// STATIC FILE HANDLER (Catch-all)
// ------------------------------------------------------------
esp_err_t static_file_handler(httpd_req_t *req)
{
// API-Routen NICHT vom static handler bedienen
if (strncmp(req->uri, "/api/", 5) == 0) {
return ESP_FAIL; // API soll weiter zum API-Handler gehen
}
String path = req->uri;
int q = path.indexOf('?');
if (q >= 0) path = path.substring(0, q);
// Root → Layout
if (path == "/") {
path = "/layout.html";
}
// Dashboard-Link aus der Sidebar → ebenfalls Layout
if (path == "/dashboard") {
path = "/layout.html";
}
// Sidebar-Seiten → Layout laden
if (path == "/dashboard" ||
path == "/basevalue" ||
path == "/celldata" ||
path == "/statistic" ||
path == "/console" ||
path == "/connection" ||
path == "/log" ||
path == "/health" ||
path == "/framedump") {
path = "/layout.html";
}
File f = SPIFFS.open(path, "r");
if (!f) {
httpd_resp_send_404(req);
return ESP_OK;
}
// MIME-Type
if (path.endsWith(".html")) httpd_resp_set_type(req, "text/html");
else if (path.endsWith(".js")) httpd_resp_set_type(req, "application/javascript");
else if (path.endsWith(".css")) httpd_resp_set_type(req, "text/css");
else if (path.endsWith(".json")) httpd_resp_set_type(req, "application/json");
else if (path.endsWith(".png")) httpd_resp_set_type(req, "image/png");
else httpd_resp_set_type(req, "text/plain");
httpd_resp_set_hdr(req, "Cache-Control", "max-age=3600");
// Chunked-Response
uint8_t buf[128];
while (true) {
size_t n = f.read(buf, sizeof(buf));
if (n <= 0) break;
httpd_resp_send_chunk(req, (const char*)buf, n);
}
httpd_resp_send_chunk(req, NULL, 0);
f.close();
return ESP_OK;
}
// ------------------------------------------------------------
// HTTPD starten
// ------------------------------------------------------------
void startHttpd() {
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.server_port = 80;
config.max_uri_handlers = 64;
config.stack_size = 6144;
// FIX: mehrere gleichzeitige Verbindungen erlauben
config.max_open_sockets = 8;
config.keep_alive_enable = false;
config.lru_purge_enable = true;
config.recv_wait_timeout = 30;
config.send_wait_timeout = 30;
config.uri_match_fn = httpd_uri_match_wildcard;
ESP_LOGI(TAG, "Starting HTTP server");
if (httpd_start(&server, &config) != ESP_OK) {
ESP_LOGE(TAG, "httpd_start failed");
server = NULL;
return;
}
registerConsoleAPI();
registerCombinedAPI();
registerPwrAPI();
registerBatAPI();
registerStatAPI();
registerHealthAPI();
registerFramedumpApi();
registerConnectAPI();
registerServiceAPI();
registerFilemanagerAPI();
httpd_uri_t all = {
.uri = "/*",
.method = HTTP_GET,
.handler = static_file_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &all);
ESP_LOGI(TAG, "HTTP server started");
}