-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
59 lines (44 loc) · 1.61 KB
/
main.cpp
File metadata and controls
59 lines (44 loc) · 1.61 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
// website built with a language meant to control nuclear plants btw
#include "httplib.h"
#include <string>
// using namespace httplib;
// im not an npc to use ts bruh
// i built this because im mentally crazy
// https://open.spotify.com/track/69NExquLGvLHGyb3yj26DH?si=a424c117109847d6
/*
// old
int main()
{
Server svr;
svr.Get("/", [](const Request &, Response &res)
{ res.set_content("<h1>yo from c++</h1>", "text/html"); });
svr.listen("0.0.0.0", 8080);
}
*/
int main()
{
httplib::Server app;
// root
app.Get("/", [](const httplib::Request &, httplib::Response &res)
{ res.set_content("cpp backend alive", "text/plain"); });
// very real api route
app.Get("/api/user", [](const httplib::Request &, httplib::Response &res)
{ res.set_content(
R"({"id":1,"name":"mike","status":"the goat"})",
"application/json"); });
// echo json post
app.Post("/api/echo", [](const httplib::Request &req, httplib::Response &res)
{ res.set_content(req.body, "application/json"); });
// query param
// example: /api/hello?name=lads
app.Get("/api/hello", [](const httplib::Request &req, httplib::Response &res)
{
std::string name = req.get_param_value("name");
res.set_content("hello " + name, "text/plain"); });
app.listen("0.0.0.0", 8080);
}
// g++ main.cpp -std=c++17 -pthread
// or
// g++ main.cpp -std=c++17 -pthread -lws2_32
// (using winsocket -> ws2_32.lib)
// localhost:8080