website progress
This commit is contained in:
parent
6b15c9e20c
commit
e9afde5216
7 changed files with 360 additions and 6 deletions
70
examples/Website/backend/main.cpp
Normal file
70
examples/Website/backend/main.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
Crafter® Build
|
||||
Copyright (C) 2025 Catcrafts®
|
||||
Catcrafts.net
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License version 3.0 as published by the Free Software Foundation;
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
import Crafter.Network;
|
||||
import std;
|
||||
using namespace Crafter;
|
||||
|
||||
struct Note {
|
||||
std::uint32_t id;
|
||||
std::string content;
|
||||
};
|
||||
std::vector<Note> notes;
|
||||
int next_id = 1;
|
||||
|
||||
int main() {
|
||||
ListenerHTTP listener(3000, {
|
||||
{"/createNote", [&](const HTTPRequest& request) {
|
||||
if(request.method == "OPTIONS") {
|
||||
std::cout << CreateResponseHTTP("200 OK", {{"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}, {"Access-Control-Allow-Methods", "*"}}) << std::endl;
|
||||
return CreateResponseHTTP("200 OK", {{"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}, {"Access-Control-Allow-Methods", "*"}});
|
||||
}
|
||||
std::uint32_t id = next_id++;
|
||||
notes.emplace_back(id, request.body);
|
||||
return CreateResponseHTTP("200 OK", {{"Content-Type", "application/json"}, {"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}}, std::format("{}", id));
|
||||
}},
|
||||
|
||||
{"/deleteNote", [&](const HTTPRequest& request) {
|
||||
if(request.method == "OPTIONS") {
|
||||
std::cout << CreateResponseHTTP("200 OK", {{"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}, {"Access-Control-Allow-Methods", "*"}}) << std::endl;
|
||||
return CreateResponseHTTP("200 OK", {{"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}, {"Access-Control-Allow-Methods", "*"}});
|
||||
}
|
||||
int idToRemove = std::stoi(request.body);
|
||||
notes.erase(std::remove_if(notes.begin(), notes.end(),
|
||||
[idToRemove](Note note) {
|
||||
return note.id == idToRemove;
|
||||
}),
|
||||
notes.end());
|
||||
return CreateResponseHTTP("200 OK", {{"Content-Type", "application/json"}, {"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}});
|
||||
}},
|
||||
|
||||
{"/getNotes", [&](const HTTPRequest& request) {
|
||||
if(request.method == "OPTIONS") {
|
||||
std::cout << CreateResponseHTTP("200 OK", {{"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}, {"Access-Control-Allow-Methods", "*"}}) << std::endl;
|
||||
return CreateResponseHTTP("200 OK", {{"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}, {"Access-Control-Allow-Methods", "*"}});
|
||||
}
|
||||
std::string result;
|
||||
for(const Note& note : notes) {
|
||||
result += std::format("{}\n{}\n\n", note.id, note.content);
|
||||
}
|
||||
return CreateResponseHTTP("200 OK", {{"Content-Type", "application/json"}, {"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "*"}}, result);
|
||||
}}
|
||||
});
|
||||
|
||||
listener.Listen();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue