Allow CORS in webserver

This commit is contained in:
Midgard 2021-09-19 03:50:03 +02:00
parent bc78ac5775
commit 72e5b663c5
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -277,6 +277,8 @@ int main(int argc, char** argv)
} }
svr.Get("/api/segments.json", [](const httplib::Request &, httplib::Response &res) { svr.Get("/api/segments.json", [](const httplib::Request &, httplib::Response &res) {
res.set_header("Access-Control-Allow-Origin", "*");
res.set_header("Access-Control-Allow-Methods", "GET");
json j; json j;
int i = 0; int i = 0;
for (LedSegment* ledsegment : ledsegments) { for (LedSegment* ledsegment : ledsegments) {
@ -293,6 +295,8 @@ int main(int argc, char** argv)
}); });
svr.Put("/api/code.json", [](const httplib::Request &req, httplib::Response &res, const httplib::ContentReader &content_reader) { svr.Put("/api/code.json", [](const httplib::Request &req, httplib::Response &res, const httplib::ContentReader &content_reader) {
res.set_header("Access-Control-Allow-Origin", "*");
res.set_header("Access-Control-Allow-Methods", "PUT");
if (req.is_multipart_form_data()) { if (req.is_multipart_form_data()) {
res.set_content("No multipart forms allowed", "text/plain"); res.set_content("No multipart forms allowed", "text/plain");
return true; return true;
@ -316,6 +320,12 @@ int main(int argc, char** argv)
return true; return true;
}); });
svr.Options("/api/code.json", [](const httplib::Request &req, httplib::Response &res) {
res.set_header("Access-Control-Allow-Origin", "*");
res.set_header("Access-Control-Allow-Methods", "PUT");
return true;
});
std::thread httpserverthread(starthttpserver); std::thread httpserverthread(starthttpserver);
ws2811_return_t ret; ws2811_return_t ret;
@ -336,4 +346,4 @@ int main(int argc, char** argv)
ws2811_render(&ledstring); ws2811_render(&ledstring);
std::this_thread::sleep_for(std::chrono::milliseconds(frametime)); std::this_thread::sleep_for(std::chrono::milliseconds(frametime));
} }
} }