-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
94 lines (86 loc) · 2.6 KB
/
Copy pathexample.cpp
File metadata and controls
94 lines (86 loc) · 2.6 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
#include <iostream>
#include <stdio.h>
#include <time.h>
#include "appweb.h"
static RESRC res;
FUNC_CB_C(api)
{
printf("%s\r\n", req->data.data);
return "{'test':'Hello,World'}";
};
FUNC_CB_C(POST_TEST)
{
#ifdef _WIN32
RESRC_FILE *p = RESRC_select_path(&res, "D:\\Dev\\appweb\\out\\post_test.html");
#else
RESRC_FILE *p = RESRC_select_path(&res, "/workspaces/appweb/out/post_test.html");
#endif
return p->data.data;
};
FUNC_CB_C(test)
{
return "<h1 style='text-align:center;'>Hello,World</h1>";
};
FUNC_CB_C(img)
{
// 在func内部使用send()需要使用func_CB_OUT,因为需要一个“\r\n”字符来终止标头
#ifdef _WIN32
RESRC_FILE *p = RESRC_select_path(&res, "D:\\Dev\\appweb\\out\\image.jpg");
#else
RESRC_FILE *p = RESRC_select_path(&res, "/workspaces/appweb/out/image.jpg");
#endif
FUNC_CB_OUT(req->addr.socket, p->data.data, p->data.length, 0);
// 返回包含“\0”是第一个字符的结果的字符串
return "";
}
void *func(void *arg)
{
#ifdef _WIN32
system("dir");
#else
system("ls -a");
#endif
return NULL;
}
#ifdef _WIN32
static FILE_PATH file_list[] = {
{"D:\\Dev\\appweb\\out\\post_test.html", "rb"},
{"D:\\Dev\\appweb\\out\\image.jpg", "rb"},
{NULL, NULL}};
#else
static FILE_PATH file_list[] = {
{"/workspaces/appweb/out/post_test.html", "rb"},
{"/workspaces/appweb/out/image.jpg", "rb"},
{NULL, NULL}};
#endif //_WIN32
int main(int argc, char const *argv[])
{
LOG_LIGHT_NF("%s\n", "Build application by Yauntyour (https://github.com/yauntyour) with C++");
RESRC_load_filelist(&res, file_list, 2);
#ifdef _WIN32
WS_Init();
#endif
appweb app(8, 10000, 3);
app.set_root_dict_func(test, Type_ALL, "text/html");
Varde home_list[] = {
Varde_def(POST_TEST, Type_GET, "postTest", COMPATH_True, "text/html"),
Varde_def(api, Type_POST, "api", COMPATH_True, "application/json"),
Varde_def(img, Type_GET, "img", COMPATH_True, "image/png"),
};
Varde home_dict[] = {
{test, Type_GET, "home", home_list, 3, 3, COMPATH_True, "text/html"},
{test, Type_GET, "index", home_list, 3, 3, COMPATH_True, "text/html"}};
app.root_dict_p->list_append(home_dict, sizeof(home_dict) / sizeof(Varde));
app.root_dict_p->ZIP();
app.add_event(func, NULL);
app.start(flag_wait);
#ifdef _WIN32
WS_clean();
#endif
for (size_t i = 0; file_list[i].path == NULL; i++)
{
RESRC_free(&(res.filelist[i]));
RESRC_FILE_CLOSE(&(res.filelist[i]));
}
return 0;
}