-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
backup.cpp
44 lines (34 loc) · 1.15 KB
/
backup.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
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/core/util.h"
#include <cstdlib>
#include "backup.h"
namespace esphome {
namespace backup {
static const char *const TAG = "backup";
static const char *const URL = "/config.yaml";
void Backup::setup() {
ESP_LOGCONFIG(TAG, "Setting up backup handler...");
this->base_->init();
this->base_->add_handler(this);
}
void Backup::dump_config() {
ESP_LOGCONFIG(TAG, "Backup:");
ESP_LOGCONFIG(TAG, " URL path is %s", URL);
if (this->using_auth()) {
ESP_LOGCONFIG(TAG, " Basic authentication enabled");
}
}
bool Backup::canHandle(AsyncWebServerRequest *request) {
return request->method() == HTTP_GET && request->url() == URL;
}
void Backup::handleRequest(AsyncWebServerRequest *request) {
if (this->using_auth() && !request->authenticate(this->username_, this->password_)) {
return request->requestAuthentication();
}
auto *response = request->beginResponse_P(200, "plain/text;charset=UTF-8", ESPHOME_BACKUP_DATA, ESPHOME_BACKUP_SIZE);
response->addHeader("Content-Encoding", "gzip");
request->send(response);
}
} // namespace backup
} // namespace esphome